#include <iostream> #include <sstream> #include <fstream> #include <string> #include <vector> #include <queue> #include <algorithm> #include <map> #include <set> #include <cassert> using namespace std; typedef long long LL; #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(val) ((val) < 0 ? -(val) : (val)) #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second const int INF = 1e9; /* PA-2016 R5 */ /* Heura! */ struct process { int p; int k; int c; int pauses; } procs [105]; int evs [210]; bool operator<(const process &lhs, const process &rhs) { if (lhs.k == rhs.k) { if(lhs.p == rhs.p) { return lhs.c < rhs.c; } return lhs.p < rhs.p; } return lhs.k < rhs.k; } int compare_int(const void * a, const void * b) { return *((int *)a) - *((int *)b); } int main() { int n, m; scanf("%d %d", &n, &m); REP(x, n) { scanf("%d %d %d", &procs[x].p, &procs[x].k, &procs[x].c); procs[x].pauses = procs[x].k - procs[x].p - procs[x].c; //printf("%d has %d pauses left!\n", x, procs[x].pauses); evs[2 * x] = procs[x].p; evs[2 * x + 1] = procs[x].k; } qsort (evs, 2 * n, sizeof(int), compare_int); /* printf("Events:\n"); REP(i, 2 * n) printf("%d ", evs[i]); printf("\n"); */ vector<process> actives; int fund = 0; int i = 0; while(i < 2 * n) { //printf("\n\n ** Event nr %d, EVT: %d, FUND: %d\n\n", i, evs[i], fund); int ev = evs[i]; int last_ev = (i == 0) ? ev : evs[i-1]; if (i > 0 && ev == last_ev) { i++; continue; // same ev } // * ANALYSE //printf(" * Analyse\n"); //printf("actives.size = %lu\n", actives.size()); int max_spend = 0; int max_pause = 0; int length = ev - last_ev; //printf("LAST_EV %d, EV %d, LENGTH %d\n", last_ev, ev, length); //assert(i == 0 || length > 0); int u = 0; FOREACH(it, actives) { int c_max_spend = MIN(it->c, length); int c_max_pause = MIN(it->pauses, length); //printf("[%d]: it->c %d, it->pauses %d, length %d\n", u++, it->c, it->pauses, length); // ??? //assert(c_max_spend + c_max_pause >= length); max_spend += c_max_spend; max_pause += c_max_pause; } int max_proc_count_in_use = MIN(m, actives.size()); //printf("TOTAL: max_spend %d, max_pause %d, max_in_use * length = %d = (%d) * (%d)\n", max_spend, max_pause, max_proc_count_in_use * length, max_proc_count_in_use, length); std::stable_sort(ALL(actives)); // int fund_security = 0; int full_deduct = 0; // int from_fund = 0; FOREACH(it, actives) { int c_max_spend = MIN(it->c, length); //assert(c_max_spend <= length); //printf(" update [%d, %d]\n", it->p, it->k); //printf(" update BEFORE it->c %d, it->pauses %d, \"max_spend\" %d\n", it->c, it->pauses, c_max_spend); int deduct = c_max_spend; full_deduct += deduct; it->c -= deduct; //assert(it->c >= 0); int deduct_pauses = length - deduct; //assert(deduct_pauses >= 0); //assert(it->pauses >= deduct_pauses); it->pauses -= deduct_pauses; //assert(it->pauses >= 0); // fund_security += it->pauses; //printf(" update AFTER it->c %d, it->pauses %d, deducted %d\n", it->c, it->pauses, deduct); } //printf(" Fund update: SECURED_BY %d\n", fund_security); // printf(" Fund update: BEFORE %d\n", fund); //assert(full_deduct == max_spend); int can_be_spent = max_proc_count_in_use * length; fund += full_deduct - can_be_spent; // printf(" Fund update: can_be_spent %d, full_deduct %d, (+) (%d); from_fund (-) (%d)\n", can_be_spent, full_deduct, can_be_spent - full_deduct, from_fund); // printf(" Fund update: AFTER %d\n", fund); // * REMOVE closed //printf(" * Remove\n"); vector<process> closing; vector<process>::iterator it; it = actives.begin(); while(it != actives.end()) { if (it->k == ev) { //assert(it->c == 0); //assert(it->pauses == 0); closing.PB(*it); it = actives.erase(it); } else { it++; } } // * ADD OPENING //printf(" * Add\n"); REP(x, n) { if (procs[x].p == ev) { // printf("Adding beggining with ev == %d\n", ev); actives.PB(procs[x]); } } // std::stable_sort(ALL(actives),compare_ends); i++; } //printf("EXIT: FUND: %d\n", fund); if (fund <= 0) { printf("TAK\n"); } else { printf("NIE\n"); } //assert(actives.size() == 0); 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | #include <iostream> #include <sstream> #include <fstream> #include <string> #include <vector> #include <queue> #include <algorithm> #include <map> #include <set> #include <cassert> using namespace std; typedef long long LL; #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(val) ((val) < 0 ? -(val) : (val)) #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second const int INF = 1e9; /* PA-2016 R5 */ /* Heura! */ struct process { int p; int k; int c; int pauses; } procs [105]; int evs [210]; bool operator<(const process &lhs, const process &rhs) { if (lhs.k == rhs.k) { if(lhs.p == rhs.p) { return lhs.c < rhs.c; } return lhs.p < rhs.p; } return lhs.k < rhs.k; } int compare_int(const void * a, const void * b) { return *((int *)a) - *((int *)b); } int main() { int n, m; scanf("%d %d", &n, &m); REP(x, n) { scanf("%d %d %d", &procs[x].p, &procs[x].k, &procs[x].c); procs[x].pauses = procs[x].k - procs[x].p - procs[x].c; //printf("%d has %d pauses left!\n", x, procs[x].pauses); evs[2 * x] = procs[x].p; evs[2 * x + 1] = procs[x].k; } qsort (evs, 2 * n, sizeof(int), compare_int); /* printf("Events:\n"); REP(i, 2 * n) printf("%d ", evs[i]); printf("\n"); */ vector<process> actives; int fund = 0; int i = 0; while(i < 2 * n) { //printf("\n\n ** Event nr %d, EVT: %d, FUND: %d\n\n", i, evs[i], fund); int ev = evs[i]; int last_ev = (i == 0) ? ev : evs[i-1]; if (i > 0 && ev == last_ev) { i++; continue; // same ev } // * ANALYSE //printf(" * Analyse\n"); //printf("actives.size = %lu\n", actives.size()); int max_spend = 0; int max_pause = 0; int length = ev - last_ev; //printf("LAST_EV %d, EV %d, LENGTH %d\n", last_ev, ev, length); //assert(i == 0 || length > 0); int u = 0; FOREACH(it, actives) { int c_max_spend = MIN(it->c, length); int c_max_pause = MIN(it->pauses, length); //printf("[%d]: it->c %d, it->pauses %d, length %d\n", u++, it->c, it->pauses, length); // ??? //assert(c_max_spend + c_max_pause >= length); max_spend += c_max_spend; max_pause += c_max_pause; } int max_proc_count_in_use = MIN(m, actives.size()); //printf("TOTAL: max_spend %d, max_pause %d, max_in_use * length = %d = (%d) * (%d)\n", max_spend, max_pause, max_proc_count_in_use * length, max_proc_count_in_use, length); std::stable_sort(ALL(actives)); // int fund_security = 0; int full_deduct = 0; // int from_fund = 0; FOREACH(it, actives) { int c_max_spend = MIN(it->c, length); //assert(c_max_spend <= length); //printf(" update [%d, %d]\n", it->p, it->k); //printf(" update BEFORE it->c %d, it->pauses %d, \"max_spend\" %d\n", it->c, it->pauses, c_max_spend); int deduct = c_max_spend; full_deduct += deduct; it->c -= deduct; //assert(it->c >= 0); int deduct_pauses = length - deduct; //assert(deduct_pauses >= 0); //assert(it->pauses >= deduct_pauses); it->pauses -= deduct_pauses; //assert(it->pauses >= 0); // fund_security += it->pauses; //printf(" update AFTER it->c %d, it->pauses %d, deducted %d\n", it->c, it->pauses, deduct); } //printf(" Fund update: SECURED_BY %d\n", fund_security); // printf(" Fund update: BEFORE %d\n", fund); //assert(full_deduct == max_spend); int can_be_spent = max_proc_count_in_use * length; fund += full_deduct - can_be_spent; // printf(" Fund update: can_be_spent %d, full_deduct %d, (+) (%d); from_fund (-) (%d)\n", can_be_spent, full_deduct, can_be_spent - full_deduct, from_fund); // printf(" Fund update: AFTER %d\n", fund); // * REMOVE closed //printf(" * Remove\n"); vector<process> closing; vector<process>::iterator it; it = actives.begin(); while(it != actives.end()) { if (it->k == ev) { //assert(it->c == 0); //assert(it->pauses == 0); closing.PB(*it); it = actives.erase(it); } else { it++; } } // * ADD OPENING //printf(" * Add\n"); REP(x, n) { if (procs[x].p == ev) { // printf("Adding beggining with ev == %d\n", ev); actives.PB(procs[x]); } } // std::stable_sort(ALL(actives),compare_ends); i++; } //printf("EXIT: FUND: %d\n", fund); if (fund <= 0) { printf("TAK\n"); } else { printf("NIE\n"); } //assert(actives.size() == 0); return 0; } |