#include <cstdio> #include <algorithm> #define MAX 109 #define DB if(0)printf using namespace std; int timeNow = 0; struct Task { int p, k, c; int value() const { return k - (min(k, timeNow) + c); } }; bool operator<(const Task &a, const Task &b) { return a.value() < b.value(); } Task tasks[MAX]; void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; } bool funComp(const Task &a, const Task &b){return a.p < b.p; } int main() { int n, m, maxk; scanf("%d%d", &n, &m); for (int i=0; i<n; i++) { scanf("%d%d%d", &tasks[i].p, &tasks[i].k, &tasks[i].c); maxk = max(maxk, tasks[i].k); } sort(tasks, tasks + n, funComp); int it = 0, ok = 0; while(timeNow <= maxk && ok < n) { DB("TimeNow: %d\n", timeNow); for (; it < n && tasks[it].p <= timeNow; it++); DB("Tasks in progress: %d\n", it); DB("Tasks done: %d\n", ok); if (it > 0) { sort(tasks + ok, tasks + it - 1); } for (int i=0; i<n; i++) { DB("(%d %d %d), ", tasks[i].p, tasks[i].k, tasks[i].c); } DB("\n"); int mini = min(m, it); for (int i=ok; i<ok + mini && i < n; i++) { if (tasks[i].k < timeNow) mini++; else tasks[i].c -= 1; DB("Running task %d\n", i); } timeNow += 1; for (int i=ok; i<n; i++) { if (tasks[i].value() < 0) { printf("NIE\n"); return 0; } if (tasks[i].c == 0) { swap(tasks[i], tasks[ok]); ok++; } } } for (int i=0; i<n; i++) { if (tasks[i].c > 0) { printf("NIE\n"); return 0; } } printf("TAK\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 | #include <cstdio> #include <algorithm> #define MAX 109 #define DB if(0)printf using namespace std; int timeNow = 0; struct Task { int p, k, c; int value() const { return k - (min(k, timeNow) + c); } }; bool operator<(const Task &a, const Task &b) { return a.value() < b.value(); } Task tasks[MAX]; void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; } bool funComp(const Task &a, const Task &b){return a.p < b.p; } int main() { int n, m, maxk; scanf("%d%d", &n, &m); for (int i=0; i<n; i++) { scanf("%d%d%d", &tasks[i].p, &tasks[i].k, &tasks[i].c); maxk = max(maxk, tasks[i].k); } sort(tasks, tasks + n, funComp); int it = 0, ok = 0; while(timeNow <= maxk && ok < n) { DB("TimeNow: %d\n", timeNow); for (; it < n && tasks[it].p <= timeNow; it++); DB("Tasks in progress: %d\n", it); DB("Tasks done: %d\n", ok); if (it > 0) { sort(tasks + ok, tasks + it - 1); } for (int i=0; i<n; i++) { DB("(%d %d %d), ", tasks[i].p, tasks[i].k, tasks[i].c); } DB("\n"); int mini = min(m, it); for (int i=ok; i<ok + mini && i < n; i++) { if (tasks[i].k < timeNow) mini++; else tasks[i].c -= 1; DB("Running task %d\n", i); } timeNow += 1; for (int i=ok; i<n; i++) { if (tasks[i].value() < 0) { printf("NIE\n"); return 0; } if (tasks[i].c == 0) { swap(tasks[i], tasks[ok]); ok++; } } } for (int i=0; i<n; i++) { if (tasks[i].c > 0) { printf("NIE\n"); return 0; } } printf("TAK\n"); } |