#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
//#define DEBUG
const int MAX = 102, MAX_Z = 1000100;
struct Task {
int f, e;
Task(){}
Task(int f, int e): f(f), e(e) {}
};
int n, m, l;
int P[MAX], K[MAX], C[MAX];
int tsize, tmod, __tsize;
Task __T[MAX];
Task* T[2][MAX];
vector<int> E[MAX_Z];
bool next_round() {
int zak = min(tsize, m), idx, maxi = 0, idy = m, new_tsize = 0;
Task* ptr;
#ifdef DEBUG
printf("tsize %d\n", tsize);
for(int i = 0; i < tsize; i++)
printf("%d %d\n", T[tmod][i]->f, T[tmod][i]->e);
#endif
for(int i = 0; i < zak; i++) {
ptr = T[tmod][i];
ptr->e -= 1;
if(maxi < ptr->f) {
idx = i;
maxi = ptr->f;
}
maxi = ptr->f ? maxi : ptr->f > maxi;
}
if(m < tsize) {
if(T[tmod][m]->f == 0) return false;
}
for(int i = m; i < tsize; i ++) {
ptr = T[tmod][i];
ptr->f -= 1;
if(ptr->f == maxi - 1) idy = i;
}
while(tsize > m && T[tmod][idx]->f > T[tmod][idy]->f && idx < idy) {
#ifdef DEBUG
printf("swap %d(%d) %d(%d)\n", idx, T[tmod][idx]->f, idy, T[tmod][idy]->f);
#endif
swap(T[tmod][idx], T[tmod][idy]);
idx++;
idy--;
}
for(int i = 0; i < tsize; i++) {
if(T[tmod][i]->e > 0) {
T[tmod^1][new_tsize] = T[tmod][i];
new_tsize ++;
}
}
tsize = new_tsize;
tmod ^= 1;
return true;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%d%d%d", P + i, K + i, C + i);
K[i] --;
E[P[i]].push_back(i);
l = max(l, K[i]);
}
for(int i = 0; i <= l; i++) {
for(auto x: E[i]) {
__T[__tsize] = (Task(K[x] - P[x] + 1 - C[x], C[x]));
T[tmod][tsize] = &(__T[__tsize]);
tsize ++;
__tsize ++;
}
if(E[i].size()) {
sort(T[tmod], T[tmod] + tsize, [](const Task *a, const Task *b) {return a-> f < b->f;});
}
#ifdef DEBUG
printf("round: %d\n", i);
#endif
if(!next_round()) {
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 94 95 96 97 98 99 100 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; //#define DEBUG const int MAX = 102, MAX_Z = 1000100; struct Task { int f, e; Task(){} Task(int f, int e): f(f), e(e) {} }; int n, m, l; int P[MAX], K[MAX], C[MAX]; int tsize, tmod, __tsize; Task __T[MAX]; Task* T[2][MAX]; vector<int> E[MAX_Z]; bool next_round() { int zak = min(tsize, m), idx, maxi = 0, idy = m, new_tsize = 0; Task* ptr; #ifdef DEBUG printf("tsize %d\n", tsize); for(int i = 0; i < tsize; i++) printf("%d %d\n", T[tmod][i]->f, T[tmod][i]->e); #endif for(int i = 0; i < zak; i++) { ptr = T[tmod][i]; ptr->e -= 1; if(maxi < ptr->f) { idx = i; maxi = ptr->f; } maxi = ptr->f ? maxi : ptr->f > maxi; } if(m < tsize) { if(T[tmod][m]->f == 0) return false; } for(int i = m; i < tsize; i ++) { ptr = T[tmod][i]; ptr->f -= 1; if(ptr->f == maxi - 1) idy = i; } while(tsize > m && T[tmod][idx]->f > T[tmod][idy]->f && idx < idy) { #ifdef DEBUG printf("swap %d(%d) %d(%d)\n", idx, T[tmod][idx]->f, idy, T[tmod][idy]->f); #endif swap(T[tmod][idx], T[tmod][idy]); idx++; idy--; } for(int i = 0; i < tsize; i++) { if(T[tmod][i]->e > 0) { T[tmod^1][new_tsize] = T[tmod][i]; new_tsize ++; } } tsize = new_tsize; tmod ^= 1; return true; } int main() { scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) { scanf("%d%d%d", P + i, K + i, C + i); K[i] --; E[P[i]].push_back(i); l = max(l, K[i]); } for(int i = 0; i <= l; i++) { for(auto x: E[i]) { __T[__tsize] = (Task(K[x] - P[x] + 1 - C[x], C[x])); T[tmod][tsize] = &(__T[__tsize]); tsize ++; __tsize ++; } if(E[i].size()) { sort(T[tmod], T[tmod] + tsize, [](const Task *a, const Task *b) {return a-> f < b->f;}); } #ifdef DEBUG printf("round: %d\n", i); #endif if(!next_round()) { printf("NIE\n"); return 0; } } printf("TAK\n"); } |
English