import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class par { final static Car[] EMPTY_ARRAY = new Car[0]; final static Comparator<Car> sourceComparator = new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return o1.x1 - o2.x1; } }; final static Comparator<Car> destinationComparator = new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return o1.destX1 - o2.destX1; } }; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); String[] result = new String[t]; for (int test = 0; test < t; ++test) { int n = sc.nextInt(); int w = sc.nextInt(); int maxW = 0; Car[] source = new Car[n]; for (int i = 0; i < n; ++i) { Car car = new Car(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); source[i] = car; if (car.height > maxW) maxW = car.height; } ArrayList<Car> destinationLeftList = new ArrayList<Car>(n); ArrayList<Car> destinationRightList = new ArrayList<Car>(n); Car[] destinationLeft; Car[] destinationRight; if (maxW == w) { for (int i = 0; i < n; ++i) { Car car = source[i]; car.setDest(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); if (car.destX1 < car.x1) { // Left move destinationLeftList.add(car); } else if (car.destX1 > car.x1) { destinationRightList.add(car); } } } else { // Remove small elements int minW = w - maxW; ArrayList<Car> sourceList = new ArrayList<Car>(n); for (int i = 0; i < n; ++i) { Car car = source[i]; car.setDest(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); if (car.height > minW) { sourceList.add(car); if (car.destX1 < car.x1) { destinationLeftList.add(car); } else if (car.destX1 > car.x1) { destinationRightList.add(car); } } } source = sourceList.toArray(EMPTY_ARRAY); sourceList = null; } if (maxW <= w / 2) { result[test] = "TAK"; continue; } destinationLeft = destinationLeftList.toArray(EMPTY_ARRAY); destinationLeftList = null; destinationRight = destinationRightList.toArray(EMPTY_ARRAY); destinationRightList = null; Arrays.sort(source, sourceComparator); Arrays.sort(destinationLeft, destinationComparator); Arrays.sort(destinationRight, destinationComparator); boolean isFailed = false; for (int i = 0; i < destinationLeft.length; ++i) { Car car = destinationLeft[i]; int idx = 0; while (true) { Car obstacle = source[idx]; if (obstacle.x2 > car.destX1 || obstacle.destX2 > car.destX1) { if (obstacle == car) { break; } if (!(obstacle.x1 > car.x1 && obstacle.destX1 > car.destX1) && !(obstacle.x1 < car.x1 && obstacle.destX1 < car.destX1) && obstacle.height + car.height > w) { isFailed = true; break; } } if (++idx == n) { break; // should never happen, but.. } } if (isFailed) { break; } } if (!isFailed) { for (int i = destinationRight.length - 1; i >= 0; --i) { Car car = destinationRight[i]; int idx = destinationRight.length - 1; while (true) { Car obstacle = source[idx]; if (obstacle.x1 < car.destX2 || obstacle.destX1 < car.destX2) { if (obstacle == car) { break; } if (!(obstacle.x1 < car.x1 && obstacle.destX1 < car.destX1) && !(obstacle.x1 > car.x1 && obstacle.destX1 > car.destX1) && obstacle.height + car.height > w) { isFailed = true; break; } } if (idx-- == 0) { break; // should never happen, but.. } } if (isFailed) { break; } } } result[test] = isFailed ? "NIE" : "TAK"; } for (int i = 0; i < t; ++i) { System.out.println(result[i]); } } } class Car { int x1; int x2; int height; int destX1; int destX2; Car(int x1, int y1, int x2, int y2) { if (x1 < x2) { this.x1 = x1; this.x2 = x2; } else { this.x1 = x2; this.x2 = x1; } if (y1 < y2) { this.height = y2 - y1; } else { this.height = y1 - y2; } } public void setDest(int x1, int y1, int x2, int y2) { if (x1 < x2) { destX1 = x1; destX2 = x2; } else { destX1 = x2; destX2 = x1; } } }
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class par { final static Car[] EMPTY_ARRAY = new Car[0]; final static Comparator<Car> sourceComparator = new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return o1.x1 - o2.x1; } }; final static Comparator<Car> destinationComparator = new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return o1.destX1 - o2.destX1; } }; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); String[] result = new String[t]; for (int test = 0; test < t; ++test) { int n = sc.nextInt(); int w = sc.nextInt(); int maxW = 0; Car[] source = new Car[n]; for (int i = 0; i < n; ++i) { Car car = new Car(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); source[i] = car; if (car.height > maxW) maxW = car.height; } ArrayList<Car> destinationLeftList = new ArrayList<Car>(n); ArrayList<Car> destinationRightList = new ArrayList<Car>(n); Car[] destinationLeft; Car[] destinationRight; if (maxW == w) { for (int i = 0; i < n; ++i) { Car car = source[i]; car.setDest(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); if (car.destX1 < car.x1) { // Left move destinationLeftList.add(car); } else if (car.destX1 > car.x1) { destinationRightList.add(car); } } } else { // Remove small elements int minW = w - maxW; ArrayList<Car> sourceList = new ArrayList<Car>(n); for (int i = 0; i < n; ++i) { Car car = source[i]; car.setDest(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()); if (car.height > minW) { sourceList.add(car); if (car.destX1 < car.x1) { destinationLeftList.add(car); } else if (car.destX1 > car.x1) { destinationRightList.add(car); } } } source = sourceList.toArray(EMPTY_ARRAY); sourceList = null; } if (maxW <= w / 2) { result[test] = "TAK"; continue; } destinationLeft = destinationLeftList.toArray(EMPTY_ARRAY); destinationLeftList = null; destinationRight = destinationRightList.toArray(EMPTY_ARRAY); destinationRightList = null; Arrays.sort(source, sourceComparator); Arrays.sort(destinationLeft, destinationComparator); Arrays.sort(destinationRight, destinationComparator); boolean isFailed = false; for (int i = 0; i < destinationLeft.length; ++i) { Car car = destinationLeft[i]; int idx = 0; while (true) { Car obstacle = source[idx]; if (obstacle.x2 > car.destX1 || obstacle.destX2 > car.destX1) { if (obstacle == car) { break; } if (!(obstacle.x1 > car.x1 && obstacle.destX1 > car.destX1) && !(obstacle.x1 < car.x1 && obstacle.destX1 < car.destX1) && obstacle.height + car.height > w) { isFailed = true; break; } } if (++idx == n) { break; // should never happen, but.. } } if (isFailed) { break; } } if (!isFailed) { for (int i = destinationRight.length - 1; i >= 0; --i) { Car car = destinationRight[i]; int idx = destinationRight.length - 1; while (true) { Car obstacle = source[idx]; if (obstacle.x1 < car.destX2 || obstacle.destX1 < car.destX2) { if (obstacle == car) { break; } if (!(obstacle.x1 < car.x1 && obstacle.destX1 < car.destX1) && !(obstacle.x1 > car.x1 && obstacle.destX1 > car.destX1) && obstacle.height + car.height > w) { isFailed = true; break; } } if (idx-- == 0) { break; // should never happen, but.. } } if (isFailed) { break; } } } result[test] = isFailed ? "NIE" : "TAK"; } for (int i = 0; i < t; ++i) { System.out.println(result[i]); } } } class Car { int x1; int x2; int height; int destX1; int destX2; Car(int x1, int y1, int x2, int y2) { if (x1 < x2) { this.x1 = x1; this.x2 = x2; } else { this.x1 = x2; this.x2 = x1; } if (y1 < y2) { this.height = y2 - y1; } else { this.height = y1 - y2; } } public void setDest(int x1, int y1, int x2, int y2) { if (x1 < x2) { destX1 = x1; destX2 = x2; } else { destX1 = x2; destX2 = x1; } } } |