#include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; # define MAX 107 struct task{ int p, k, c; int b, r; int n; bool null; }; struct event{ bool poczatek; int t, n; }; priority_queue< task > Q; task T[MAX]; task M[MAX]; bool done[MAX]; bool abortTheMission; bool operator<(task a, task b) { // if(a.r == 0) return true; return ( a.r > b.r); } bool operator<(event a, event b) { if( a.t == b.t) return ( a.poczatek < b.poczatek ); else return ( a.t < b.t ); } void wypisz( int n, int m) { for(int i=0; i<n; i++) cout<<" "<<T[i].n<<" "<<T[i].c<<" "<<T[i].r<<endl; for(int i=0; i<m; i++) cout<<" "<<M[i].n<<" "<<M[i].null<<" "<<M[i].r<<endl; } void update(int m, int t) { for(int i = 0; i<m; i++) { if(M[i].null) continue; M[i].r += (t-M[i].b); if(M[i].r < M[i].k) Q.push(M[i]); else done[ M[i].n ] = true; M[i].null = true; T[M[i].n].c -= (t-M[i].b); T[M[i].n].r = M[i].r; } } void update2(int m, int t) { for(int i = 0; i<m; i++) { if(!Q.empty()) { // cout<<"Wchodzi "<<Q.top().n<<" "<<Q.top().r<<endl; M[i] = Q.top(); M[i].b = t; M[i].null = false; Q.pop(); } else break; } /*if(!Q.empty()) if(Q.top().r == t) abortTheMission = true;*/ } int main() { ios_base::sync_with_stdio(0); int n, m; cin>>n>>m; int p, k, c; task ta; event e; //vector<task> T; vector<event> E; for(int i=0; i<n; i++) { cin>>p>>k>>c; ta.p = p; ta.k = k; ta.c = c; ta.n = i; ta.r = k -c; ta.null = false; //T.push_back(ta); T[i] = ta; e.n = i; e.t = p; e.poczatek = true; E.push_back(e); e.t = k; e.poczatek = false; E.push_back(e); } for(int i=0; i<m; i++) M[i].null = true; sort(E.begin(), E.end() ); //for(int i=0; i<E.size(); i++) //cout<<E[i].poczatek<<" "<<E[i].t<<" "<<E[i].n<<endl; int j = 0, t; int y = 10; abortTheMission = false; int siz = E.size(); while((j < siz || !Q.empty()) && !abortTheMission ) { p = k = 2*1000000000; //cout<<Q.size()<<" "<<t<<" "<<Q.top().n<<endl; //cout<<t<<endl; update(m, t); update2(m, t); if(j < siz) p = E[j].t; if(!Q.empty()) { k = Q.top().r; } //cout<<t<<" "<<p<<" "<<k<<endl; if(p<=k) { t = E[j].t; if(E[j].poczatek) { Q.push( T[ E[j].n ]); // cout<<t<<" Wrzucamy "<<E[j].n<<endl; // cout<<Q.top().n<<"!"<<" "<<Q.top().r<<" "<<Q.size()<<endl; done[ E[j].n ] = false; update(m, t); // cout<<Q.top().n<<"!"<<" "<<Q.top().r<<" "<<Q.size()<<endl; update2(m, t); } else { // cout<<t<<" Sprawdzamy "<<E[j].n<<endl; update(m, t); update2(m, t); if(!done[ E[j].n ]) { abortTheMission = true; break; } } j++; } if(k < p) { //break; // cout<<t<<" Aktualizujemy bo "<<Q.top().n<<endl; t = Q.top().r; update(m, t); update2(m, t); if(!Q.empty()) if(Q.top().r == t) abortTheMission = true; } // wypisz(n, m); } string msg = (abortTheMission) ? "NIE" : "TAK"; cout<<msg<<endl; return 0; } /* 3 2 3 8 3 2 5 2 3 7 3 */
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 | #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; # define MAX 107 struct task{ int p, k, c; int b, r; int n; bool null; }; struct event{ bool poczatek; int t, n; }; priority_queue< task > Q; task T[MAX]; task M[MAX]; bool done[MAX]; bool abortTheMission; bool operator<(task a, task b) { // if(a.r == 0) return true; return ( a.r > b.r); } bool operator<(event a, event b) { if( a.t == b.t) return ( a.poczatek < b.poczatek ); else return ( a.t < b.t ); } void wypisz( int n, int m) { for(int i=0; i<n; i++) cout<<" "<<T[i].n<<" "<<T[i].c<<" "<<T[i].r<<endl; for(int i=0; i<m; i++) cout<<" "<<M[i].n<<" "<<M[i].null<<" "<<M[i].r<<endl; } void update(int m, int t) { for(int i = 0; i<m; i++) { if(M[i].null) continue; M[i].r += (t-M[i].b); if(M[i].r < M[i].k) Q.push(M[i]); else done[ M[i].n ] = true; M[i].null = true; T[M[i].n].c -= (t-M[i].b); T[M[i].n].r = M[i].r; } } void update2(int m, int t) { for(int i = 0; i<m; i++) { if(!Q.empty()) { // cout<<"Wchodzi "<<Q.top().n<<" "<<Q.top().r<<endl; M[i] = Q.top(); M[i].b = t; M[i].null = false; Q.pop(); } else break; } /*if(!Q.empty()) if(Q.top().r == t) abortTheMission = true;*/ } int main() { ios_base::sync_with_stdio(0); int n, m; cin>>n>>m; int p, k, c; task ta; event e; //vector<task> T; vector<event> E; for(int i=0; i<n; i++) { cin>>p>>k>>c; ta.p = p; ta.k = k; ta.c = c; ta.n = i; ta.r = k -c; ta.null = false; //T.push_back(ta); T[i] = ta; e.n = i; e.t = p; e.poczatek = true; E.push_back(e); e.t = k; e.poczatek = false; E.push_back(e); } for(int i=0; i<m; i++) M[i].null = true; sort(E.begin(), E.end() ); //for(int i=0; i<E.size(); i++) //cout<<E[i].poczatek<<" "<<E[i].t<<" "<<E[i].n<<endl; int j = 0, t; int y = 10; abortTheMission = false; int siz = E.size(); while((j < siz || !Q.empty()) && !abortTheMission ) { p = k = 2*1000000000; //cout<<Q.size()<<" "<<t<<" "<<Q.top().n<<endl; //cout<<t<<endl; update(m, t); update2(m, t); if(j < siz) p = E[j].t; if(!Q.empty()) { k = Q.top().r; } //cout<<t<<" "<<p<<" "<<k<<endl; if(p<=k) { t = E[j].t; if(E[j].poczatek) { Q.push( T[ E[j].n ]); // cout<<t<<" Wrzucamy "<<E[j].n<<endl; // cout<<Q.top().n<<"!"<<" "<<Q.top().r<<" "<<Q.size()<<endl; done[ E[j].n ] = false; update(m, t); // cout<<Q.top().n<<"!"<<" "<<Q.top().r<<" "<<Q.size()<<endl; update2(m, t); } else { // cout<<t<<" Sprawdzamy "<<E[j].n<<endl; update(m, t); update2(m, t); if(!done[ E[j].n ]) { abortTheMission = true; break; } } j++; } if(k < p) { //break; // cout<<t<<" Aktualizujemy bo "<<Q.top().n<<endl; t = Q.top().r; update(m, t); update2(m, t); if(!Q.empty()) if(Q.top().r == t) abortTheMission = true; } // wypisz(n, m); } string msg = (abortTheMission) ? "NIE" : "TAK"; cout<<msg<<endl; return 0; } /* 3 2 3 8 3 2 5 2 3 7 3 */ |