#include <cstdio> #include <vector> #include <map> #include <queue> #include <cmath> #include <algorithm> #include <functional> using namespace std; struct Task { int p; int k; int c; int w; void calcW() { w = k - p - c; } }; Task tasks[101]; vector<int> orderedTaskIds; vector<int> orderedTaskIds2; bool wayToSort(int i, int j) { if (tasks[i].p == tasks[j].p) { if (tasks[i].w == tasks[j].w) { return tasks[i].c > tasks[j].c; } return tasks[i].w < tasks[j].w; } return tasks[i].p < tasks[j].p; } int main() { bool ans = true; int n, m; scanf("%d%d", &n, &m); priority_queue<int, vector<int>, std::greater<int>> t; orderedTaskIds.resize(n); for (int i = 0; i < n; ++i) { scanf("%d%d%d", &tasks[i].p, &tasks[i].k, &tasks[i].c); t.push(tasks[i].p); t.push(tasks[i].k); orderedTaskIds[i] = i; tasks[i].calcW(); } t.push(10000001); int jjj = 0; while (!t.empty() && !orderedTaskIds.empty()) { jjj++; int now = t.top(); t.pop(); sort(orderedTaskIds.begin(), orderedTaskIds.end(), wayToSort); /* printf("=================%d\n", now); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; printf("%d %d %d %d %d\n", id, tasks[id].p, tasks[id].k, tasks[id].c, tasks[id].w); } printf("=================\n");*/ int next = t.top(); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; if (tasks[id].p != now) break; if (i < m) { next = min(next, now + tasks[id].c); } else { if (tasks[id].w > 0) { next = min(next, now + tasks[id].w); } /*int dfg = 9; printf("==========CUT\n"); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; printf("%d %d %d\n", tasks[id].p, tasks[id].k, tasks[id].c); } printf("==========CUT\n"); exit(0);*/ } } //next = min(next, now + 100); int d = next - now; if (d > 0) { t.push(next); orderedTaskIds2.clear(); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; if (tasks[id].p == now) { if (i < m) { tasks[id].c -= d; } tasks[id].p = next; tasks[id].calcW(); if (tasks[id].w < 0) { ans = false; } } if (tasks[id].c != 0) { orderedTaskIds2.push_back(id); } } orderedTaskIds.swap(orderedTaskIds2); } if (ans == false) break; } printf(ans ? "TAK" : "NIE"); 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 | #include <cstdio> #include <vector> #include <map> #include <queue> #include <cmath> #include <algorithm> #include <functional> using namespace std; struct Task { int p; int k; int c; int w; void calcW() { w = k - p - c; } }; Task tasks[101]; vector<int> orderedTaskIds; vector<int> orderedTaskIds2; bool wayToSort(int i, int j) { if (tasks[i].p == tasks[j].p) { if (tasks[i].w == tasks[j].w) { return tasks[i].c > tasks[j].c; } return tasks[i].w < tasks[j].w; } return tasks[i].p < tasks[j].p; } int main() { bool ans = true; int n, m; scanf("%d%d", &n, &m); priority_queue<int, vector<int>, std::greater<int>> t; orderedTaskIds.resize(n); for (int i = 0; i < n; ++i) { scanf("%d%d%d", &tasks[i].p, &tasks[i].k, &tasks[i].c); t.push(tasks[i].p); t.push(tasks[i].k); orderedTaskIds[i] = i; tasks[i].calcW(); } t.push(10000001); int jjj = 0; while (!t.empty() && !orderedTaskIds.empty()) { jjj++; int now = t.top(); t.pop(); sort(orderedTaskIds.begin(), orderedTaskIds.end(), wayToSort); /* printf("=================%d\n", now); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; printf("%d %d %d %d %d\n", id, tasks[id].p, tasks[id].k, tasks[id].c, tasks[id].w); } printf("=================\n");*/ int next = t.top(); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; if (tasks[id].p != now) break; if (i < m) { next = min(next, now + tasks[id].c); } else { if (tasks[id].w > 0) { next = min(next, now + tasks[id].w); } /*int dfg = 9; printf("==========CUT\n"); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; printf("%d %d %d\n", tasks[id].p, tasks[id].k, tasks[id].c); } printf("==========CUT\n"); exit(0);*/ } } //next = min(next, now + 100); int d = next - now; if (d > 0) { t.push(next); orderedTaskIds2.clear(); for (int i = 0; i < orderedTaskIds.size(); ++i) { int id = orderedTaskIds[i]; if (tasks[id].p == now) { if (i < m) { tasks[id].c -= d; } tasks[id].p = next; tasks[id].calcW(); if (tasks[id].w < 0) { ans = false; } } if (tasks[id].c != 0) { orderedTaskIds2.push_back(id); } } orderedTaskIds.swap(orderedTaskIds2); } if (ans == false) break; } printf(ans ? "TAK" : "NIE"); return 0; } |