#include <cstdio>
#include <cstdlib>
#include <list>
#include <algorithm>
using namespace std;
typedef pair< pair<int, int>, pair<int, int> > prostokat;
bool intersect(prostokat &a, prostokat &b) {
return (a.first.first < b.first.second && a.first.second > b.first.first &&
a.second.first < b.second.second && a.second.second > b.second.first);
}
int min(int a, int b) {
return a > b ? b : a;
}
int max(int a, int b) {
return a > b ? a : b;
}
int main() {
int n;
scanf("%d\n", &n);
list< prostokat > l;
for (int i = 0; i < n; ++i) {
int a, b, c, d;
scanf("%d %d %d %d\n", &a, &b, &c, &d);
l.push_back(make_pair(make_pair(a, b), make_pair(c, d)));
}
list<prostokat>::iterator i, j;
bool stop;
do {
stop = true;
for (i = l.begin(); i != l.end(); ++i) {
j = i;
j++;
for (; j != l.end(); ++j) {
if (intersect(*i, *j)) {
(*i) = make_pair(
make_pair(
min(i->first.first, j->first.first),
max(i->first.second, j->first.second)),
make_pair(
min(i->second.first, j->second.first),
max(i->second.second, j->second.second))
);
stop = false;
l.erase(j);
break;
}
}
}
} while (!stop);
l.sort();
printf("%d\n", (int) l.size());
for (i = l.begin(); i != l.end(); ++i) {
printf("%d %d %d %d\n", i->first.first, i->first.second, i->second.first, i->second.second);
}
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 | #include <cstdio> #include <cstdlib> #include <list> #include <algorithm> using namespace std; typedef pair< pair<int, int>, pair<int, int> > prostokat; bool intersect(prostokat &a, prostokat &b) { return (a.first.first < b.first.second && a.first.second > b.first.first && a.second.first < b.second.second && a.second.second > b.second.first); } int min(int a, int b) { return a > b ? b : a; } int max(int a, int b) { return a > b ? a : b; } int main() { int n; scanf("%d\n", &n); list< prostokat > l; for (int i = 0; i < n; ++i) { int a, b, c, d; scanf("%d %d %d %d\n", &a, &b, &c, &d); l.push_back(make_pair(make_pair(a, b), make_pair(c, d))); } list<prostokat>::iterator i, j; bool stop; do { stop = true; for (i = l.begin(); i != l.end(); ++i) { j = i; j++; for (; j != l.end(); ++j) { if (intersect(*i, *j)) { (*i) = make_pair( make_pair( min(i->first.first, j->first.first), max(i->first.second, j->first.second)), make_pair( min(i->second.first, j->second.first), max(i->second.second, j->second.second)) ); stop = false; l.erase(j); break; } } } } while (!stop); l.sort(); printf("%d\n", (int) l.size()); for (i = l.begin(); i != l.end(); ++i) { printf("%d %d %d %d\n", i->first.first, i->first.second, i->second.first, i->second.second); } return 0; } |
English