#include <stdio.h> #include <vector> #include <algorithm> struct Box { Box(long pos, long idx, long height) : pos(pos), idx(idx), height(height){} long pos; long idx; long height; long prevMax; }; int main() { int numberOfTestSeries; scanf("%d", &numberOfTestSeries); long n, w; long x1, x2, y1, y2; std::vector<Box> boxes; std::vector<long> boxesSortedPositions; std::vector<Box> destBoxes; for(int serie = 0; serie<numberOfTestSeries; ++serie) { scanf("%ld %ld", &n, &w); boxes.clear(); destBoxes.clear(); boxesSortedPositions.clear(); boxes.reserve(n); destBoxes.reserve(n); boxesSortedPositions.resize(n); bool isSorted = true; long prevVal = -1; for(long i = 0; i<n; ++i) { scanf("%ld %ld %ld %ld", &x1, &y1, &x2, &y2); long h = y2 - y1; if(h < 0) h = -h; boxes.push_back(Box(std::min(x1, x2), i, h)); if(x1 < prevVal) isSorted = false; else prevVal = x1; } if(!isSorted) std::sort(boxes.begin(), boxes.end(), [](const Box& l, const Box& r){ return l.pos < r.pos; }); long currMaxHeight = 0; for(long i = 0; i < n; ++i) { Box& box = boxes[i]; box.prevMax = currMaxHeight; if(currMaxHeight < box.height) currMaxHeight = box.height; boxesSortedPositions[box.idx] = i; } isSorted = true; prevVal = -1; for(long i = 0; i<n; ++i) { scanf("%ld %ld %ld %ld", &x1, &y1, &x2, &y2); destBoxes.push_back(Box(std::min(x1, x2), i, 0)); if(x1 < prevVal) isSorted = false; else prevVal = x1; } if(!isSorted) std::sort(destBoxes.begin(), destBoxes.end(), [](const Box& l, const Box& r){ return l.pos < r.pos; }); bool possible = true; long i, j; for(i = 0; i<n && possible; ++i) { Box& destBox = destBoxes[i]; long sortedPosition = boxesSortedPositions[destBox.idx]; Box& sortedBox = boxes[sortedPosition]; if(sortedBox.prevMax + sortedBox.height > w) { possible = false; break; } else if(sortedBox.height > sortedBox.prevMax) { long currMax = sortedBox.prevMax; for(j = sortedPosition + 1; j < n; ++j) { if(boxes[j].height == 0) continue; boxes[j].prevMax = currMax; if(sortedBox.height <= boxes[j].height) break; if(boxes[j].height > currMax) currMax = boxes[j].height; } } sortedBox.height = 0; } if(possible) printf("TAK\n"); else printf("NIE\n"); } }
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 | #include <stdio.h> #include <vector> #include <algorithm> struct Box { Box(long pos, long idx, long height) : pos(pos), idx(idx), height(height){} long pos; long idx; long height; long prevMax; }; int main() { int numberOfTestSeries; scanf("%d", &numberOfTestSeries); long n, w; long x1, x2, y1, y2; std::vector<Box> boxes; std::vector<long> boxesSortedPositions; std::vector<Box> destBoxes; for(int serie = 0; serie<numberOfTestSeries; ++serie) { scanf("%ld %ld", &n, &w); boxes.clear(); destBoxes.clear(); boxesSortedPositions.clear(); boxes.reserve(n); destBoxes.reserve(n); boxesSortedPositions.resize(n); bool isSorted = true; long prevVal = -1; for(long i = 0; i<n; ++i) { scanf("%ld %ld %ld %ld", &x1, &y1, &x2, &y2); long h = y2 - y1; if(h < 0) h = -h; boxes.push_back(Box(std::min(x1, x2), i, h)); if(x1 < prevVal) isSorted = false; else prevVal = x1; } if(!isSorted) std::sort(boxes.begin(), boxes.end(), [](const Box& l, const Box& r){ return l.pos < r.pos; }); long currMaxHeight = 0; for(long i = 0; i < n; ++i) { Box& box = boxes[i]; box.prevMax = currMaxHeight; if(currMaxHeight < box.height) currMaxHeight = box.height; boxesSortedPositions[box.idx] = i; } isSorted = true; prevVal = -1; for(long i = 0; i<n; ++i) { scanf("%ld %ld %ld %ld", &x1, &y1, &x2, &y2); destBoxes.push_back(Box(std::min(x1, x2), i, 0)); if(x1 < prevVal) isSorted = false; else prevVal = x1; } if(!isSorted) std::sort(destBoxes.begin(), destBoxes.end(), [](const Box& l, const Box& r){ return l.pos < r.pos; }); bool possible = true; long i, j; for(i = 0; i<n && possible; ++i) { Box& destBox = destBoxes[i]; long sortedPosition = boxesSortedPositions[destBox.idx]; Box& sortedBox = boxes[sortedPosition]; if(sortedBox.prevMax + sortedBox.height > w) { possible = false; break; } else if(sortedBox.height > sortedBox.prevMax) { long currMax = sortedBox.prevMax; for(j = sortedPosition + 1; j < n; ++j) { if(boxes[j].height == 0) continue; boxes[j].prevMax = currMax; if(sortedBox.height <= boxes[j].height) break; if(boxes[j].height > currMax) currMax = boxes[j].height; } } sortedBox.height = 0; } if(possible) printf("TAK\n"); else printf("NIE\n"); } } |