#include <iostream> #include <stdlib.h> using namespace std; int P; int T; int C; int *c, *p, *k, *sp; int *toRun; bool *r; int taskCompare(const void * a, const void * b) { int x = *(int*)a; int y = *(int*)b; int sx = k[x] - c[x]; int sy = k[y] - c[y]; if(sx - sy == 0) return (k[x] - k[y]); return (sx - sy); } int StartTasks() { int countToRun = 0; for (int i = 0; i < T; ++i) { if (C >= p[i] && c[i] > 0 && k[i] > C)//na czasie i jest robota { if (C + c[i] > k[i]) { return -1; } toRun[countToRun++] = i; } } if (countToRun == 0) return 10000000; qsort(toRun, countToRun, sizeof(int), taskCompare); int end = 10000000; if (countToRun > P) { countToRun = P; int x = toRun[P]; end = k[x] - c[x]; if (end <= C) end = C + 1; } for (int i = 0; i < countToRun; ++i) { int x = toRun[i]; r[x] = true; if (c[x] + C < end) { end = c[x] + C; } if (k[x] < end) end = k[x]; } return end; } void StopTasks(int w) { for (int i = 0; i < T; ++i) { if (r[i]) { c[i] -= w; r[i] = false; } } } int intcompare(const void * a, const void * b) { return (*(int*)a - *(int*)b); } void Sol() { int sum = 0; for (int i = 0; i < T; ++i) { sum += c[i]; } if (sum > P * sp[T]) { cout << "NIE"; return; } r = new bool[T]; toRun = new int[T]; for (int i = 0; i < T; ++i) { r[i] = false; } int spIndex = 0; qsort(sp, T, sizeof(int), intcompare); C = sp[0]; int prevC = C; while (true) { StopTasks(C - prevC); if (spIndex == T) break; int t = StartTasks(); if (t == -1) { cout << "NIE"; return; } prevC = C; if (t < sp[spIndex + 1]) { C = t; } else { C = sp[++spIndex]; while (spIndex < T && C == sp[spIndex + 1]) ++spIndex; } } for (int i = 0; i < T; ++i) { if (c[i] > 0) { cout << "NIE"; return; } } cout << "TAK"; } void Read() { cin >> T; cin >> P; int maxK = 0; c = new int[T]; p = new int[T]; sp = new int[T + 1]; k = new int[T]; for (int i = 0; i < T; ++i) { cin >> sp[i]; p[i] = sp[i]; cin >> k[i]; cin >> c[i]; if (k[i] > maxK) maxK = k[i]; } sp[T] = maxK; } int main() { Read(); if (T <= P) { cout << "TAK"; return 0; } Sol(); 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 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 | #include <iostream> #include <stdlib.h> using namespace std; int P; int T; int C; int *c, *p, *k, *sp; int *toRun; bool *r; int taskCompare(const void * a, const void * b) { int x = *(int*)a; int y = *(int*)b; int sx = k[x] - c[x]; int sy = k[y] - c[y]; if(sx - sy == 0) return (k[x] - k[y]); return (sx - sy); } int StartTasks() { int countToRun = 0; for (int i = 0; i < T; ++i) { if (C >= p[i] && c[i] > 0 && k[i] > C)//na czasie i jest robota { if (C + c[i] > k[i]) { return -1; } toRun[countToRun++] = i; } } if (countToRun == 0) return 10000000; qsort(toRun, countToRun, sizeof(int), taskCompare); int end = 10000000; if (countToRun > P) { countToRun = P; int x = toRun[P]; end = k[x] - c[x]; if (end <= C) end = C + 1; } for (int i = 0; i < countToRun; ++i) { int x = toRun[i]; r[x] = true; if (c[x] + C < end) { end = c[x] + C; } if (k[x] < end) end = k[x]; } return end; } void StopTasks(int w) { for (int i = 0; i < T; ++i) { if (r[i]) { c[i] -= w; r[i] = false; } } } int intcompare(const void * a, const void * b) { return (*(int*)a - *(int*)b); } void Sol() { int sum = 0; for (int i = 0; i < T; ++i) { sum += c[i]; } if (sum > P * sp[T]) { cout << "NIE"; return; } r = new bool[T]; toRun = new int[T]; for (int i = 0; i < T; ++i) { r[i] = false; } int spIndex = 0; qsort(sp, T, sizeof(int), intcompare); C = sp[0]; int prevC = C; while (true) { StopTasks(C - prevC); if (spIndex == T) break; int t = StartTasks(); if (t == -1) { cout << "NIE"; return; } prevC = C; if (t < sp[spIndex + 1]) { C = t; } else { C = sp[++spIndex]; while (spIndex < T && C == sp[spIndex + 1]) ++spIndex; } } for (int i = 0; i < T; ++i) { if (c[i] > 0) { cout << "NIE"; return; } } cout << "TAK"; } void Read() { cin >> T; cin >> P; int maxK = 0; c = new int[T]; p = new int[T]; sp = new int[T + 1]; k = new int[T]; for (int i = 0; i < T; ++i) { cin >> sp[i]; p[i] = sp[i]; cin >> k[i]; cin >> c[i]; if (k[i] > maxK) maxK = k[i]; } sp[T] = maxK; } int main() { Read(); if (T <= P) { cout << "TAK"; return 0; } Sol(); return 0; } |