// Grzegorz Bukowiec // PA 2014 - Parking #include <iostream> #include <algorithm> using namespace std; struct Car { int start; int height; int id; }; bool compCar(const Car &a, const Car &b) { return a.start < b.start; } int abs(int a) { if (a >= 0) { return a; } else { return -a; } } int min(int a, int b) { if (a > b) { return b; } else { return a; } } bool merge(Car *tab, Car *tempTab, int L, int P, int w) { int S = (L + P) / 2; for (int i = L; i <= P; ++i) { tempTab[i] = tab[i]; } int i = L; int j = S + 1; int k = L; int maxHeight = 0; while (i <= S && j <= P) { if (tempTab[i].id < tempTab[j].id) { if (tempTab[i].height + maxHeight > w) { return true; } tab[k++] = tempTab[i++]; } else { if (maxHeight < tempTab[j].height) { maxHeight = tempTab[j].height; } tab[k++] = tempTab[j++]; } } while (i <= S) { if (tempTab[i].height + maxHeight > w) { return true; } tab[k++] = tempTab[i++]; } return false; } bool mergesort(Car *tab, Car *tempTab, int L, int P, int w) { if (L < P) { if (mergesort(tab, tempTab, L, (L + P) / 2, w)) { return true; } if (mergesort(tab, tempTab, (L + P) / 2 + 1, P, w)) { return true; } if (merge(tab, tempTab, L, P, w)) { return true; } } return false; } int main() { ios_base::sync_with_stdio(0); int t, n, w, x1, x2, y1, y2; cin >> t; while (t--) { cin >> n >> w; Car *cars = new Car[n]; Car *cars2 = new Car[n]; Car *carsTemp = new Car[n]; int *index = new int[n]; for (int i = 0; i < n; ++i) { cin >> x1 >> y1 >> x2 >> y2; cars[i].start = min(x1, x2); cars[i].height = abs(y2 - y1); cars[i].id = i; } for (int i = 0; i < n; ++i) { cin >> x1 >> y1 >> x2 >> y2; cars2[i].start = min(x1, x2); cars2[i].height = abs(y2 - y1); cars2[i].id = i; } sort(cars, cars + n, compCar); sort(cars2, cars2 + n, compCar); for (int i = 0; i < n; ++i) { index[cars[i].id] = i; } for (int i = 0; i < n; ++i) { cars2[i].id = index[cars2[i].id]; } if (mergesort(cars2, carsTemp, 0, n - 1, w)) { cout << "NIE" << endl; } else { cout << "TAK" << endl; } delete [] cars; delete [] cars2; delete [] carsTemp; delete [] index; } 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 | // Grzegorz Bukowiec // PA 2014 - Parking #include <iostream> #include <algorithm> using namespace std; struct Car { int start; int height; int id; }; bool compCar(const Car &a, const Car &b) { return a.start < b.start; } int abs(int a) { if (a >= 0) { return a; } else { return -a; } } int min(int a, int b) { if (a > b) { return b; } else { return a; } } bool merge(Car *tab, Car *tempTab, int L, int P, int w) { int S = (L + P) / 2; for (int i = L; i <= P; ++i) { tempTab[i] = tab[i]; } int i = L; int j = S + 1; int k = L; int maxHeight = 0; while (i <= S && j <= P) { if (tempTab[i].id < tempTab[j].id) { if (tempTab[i].height + maxHeight > w) { return true; } tab[k++] = tempTab[i++]; } else { if (maxHeight < tempTab[j].height) { maxHeight = tempTab[j].height; } tab[k++] = tempTab[j++]; } } while (i <= S) { if (tempTab[i].height + maxHeight > w) { return true; } tab[k++] = tempTab[i++]; } return false; } bool mergesort(Car *tab, Car *tempTab, int L, int P, int w) { if (L < P) { if (mergesort(tab, tempTab, L, (L + P) / 2, w)) { return true; } if (mergesort(tab, tempTab, (L + P) / 2 + 1, P, w)) { return true; } if (merge(tab, tempTab, L, P, w)) { return true; } } return false; } int main() { ios_base::sync_with_stdio(0); int t, n, w, x1, x2, y1, y2; cin >> t; while (t--) { cin >> n >> w; Car *cars = new Car[n]; Car *cars2 = new Car[n]; Car *carsTemp = new Car[n]; int *index = new int[n]; for (int i = 0; i < n; ++i) { cin >> x1 >> y1 >> x2 >> y2; cars[i].start = min(x1, x2); cars[i].height = abs(y2 - y1); cars[i].id = i; } for (int i = 0; i < n; ++i) { cin >> x1 >> y1 >> x2 >> y2; cars2[i].start = min(x1, x2); cars2[i].height = abs(y2 - y1); cars2[i].id = i; } sort(cars, cars + n, compCar); sort(cars2, cars2 + n, compCar); for (int i = 0; i < n; ++i) { index[cars[i].id] = i; } for (int i = 0; i < n; ++i) { cars2[i].id = index[cars2[i].id]; } if (mergesort(cars2, carsTemp, 0, n - 1, w)) { cout << "NIE" << endl; } else { cout << "TAK" << endl; } delete [] cars; delete [] cars2; delete [] carsTemp; delete [] index; } return 0; } |