#include <iostream> #include <vector> #include <algorithm> struct Field { int x1; int y1; int x2; int y2; Field(int x_1, int y_1, int x_2, int y_2) { x1 = x_1; y1 = y_1; x2 = x_2; y2 = y_2; } }; bool tribeCompare(Field t1, Field t2) { if (t1.x1==t2.x1) { if(t1.x2==t2.x2) { if (t1.y1==t2.y1) { if(t1.y2==t2.y2) return t1.y2<t2.y2; else return 0; } else return t1.y1<t2.y1; } else return t1.x2<t2.x2; } else return t1.x1<t2.x1; } bool includes(int x, int a, int b) { return x >= a and x < b; } bool fieldsCross(Field f1, Field f2) { return (includes(f2.x1, f1.x1, f1.x2) and (includes(f1.y1, f2.y1, f2.y2) or includes(f2.y2, f1.y1, f1.y2) or includes(f2.y1, f1.y1, f1.y2) or includes(f2.y2, f1.y1, f1.y2))); } int main(int argc, const char * argv[]) { //wczytaj dane int i, j, n, x1, y1, x2, y2; std::vector<Field> tribes; scanf("%d", &n); //scanf("%d", &n); tribes.reserve(n); for(int i=0; i<n; i++) { //scanf("%d %d %d %d", &x1, &x2, &y1, &y2); //fscanf(pFile,"%d %d %d %d", &x1, &x2, &y1, &y2); std::cin>>x1; std::cin>>x2; std::cin>>y1; std::cin>>y2; tribes.push_back(Field(x1, y1, x2, y2)); } //posortuj plemiona std::sort(tribes.begin(), tribes.end(), tribeCompare); //scal plemiona i = 0; j = 1; while(i<tribes.size()) { while(j<tribes.size()) { if (fieldsCross(tribes[i],tribes[j])) { // printf("\ncross: i=%d j=%d", i, j); tribes[i].x2 = std::max(tribes[i].x2, tribes[j].x2); tribes[i].y1 = std::min(tribes[i].y1, tribes[j].y1); tribes[i].y2 = std::max(tribes[i].y2, tribes[j].y2); tribes.erase(tribes.begin()+j); //n--; i=0; j=i+1; } else j++; } i++; j=i+1; } //wypisz miasta std::sort(tribes.begin(), tribes.end(), tribeCompare); printf("%lu", tribes.size()); for(int i=0; i<tribes.size(); i++) printf("\n%d %d %d %d", tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2); 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 116 117 118 | #include <iostream> #include <vector> #include <algorithm> struct Field { int x1; int y1; int x2; int y2; Field(int x_1, int y_1, int x_2, int y_2) { x1 = x_1; y1 = y_1; x2 = x_2; y2 = y_2; } }; bool tribeCompare(Field t1, Field t2) { if (t1.x1==t2.x1) { if(t1.x2==t2.x2) { if (t1.y1==t2.y1) { if(t1.y2==t2.y2) return t1.y2<t2.y2; else return 0; } else return t1.y1<t2.y1; } else return t1.x2<t2.x2; } else return t1.x1<t2.x1; } bool includes(int x, int a, int b) { return x >= a and x < b; } bool fieldsCross(Field f1, Field f2) { return (includes(f2.x1, f1.x1, f1.x2) and (includes(f1.y1, f2.y1, f2.y2) or includes(f2.y2, f1.y1, f1.y2) or includes(f2.y1, f1.y1, f1.y2) or includes(f2.y2, f1.y1, f1.y2))); } int main(int argc, const char * argv[]) { //wczytaj dane int i, j, n, x1, y1, x2, y2; std::vector<Field> tribes; scanf("%d", &n); //scanf("%d", &n); tribes.reserve(n); for(int i=0; i<n; i++) { //scanf("%d %d %d %d", &x1, &x2, &y1, &y2); //fscanf(pFile,"%d %d %d %d", &x1, &x2, &y1, &y2); std::cin>>x1; std::cin>>x2; std::cin>>y1; std::cin>>y2; tribes.push_back(Field(x1, y1, x2, y2)); } //posortuj plemiona std::sort(tribes.begin(), tribes.end(), tribeCompare); //scal plemiona i = 0; j = 1; while(i<tribes.size()) { while(j<tribes.size()) { if (fieldsCross(tribes[i],tribes[j])) { // printf("\ncross: i=%d j=%d", i, j); tribes[i].x2 = std::max(tribes[i].x2, tribes[j].x2); tribes[i].y1 = std::min(tribes[i].y1, tribes[j].y1); tribes[i].y2 = std::max(tribes[i].y2, tribes[j].y2); tribes.erase(tribes.begin()+j); //n--; i=0; j=i+1; } else j++; } i++; j=i+1; } //wypisz miasta std::sort(tribes.begin(), tribes.end(), tribeCompare); printf("%lu", tribes.size()); for(int i=0; i<tribes.size(); i++) printf("\n%d %d %d %d", tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2); return 0; } |