#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
using namespace std;
struct prostokat
{
long long x1, x2, y1, y2;
};
bool porownanie(prostokat a, prostokat b)
{
vector<long long> c_1;
c_1.push_back(a.x1);
c_1.push_back(a.x2);
c_1.push_back(a.y1);
c_1.push_back(a.y2);
vector<long long> c_2;
c_2.push_back(b.x1);
c_2.push_back(b.x2);
c_2.push_back(b.y1);
c_2.push_back(b.y2);
if (lexicographical_compare(c_1.begin(), c_1.end(), c_2.begin(), c_2.end())) return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(0);
int n;
cin >> n;
list<prostokat> pro(n);
for (list<prostokat>::iterator iter = pro.begin(); iter != pro.end(); ++iter)
{
cin >> iter->x1 >> iter->x2 >> iter->y1 >> iter->y2;
}
bool poprawy = true;
while (poprawy)
{
poprawy = false;
for (list<prostokat>::iterator iter = pro.begin(); iter != pro.end(); ++iter)
{
for (list<prostokat>::iterator iter2 = pro.begin(); iter2 != pro.end(); ++iter2)
{
if (iter != iter2)
{
long long max_x = max(iter->x2, iter2->x2), min_x = min(iter->x1, iter2->x1);
long long max_y = max(iter->y2, iter2->y2), min_y = min(iter->y1, iter2->y1);
if (!(max_x - min_x >= iter->x2 - iter->x1 + iter2->x2 - iter2->x1 ||
max_y - min_y >= iter->y2 - iter->y1 + iter2->y2 - iter2->y1))
{
poprawy = true;
iter->x1 = min(iter->x1, iter2->x1);
iter->x2 = max(iter->x2, iter2->x2);
iter->y1 = min(iter->y1, iter2->y1);
iter->y2 = max(iter->y2, iter2->y2);
pro.erase(iter2);
iter2 = pro.begin();
}
}
}
}
}
pro.sort(porownanie);
cout << pro.size() << endl;
for (list<prostokat>::iterator iter = pro.begin(); iter != pro.end(); ++iter)
{
cout << iter->x1 << " " << iter->x2 << " " << iter->y1 << " " << iter->y2 << endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <iostream> #include <list> #include <algorithm> #include <vector> using namespace std; struct prostokat { long long x1, x2, y1, y2; }; bool porownanie(prostokat a, prostokat b) { vector<long long> c_1; c_1.push_back(a.x1); c_1.push_back(a.x2); c_1.push_back(a.y1); c_1.push_back(a.y2); vector<long long> c_2; c_2.push_back(b.x1); c_2.push_back(b.x2); c_2.push_back(b.y1); c_2.push_back(b.y2); if (lexicographical_compare(c_1.begin(), c_1.end(), c_2.begin(), c_2.end())) return true; return false; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; list<prostokat> pro(n); for (list<prostokat>::iterator iter = pro.begin(); iter != pro.end(); ++iter) { cin >> iter->x1 >> iter->x2 >> iter->y1 >> iter->y2; } bool poprawy = true; while (poprawy) { poprawy = false; for (list<prostokat>::iterator iter = pro.begin(); iter != pro.end(); ++iter) { for (list<prostokat>::iterator iter2 = pro.begin(); iter2 != pro.end(); ++iter2) { if (iter != iter2) { long long max_x = max(iter->x2, iter2->x2), min_x = min(iter->x1, iter2->x1); long long max_y = max(iter->y2, iter2->y2), min_y = min(iter->y1, iter2->y1); if (!(max_x - min_x >= iter->x2 - iter->x1 + iter2->x2 - iter2->x1 || max_y - min_y >= iter->y2 - iter->y1 + iter2->y2 - iter2->y1)) { poprawy = true; iter->x1 = min(iter->x1, iter2->x1); iter->x2 = max(iter->x2, iter2->x2); iter->y1 = min(iter->y1, iter2->y1); iter->y2 = max(iter->y2, iter2->y2); pro.erase(iter2); iter2 = pro.begin(); } } } } } pro.sort(porownanie); cout << pro.size() << endl; for (list<prostokat>::iterator iter = pro.begin(); iter != pro.end(); ++iter) { cout << iter->x1 << " " << iter->x2 << " " << iter->y1 << " " << iter->y2 << endl; } return 0; } |
English