Open 13o4t opened 6 years ago
里面的 sort 就导致了时间复杂度不是 O(n) 吧
#include <iostream>
#include <vector>
#include <tuple>
#include <list>
#include <algorithm>
using namespace std;
vector<tuple<int, int, int, int>> merge(vector<tuple<int, int, int, int>> rectangles)
{
list<tuple<int, int, int, int>> res(rectangles.begin(), rectangles.end());
int u1, d1, l1, r1, u2, d2, l2, r2;
for (auto it1 = res.begin(); it1 != res.end(); ) {
bool flag = false;
for (auto it2 = res.begin(); it2 != it1; ++it2) {
tie(u1, d1, l1, r1) = *it1;
tie(u2, d2, l2, r2) = *it2;
if (min(u1, u2) >= max(d1, d2) && max(l1, l2) <= min(r1, r2)) {
res.emplace_back(max(u1, u2), min(d1, d2), min(l1, l2), max(r1, r2));
res.erase(it2);
it1 = res.erase(it1);
flag = true;
break;
}
}
if (!flag) it1++;
}
return {res.begin(), res.end()};
}
int main(void)
{
vector<tuple<int, int, int, int>> input, output;
int u, d, l, r;
while (cin >> u >> d >> l >> r) {
input.emplace_back(u, d, l, r);
}
output = merge(input);
for (int i = 0; i < output.size(); ++i) {
tie(u, d, l, r) = output[i];
cout << u << " " << d << " " << l << " " << r << endl;
}
return 0;
}
g++ -std=c++11
看了 @palayutm 大的代码收获颇多
Question_ID: Q003
Language: C++
Time Cost: 20-mins
Time Complexity: O(n)
Solution
My Code
Other
gcc --std=c++11