#include <iostream> #include <cctype> #include <vector> #include <algorithm> #include <random> #define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x))) typedef unsigned int uint; uint get_uint() { int c; while (isspace(c = std::cin.get())); uint value = (c - '0'); while (isdigit(c = std::cin.get())) { value = (10 * value) + (c - '0'); } return value; } static char put_uint_buffer[16]; void put_uint(uint value) { if (value == 0) { std::cout.put('0'); return; } int i; for (i = SIZE_OF_ARRAY(put_uint_buffer); value != 0; value /= 10) { put_uint_buffer[--i] = '0' + (value % 10); } std::cout.write(put_uint_buffer + i, SIZE_OF_ARRAY(put_uint_buffer) - i); } struct data { int x1; int x2; int y1; int y2; bool done; } ple[100001]; inline bool operator<(const data &a, const data &b) { if (a.x1 != b.x1) return a.x1 < b.x1; if (a.x2 != b.x2) return a.x2 < b.x2; if (a.y1 != b.y1) return a.y1 < b.y1; return a.y2 < b.y2; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); for (uint i = 0; i < n; ++i) { ple[i].x1 = get_uint(); ple[i].x2 = get_uint(); ple[i].y1 = get_uint(); ple[i].y2 = get_uint(); ple[i].done = false; } std::mt19937 rng(0x12345432); bool done = false; while (!done) { std::shuffle(ple, ple + n, rng); done = true; for (uint i = 0; i < n; ++i) { if (ple[i].done) { continue; } std::swap(ple[0], ple[i]); data &ple_i = ple[0]; bool updated_i = false; for (uint j = 1; j < n; ++j) { const data &ple_j = ple[j]; if (ple_i.x2 <= ple_j.x1 || ple_j.x2 <= ple_i.x1 || ple_i.y2 <= ple_j.y1 || ple_j.y2 <= ple_i.y1) { continue; } ple_i.x1 = std::min(ple_i.x1, ple_j.x1); ple_i.x2 = std::max(ple_i.x2, ple_j.x2); ple_i.y1 = std::min(ple_i.y1, ple_j.y1); ple_i.y2 = std::max(ple_i.y2, ple_j.y2); ple[j--] = ple[--n]; updated_i = true; } if (updated_i) { done = false; } else { ple_i.done = true; } } } std::sort(ple, ple + n); put_uint(n); std::cout.put('\n'); for (uint i = 0; i < n; ++i) { put_uint(ple[i].x1); std::cout.put(' '); put_uint(ple[i].x2); std::cout.put(' '); put_uint(ple[i].y1); std::cout.put(' '); put_uint(ple[i].y2); std::cout.put('\n'); } 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <iostream> #include <cctype> #include <vector> #include <algorithm> #include <random> #define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x))) typedef unsigned int uint; uint get_uint() { int c; while (isspace(c = std::cin.get())); uint value = (c - '0'); while (isdigit(c = std::cin.get())) { value = (10 * value) + (c - '0'); } return value; } static char put_uint_buffer[16]; void put_uint(uint value) { if (value == 0) { std::cout.put('0'); return; } int i; for (i = SIZE_OF_ARRAY(put_uint_buffer); value != 0; value /= 10) { put_uint_buffer[--i] = '0' + (value % 10); } std::cout.write(put_uint_buffer + i, SIZE_OF_ARRAY(put_uint_buffer) - i); } struct data { int x1; int x2; int y1; int y2; bool done; } ple[100001]; inline bool operator<(const data &a, const data &b) { if (a.x1 != b.x1) return a.x1 < b.x1; if (a.x2 != b.x2) return a.x2 < b.x2; if (a.y1 != b.y1) return a.y1 < b.y1; return a.y2 < b.y2; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); for (uint i = 0; i < n; ++i) { ple[i].x1 = get_uint(); ple[i].x2 = get_uint(); ple[i].y1 = get_uint(); ple[i].y2 = get_uint(); ple[i].done = false; } std::mt19937 rng(0x12345432); bool done = false; while (!done) { std::shuffle(ple, ple + n, rng); done = true; for (uint i = 0; i < n; ++i) { if (ple[i].done) { continue; } std::swap(ple[0], ple[i]); data &ple_i = ple[0]; bool updated_i = false; for (uint j = 1; j < n; ++j) { const data &ple_j = ple[j]; if (ple_i.x2 <= ple_j.x1 || ple_j.x2 <= ple_i.x1 || ple_i.y2 <= ple_j.y1 || ple_j.y2 <= ple_i.y1) { continue; } ple_i.x1 = std::min(ple_i.x1, ple_j.x1); ple_i.x2 = std::max(ple_i.x2, ple_j.x2); ple_i.y1 = std::min(ple_i.y1, ple_j.y1); ple_i.y2 = std::max(ple_i.y2, ple_j.y2); ple[j--] = ple[--n]; updated_i = true; } if (updated_i) { done = false; } else { ple_i.done = true; } } } std::sort(ple, ple + n); put_uint(n); std::cout.put('\n'); for (uint i = 0; i < n; ++i) { put_uint(ple[i].x1); std::cout.put(' '); put_uint(ple[i].x2); std::cout.put(' '); put_uint(ple[i].y1); std::cout.put(' '); put_uint(ple[i].y2); std::cout.put('\n'); } return 0; } |