#include <cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct T3
{
int p;
int k;
int c;
};
T3 *t;
int n, m;
bool Por(T3 a, T3 b)
{
if(a.p < b.p)
return true;
return false;
}
struct Por2
{
bool operator()(const int &a, const int &b)
{
if(t[a].k-t[a].c > t[b].k-t[b].c)
return true;
return false;
}
};
bool wynik()
{
int i, j, q, s=0, v=0, r, a;
priority_queue<int, vector<int>, Por2 > kolejka;
priority_queue<int> res;
vector<int> tab;
for(i=0; i < n;)
{
s=t[i].p;
//cout << s << endl;
for(j=i; j < n && t[j].p == t[i].p; j++)
{
kolejka.push(j);
//cout << j << endl;
}
v=t[j].p;
r=v-s;
for(q=0; q < m; q++)
{
res.push(r);
}
while(res.empty()==false && kolejka.empty()==false)
{
a=kolejka.top();
kolejka.pop();
// cout << "r:" << r << endl;
// cout << "v-r:" << v-r << endl;
// cout << "a:" << a << endl;
r=res.top();
res.pop();
if(v-r + t[a].c > t[a].k)
return false;
if(t[a].c > r)
{
t[a].c-=r;
tab.push_back(a);
}
else if(t[a].c == r)
{
t[a].c-=r;
}
else if(t[a].c < r)
{
res.push(r-t[a].c);
t[a].c=0;
}
}
for(q=0; q < tab.size(); q++)
{
kolejka.push(tab[q]);
}
while(res.empty()==false)
res.pop();
tab.clear();
i=j;
}
for(i=0; i < n; i++)
{
if(t[i].c)
return false;
}
return true;
}
int main() {
int i;
scanf("%d%d", &n, &m);
t = new T3[n+1];
for(i=0; i < n; i++)
{
scanf("%d%d%d", &t[i].p, &t[i].k, &t[i].c);
}
t[n].p = 1e7; t[n].k=2e7; t[n].c=2;
sort(t, t+n, Por);
// for(i=0; i < n; i++)
// printf("%d ", t[i].p);
if(wynik())
printf("TAK");
else
printf("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 | #include <cstdio> #include<algorithm> #include<queue> #include<vector> using namespace std; struct T3 { int p; int k; int c; }; T3 *t; int n, m; bool Por(T3 a, T3 b) { if(a.p < b.p) return true; return false; } struct Por2 { bool operator()(const int &a, const int &b) { if(t[a].k-t[a].c > t[b].k-t[b].c) return true; return false; } }; bool wynik() { int i, j, q, s=0, v=0, r, a; priority_queue<int, vector<int>, Por2 > kolejka; priority_queue<int> res; vector<int> tab; for(i=0; i < n;) { s=t[i].p; //cout << s << endl; for(j=i; j < n && t[j].p == t[i].p; j++) { kolejka.push(j); //cout << j << endl; } v=t[j].p; r=v-s; for(q=0; q < m; q++) { res.push(r); } while(res.empty()==false && kolejka.empty()==false) { a=kolejka.top(); kolejka.pop(); // cout << "r:" << r << endl; // cout << "v-r:" << v-r << endl; // cout << "a:" << a << endl; r=res.top(); res.pop(); if(v-r + t[a].c > t[a].k) return false; if(t[a].c > r) { t[a].c-=r; tab.push_back(a); } else if(t[a].c == r) { t[a].c-=r; } else if(t[a].c < r) { res.push(r-t[a].c); t[a].c=0; } } for(q=0; q < tab.size(); q++) { kolejka.push(tab[q]); } while(res.empty()==false) res.pop(); tab.clear(); i=j; } for(i=0; i < n; i++) { if(t[i].c) return false; } return true; } int main() { int i; scanf("%d%d", &n, &m); t = new T3[n+1]; for(i=0; i < n; i++) { scanf("%d%d%d", &t[i].p, &t[i].k, &t[i].c); } t[n].p = 1e7; t[n].k=2e7; t[n].c=2; sort(t, t+n, Por); // for(i=0; i < n; i++) // printf("%d ", t[i].p); if(wynik()) printf("TAK"); else printf("NIE"); return 0; } |
English