#include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; class Field { public: int x1; int x2; int y1; int y2; void merge(Field other) { if (x1 > other.x1) x1 = other.x1; if (x2 < other.x2) x2 = other.x2; if (y1 > other.y1) y1 = other.y1; if (y2 < other.y2) y2 = other.y2; } Field(int x1, int x2, int y1, int y2) { if (x1 < x2) { this->x1 = x1; this->x2 = x2; } else { this->x1 = x2; this->x2 = x1; } if (y1 < y2) { this->y1 = y1; this->y2 = y2; } else { this->y1 = y2; this->y2 = y1; } } }; bool fieldComparator(Field a, Field b) { if (a.x1 == b.x1) { if (a.x2 == b.x2) { if (a.y1 == b.y1) { return a.y2 < b.y2; } return a.y1 < b.y1; } return a.x2 < b.x2; } return a.x1 < b.x1; } int main() { int n; cin >> n; Field* fields[n]; for (int i = 0; i < n; ++i) { int x1, y1, x2, y2; cin >> x1; cin >> x2; cin >> y1; cin >> y2; fields[i] = new Field(x1, x2, y1, y2); } bool isMerged = false; while (!isMerged) { isMerged = true; for (int i = 0; i < n; ++i) { Field *base = fields[i]; if (base == nullptr) { continue; } for (int j = i + 1; j < n; ++j) { Field *other = fields[j]; if (other == nullptr) { continue; } if (base->x1 < other->x2 && base->x2 > other->x1 && base->y1 < other->y2 && base->y2 > other->y1) { base->merge(*other); delete fields[j]; fields[j] = nullptr; isMerged = false; } } } } vector<Field> countries; countries.reserve(n); for (int i = 0; i < n; ++i) { Field *maybeCountry = fields[i]; if (maybeCountry != nullptr) { countries.push_back(*maybeCountry); } } sort(countries.begin(), countries.end(), fieldComparator); cout << countries.size() << endl; for (unsigned int i = 0; i < countries.size(); ++i) { Field c = countries[i]; cout << c.x1 << " " << c.x2 << " " << c.y1 << " " << c.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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; class Field { public: int x1; int x2; int y1; int y2; void merge(Field other) { if (x1 > other.x1) x1 = other.x1; if (x2 < other.x2) x2 = other.x2; if (y1 > other.y1) y1 = other.y1; if (y2 < other.y2) y2 = other.y2; } Field(int x1, int x2, int y1, int y2) { if (x1 < x2) { this->x1 = x1; this->x2 = x2; } else { this->x1 = x2; this->x2 = x1; } if (y1 < y2) { this->y1 = y1; this->y2 = y2; } else { this->y1 = y2; this->y2 = y1; } } }; bool fieldComparator(Field a, Field b) { if (a.x1 == b.x1) { if (a.x2 == b.x2) { if (a.y1 == b.y1) { return a.y2 < b.y2; } return a.y1 < b.y1; } return a.x2 < b.x2; } return a.x1 < b.x1; } int main() { int n; cin >> n; Field* fields[n]; for (int i = 0; i < n; ++i) { int x1, y1, x2, y2; cin >> x1; cin >> x2; cin >> y1; cin >> y2; fields[i] = new Field(x1, x2, y1, y2); } bool isMerged = false; while (!isMerged) { isMerged = true; for (int i = 0; i < n; ++i) { Field *base = fields[i]; if (base == nullptr) { continue; } for (int j = i + 1; j < n; ++j) { Field *other = fields[j]; if (other == nullptr) { continue; } if (base->x1 < other->x2 && base->x2 > other->x1 && base->y1 < other->y2 && base->y2 > other->y1) { base->merge(*other); delete fields[j]; fields[j] = nullptr; isMerged = false; } } } } vector<Field> countries; countries.reserve(n); for (int i = 0; i < n; ++i) { Field *maybeCountry = fields[i]; if (maybeCountry != nullptr) { countries.push_back(*maybeCountry); } } sort(countries.begin(), countries.end(), fieldComparator); cout << countries.size() << endl; for (unsigned int i = 0; i < countries.size(); ++i) { Field c = countries[i]; cout << c.x1 << " " << c.x2 << " " << c.y1 << " " << c.y2 << endl; } return 0; } |