#include <bits/stdc++.h> using namespace std; #define SYN_ ios_base::sync_with_stdio(0);cin.tie(0) #define REP(I,N) for(int I=0;I<(N);I++) #define FOR(I,A,B) for(int I=(A);I<=(B);I++) #define SZ(v) (int)((v).size()) #define CLR(ar,val) memset(ar,val,sizeof(ar)) #define PR(x) cout<<(#x)<<" = "<<x<<endl #define PRLN printf("\n") #define SHOW(VEC) do{REP(I,SZ(VEC)) cout<<(VEC)[I]<<" "; cout<<endl;}while(0) #define SHOWARR(ARR,N) do{REP(I,N) cout<<(ARR)[I]<<" "; cout<<endl;}while(0) //------------------------ #define REPD(I,N) for(int I=(N)-1;I>=0;I--) #define FORD(I,A,B) for(int I=(A);I>=(B);I--) #define ALL(X) (X).begin(),(X).end() #define SRT(A) sort(ALL(A)) #define ERS(VEC,P) VEC.erase(VEC.begin()+P) #define PB push_back #define MP make_pair #define FI first #define SE second #define INFTY 2000000000 #define MAXN 1000000000 #define SQR(a) ((a)*(a)) #define MIN(a,b) ((a)<(b) ? (a):(b)) #define MAX(a,b) ((a)>(b) ? (a):(b)) #define remin(a,b) ((a) = (b) < (a) ? (b) : (a)) #define remax(a,b) ((a) = (b) > (a) ? (b) : (a)) #define F_MIN(MIND,ARR,N) do{MIND=0; REP(I,N){ if(ARR[MIND]>ARR[I]){MIND = I;}} }while(0) #define F_MAX(MIND,ARR,N) do{MIND=0; REP(I,N){ if(ARR[MIND]<ARR[I]){MIND = I;}} }while(0) #define F_MINV(MIND,VEC) F_MIN(MIND,VEC,SZ(VEC)) #define F_MAXV(MIND,VEC) F_MAX(MIND,VEC,SZ(VEC)) #define DEBUG 0 #define dprintf if(DEBUG) printf typedef vector<int> VI; typedef pair<int,int> PII; typedef long long ll; typedef list<int> LI; typedef priority_queue<int> PQI; typedef vector<PII> VPII; typedef vector<string> VS; struct smok { int d,a,gain,idx; friend bool operator<(smok a, smok b) { if(a.d == b.d) return a.idx < b.idx; else return a.d < b.d; } }; bool cmpIncr(smok a, smok b) { if(a.d == b.d) return a.gain > b.gain; else return a.d < b.d; } bool cmpDecr(smok a, smok b) { if(a.gain == b.gain) return a.d > b.d; else return a.gain > b.gain; } class mycomparisonq { public: bool operator() (const smok& a, const smok& b) const { if(a.gain == b.gain) return a.idx < b.idx; else return a.gain > b.gain; } }; class mycomparisonq2 { public: bool operator() (const smok& a, const smok& b) const { if(a.d == b.d) return a.idx < b.idx; else return a.d > b.d; } }; class mycomparisonq3 { public: bool operator() (const smok& a, const smok& b) const { if(a.a == b.a) return a.idx < b.idx; else return a.a > b.a; } }; bool try1(vector<smok> &decr, int z, int n, VI &res) { multiset<smok, mycomparisonq> q; multiset<smok, mycomparisonq>::const_iterator itq; multiset<smok> qu; multiset<smok>::iterator it; REP(i,SZ(decr)) { qu.insert(decr[i]); q.insert(decr[i]); } /*for (itq=q.begin(); itq!=q.end(); ++itq) dprintf("%d (%d) ",(*itq).gain, (*itq).idx); dprintf("\n"); for (it=qu.begin(); it!=qu.end(); ++it) dprintf("%d (%d) ",(*it).d, (*it).idx); dprintf("\n---\n");*/ for (itq=q.begin(); itq!=q.end();) { smok test; smok curr = *itq; //dprintf("- %d %d (%d)\n",z,curr.gain,curr.idx); test.d = z + curr.gain -1; test.idx = n+2; //dprintf("looking for %d (%d)\n",test.d,test.idx); multiset<smok>::iterator itt = qu.upper_bound(test); if(itt!=qu.end() && (*itt).idx!=curr.idx) { //for (it=qu.begin(); it!=qu.end(); ++it) // dprintf("%d (%d) ",(*it).d, (*it).idx); smok upB = *(itt); //dprintf("u %d (%d)\n",upB.d,upB.idx); ++itq; }else// nie ma takiego ktorego by sie nie dalo rozwalic po zrobieniu curr { if(z<=curr.d) { //printf("NIE\n"); return false; } res.PB(curr.idx); z+=curr.gain; //dprintf("p %d sz %d\n",curr.idx,qu.size()); q.erase(itq); qu.erase(curr); itq=q.begin(); } } if(qu.size()!=0) { //printf("NIE\n"); return false; } /*dprintf("\n---\n"); REP(i,SZ(decr)) { dprintf("%d %d %d %d\n",decr[i].d, decr[i].a, decr[i].gain, decr[i].idx); }*/ return true; } bool try2(vector<smok> &decr, int z, int n, VI &res) { multiset<smok, mycomparisonq2> q; multiset<smok, mycomparisonq2>::const_iterator itq; multiset<smok> qu; multiset<smok>::iterator it; REP(i,SZ(decr)) { qu.insert(decr[i]); q.insert(decr[i]); } /*for (itq=q.begin(); itq!=q.end(); ++itq) dprintf("%d (%d) ",(*itq).gain, (*itq).idx); dprintf("\n"); for (it=qu.begin(); it!=qu.end(); ++it) dprintf("%d (%d) ",(*it).d, (*it).idx); dprintf("\n---\n");*/ for (itq=q.begin(); itq!=q.end();) { smok test; smok curr = *itq; //dprintf("- %d %d (%d)\n",z,curr.gain,curr.idx); test.d = z + curr.gain -1; test.idx = n+2; //dprintf("looking for %d (%d)\n",test.d,test.idx); multiset<smok>::iterator itt = qu.upper_bound(test); if(itt!=qu.end() && (*itt).idx!=curr.idx) { //for (it=qu.begin(); it!=qu.end(); ++it) // dprintf("%d (%d) ",(*it).d, (*it).idx); smok upB = *(itt); //dprintf("u %d (%d)\n",upB.d,upB.idx); ++itq; }else// nie ma takiego ktorego by sie nie dalo rozwalic po zrobieniu curr { if(z<=curr.d) { //printf("NIE\n"); return false; } res.PB(curr.idx); z+=curr.gain; //dprintf("p %d sz %d\n",curr.idx,qu.size()); q.erase(itq); qu.erase(curr); itq=q.begin(); } } if(qu.size()!=0) { //printf("NIE\n"); return false; } /*dprintf("\n---\n"); REP(i,SZ(decr)) { dprintf("%d %d %d %d\n",decr[i].d, decr[i].a, decr[i].gain, decr[i].idx); }*/ return true; } bool try3(vector<smok> &decr, int z, int n, VI &res) { multiset<smok, mycomparisonq3> q; multiset<smok, mycomparisonq3>::const_iterator itq; multiset<smok> qu; multiset<smok>::iterator it; REP(i,SZ(decr)) { qu.insert(decr[i]); q.insert(decr[i]); } /*for (itq=q.begin(); itq!=q.end(); ++itq) dprintf("%d (%d) ",(*itq).gain, (*itq).idx); dprintf("\n"); for (it=qu.begin(); it!=qu.end(); ++it) dprintf("%d (%d) ",(*it).d, (*it).idx); dprintf("\n---\n");*/ for (itq=q.begin(); itq!=q.end();) { smok test; smok curr = *itq; //dprintf("- %d %d (%d)\n",z,curr.gain,curr.idx); test.d = z + curr.gain -1; test.idx = n+2; //dprintf("looking for %d (%d)\n",test.d,test.idx); multiset<smok>::iterator itt = qu.upper_bound(test); if(itt!=qu.end() && (*itt).idx!=curr.idx) { //for (it=qu.begin(); it!=qu.end(); ++it) // dprintf("%d (%d) ",(*it).d, (*it).idx); smok upB = *(itt); //dprintf("u %d (%d)\n",upB.d,upB.idx); ++itq; }else// nie ma takiego ktorego by sie nie dalo rozwalic po zrobieniu curr { if(z<=curr.d) { //printf("NIE\n"); return false; } res.PB(curr.idx); z+=curr.gain; //dprintf("p %d sz %d\n",curr.idx,qu.size()); q.erase(itq); qu.erase(curr); itq=q.begin(); } } if(qu.size()!=0) { //printf("NIE\n"); return false; } /*dprintf("\n---\n"); REP(i,SZ(decr)) { dprintf("%d %d %d %d\n",decr[i].d, decr[i].a, decr[i].gain, decr[i].idx); }*/ return true; } int main() { SYN_; int n,z; scanf("%d %d",&n,&z); vector<smok> incr; vector<smok> zero; vector<smok> decr; VI res; REP(i,n) { smok s; scanf("%d %d",&s.d, &s.a); s.gain = s.a - s.d; //printf("%d %d %d ",s.d, s.a, s.gain); s.idx = i; if(s.gain > 0) incr.PB(s); else if(s.gain ==0) zero.PB(s); else decr.PB(s); } sort(ALL(incr),cmpIncr); /*REP(i,SZ(incr)) { dprintf("%d %d %d %d\n",incr[i].d, incr[i].a, incr[i].gain, incr[i].idx); }*/ REP(i,SZ(incr)) { if(z>incr[i].d) { z+=incr[i].gain; res.PB(incr[i].idx); } else { printf("NIE\n"); return 0; } } /*dprintf("--- %d\n",z); REP(i,SZ(zero)) { dprintf("%d %d %d %d\n",zero[i].d, zero[i].a, zero[i].gain, zero[i].idx); }*/ REP(i,SZ(zero)) { if(z>zero[i].d) { res.PB(zero[i].idx); } else { printf("NIE\n"); return 0; } } //dprintf("--- %d\n",z); sort(ALL(decr),cmpDecr); ll sumDecr = 0; REP(i,SZ(decr))//ktorys zabiera wiecej niz mamy { if(z<=decr[i].d) { printf("NIE\n"); return 0; } sumDecr+=decr[i].gain; } if(-1*sumDecr>=z)//czy starczy zycia na all potwory { printf("NIE\n"); return 0; } dprintf("result: "); //REP(i,SZ(res)) // dprintf("%d ",res[i]); //dprintf("\n"); VI res2(res); VI res3(res); bool isok = try1(decr,z,n,res); if(isok) { printf("TAK\n"); REP(i,SZ(res)) printf("%d ",res[i]+1); } else { bool isok2 = try2(decr,z,n,res2); if(isok2) { printf("TAK\n"); REP(i,SZ(res2)) printf("%d ",res2[i]+1); } else { bool isok3 = try3(decr,z,n,res3); if(isok3) { printf("TAK\n"); REP(i,SZ(res3)) printf("%d ",res3[i]+1); } else { printf("NIE\n"); } } } //printf("---odp:\n"); //printf("TAK\n"); //REP(i,SZ(res)) // printf("%d ",res[i]+1); 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | #include <bits/stdc++.h> using namespace std; #define SYN_ ios_base::sync_with_stdio(0);cin.tie(0) #define REP(I,N) for(int I=0;I<(N);I++) #define FOR(I,A,B) for(int I=(A);I<=(B);I++) #define SZ(v) (int)((v).size()) #define CLR(ar,val) memset(ar,val,sizeof(ar)) #define PR(x) cout<<(#x)<<" = "<<x<<endl #define PRLN printf("\n") #define SHOW(VEC) do{REP(I,SZ(VEC)) cout<<(VEC)[I]<<" "; cout<<endl;}while(0) #define SHOWARR(ARR,N) do{REP(I,N) cout<<(ARR)[I]<<" "; cout<<endl;}while(0) //------------------------ #define REPD(I,N) for(int I=(N)-1;I>=0;I--) #define FORD(I,A,B) for(int I=(A);I>=(B);I--) #define ALL(X) (X).begin(),(X).end() #define SRT(A) sort(ALL(A)) #define ERS(VEC,P) VEC.erase(VEC.begin()+P) #define PB push_back #define MP make_pair #define FI first #define SE second #define INFTY 2000000000 #define MAXN 1000000000 #define SQR(a) ((a)*(a)) #define MIN(a,b) ((a)<(b) ? (a):(b)) #define MAX(a,b) ((a)>(b) ? (a):(b)) #define remin(a,b) ((a) = (b) < (a) ? (b) : (a)) #define remax(a,b) ((a) = (b) > (a) ? (b) : (a)) #define F_MIN(MIND,ARR,N) do{MIND=0; REP(I,N){ if(ARR[MIND]>ARR[I]){MIND = I;}} }while(0) #define F_MAX(MIND,ARR,N) do{MIND=0; REP(I,N){ if(ARR[MIND]<ARR[I]){MIND = I;}} }while(0) #define F_MINV(MIND,VEC) F_MIN(MIND,VEC,SZ(VEC)) #define F_MAXV(MIND,VEC) F_MAX(MIND,VEC,SZ(VEC)) #define DEBUG 0 #define dprintf if(DEBUG) printf typedef vector<int> VI; typedef pair<int,int> PII; typedef long long ll; typedef list<int> LI; typedef priority_queue<int> PQI; typedef vector<PII> VPII; typedef vector<string> VS; struct smok { int d,a,gain,idx; friend bool operator<(smok a, smok b) { if(a.d == b.d) return a.idx < b.idx; else return a.d < b.d; } }; bool cmpIncr(smok a, smok b) { if(a.d == b.d) return a.gain > b.gain; else return a.d < b.d; } bool cmpDecr(smok a, smok b) { if(a.gain == b.gain) return a.d > b.d; else return a.gain > b.gain; } class mycomparisonq { public: bool operator() (const smok& a, const smok& b) const { if(a.gain == b.gain) return a.idx < b.idx; else return a.gain > b.gain; } }; class mycomparisonq2 { public: bool operator() (const smok& a, const smok& b) const { if(a.d == b.d) return a.idx < b.idx; else return a.d > b.d; } }; class mycomparisonq3 { public: bool operator() (const smok& a, const smok& b) const { if(a.a == b.a) return a.idx < b.idx; else return a.a > b.a; } }; bool try1(vector<smok> &decr, int z, int n, VI &res) { multiset<smok, mycomparisonq> q; multiset<smok, mycomparisonq>::const_iterator itq; multiset<smok> qu; multiset<smok>::iterator it; REP(i,SZ(decr)) { qu.insert(decr[i]); q.insert(decr[i]); } /*for (itq=q.begin(); itq!=q.end(); ++itq) dprintf("%d (%d) ",(*itq).gain, (*itq).idx); dprintf("\n"); for (it=qu.begin(); it!=qu.end(); ++it) dprintf("%d (%d) ",(*it).d, (*it).idx); dprintf("\n---\n");*/ for (itq=q.begin(); itq!=q.end();) { smok test; smok curr = *itq; //dprintf("- %d %d (%d)\n",z,curr.gain,curr.idx); test.d = z + curr.gain -1; test.idx = n+2; //dprintf("looking for %d (%d)\n",test.d,test.idx); multiset<smok>::iterator itt = qu.upper_bound(test); if(itt!=qu.end() && (*itt).idx!=curr.idx) { //for (it=qu.begin(); it!=qu.end(); ++it) // dprintf("%d (%d) ",(*it).d, (*it).idx); smok upB = *(itt); //dprintf("u %d (%d)\n",upB.d,upB.idx); ++itq; }else// nie ma takiego ktorego by sie nie dalo rozwalic po zrobieniu curr { if(z<=curr.d) { //printf("NIE\n"); return false; } res.PB(curr.idx); z+=curr.gain; //dprintf("p %d sz %d\n",curr.idx,qu.size()); q.erase(itq); qu.erase(curr); itq=q.begin(); } } if(qu.size()!=0) { //printf("NIE\n"); return false; } /*dprintf("\n---\n"); REP(i,SZ(decr)) { dprintf("%d %d %d %d\n",decr[i].d, decr[i].a, decr[i].gain, decr[i].idx); }*/ return true; } bool try2(vector<smok> &decr, int z, int n, VI &res) { multiset<smok, mycomparisonq2> q; multiset<smok, mycomparisonq2>::const_iterator itq; multiset<smok> qu; multiset<smok>::iterator it; REP(i,SZ(decr)) { qu.insert(decr[i]); q.insert(decr[i]); } /*for (itq=q.begin(); itq!=q.end(); ++itq) dprintf("%d (%d) ",(*itq).gain, (*itq).idx); dprintf("\n"); for (it=qu.begin(); it!=qu.end(); ++it) dprintf("%d (%d) ",(*it).d, (*it).idx); dprintf("\n---\n");*/ for (itq=q.begin(); itq!=q.end();) { smok test; smok curr = *itq; //dprintf("- %d %d (%d)\n",z,curr.gain,curr.idx); test.d = z + curr.gain -1; test.idx = n+2; //dprintf("looking for %d (%d)\n",test.d,test.idx); multiset<smok>::iterator itt = qu.upper_bound(test); if(itt!=qu.end() && (*itt).idx!=curr.idx) { //for (it=qu.begin(); it!=qu.end(); ++it) // dprintf("%d (%d) ",(*it).d, (*it).idx); smok upB = *(itt); //dprintf("u %d (%d)\n",upB.d,upB.idx); ++itq; }else// nie ma takiego ktorego by sie nie dalo rozwalic po zrobieniu curr { if(z<=curr.d) { //printf("NIE\n"); return false; } res.PB(curr.idx); z+=curr.gain; //dprintf("p %d sz %d\n",curr.idx,qu.size()); q.erase(itq); qu.erase(curr); itq=q.begin(); } } if(qu.size()!=0) { //printf("NIE\n"); return false; } /*dprintf("\n---\n"); REP(i,SZ(decr)) { dprintf("%d %d %d %d\n",decr[i].d, decr[i].a, decr[i].gain, decr[i].idx); }*/ return true; } bool try3(vector<smok> &decr, int z, int n, VI &res) { multiset<smok, mycomparisonq3> q; multiset<smok, mycomparisonq3>::const_iterator itq; multiset<smok> qu; multiset<smok>::iterator it; REP(i,SZ(decr)) { qu.insert(decr[i]); q.insert(decr[i]); } /*for (itq=q.begin(); itq!=q.end(); ++itq) dprintf("%d (%d) ",(*itq).gain, (*itq).idx); dprintf("\n"); for (it=qu.begin(); it!=qu.end(); ++it) dprintf("%d (%d) ",(*it).d, (*it).idx); dprintf("\n---\n");*/ for (itq=q.begin(); itq!=q.end();) { smok test; smok curr = *itq; //dprintf("- %d %d (%d)\n",z,curr.gain,curr.idx); test.d = z + curr.gain -1; test.idx = n+2; //dprintf("looking for %d (%d)\n",test.d,test.idx); multiset<smok>::iterator itt = qu.upper_bound(test); if(itt!=qu.end() && (*itt).idx!=curr.idx) { //for (it=qu.begin(); it!=qu.end(); ++it) // dprintf("%d (%d) ",(*it).d, (*it).idx); smok upB = *(itt); //dprintf("u %d (%d)\n",upB.d,upB.idx); ++itq; }else// nie ma takiego ktorego by sie nie dalo rozwalic po zrobieniu curr { if(z<=curr.d) { //printf("NIE\n"); return false; } res.PB(curr.idx); z+=curr.gain; //dprintf("p %d sz %d\n",curr.idx,qu.size()); q.erase(itq); qu.erase(curr); itq=q.begin(); } } if(qu.size()!=0) { //printf("NIE\n"); return false; } /*dprintf("\n---\n"); REP(i,SZ(decr)) { dprintf("%d %d %d %d\n",decr[i].d, decr[i].a, decr[i].gain, decr[i].idx); }*/ return true; } int main() { SYN_; int n,z; scanf("%d %d",&n,&z); vector<smok> incr; vector<smok> zero; vector<smok> decr; VI res; REP(i,n) { smok s; scanf("%d %d",&s.d, &s.a); s.gain = s.a - s.d; //printf("%d %d %d ",s.d, s.a, s.gain); s.idx = i; if(s.gain > 0) incr.PB(s); else if(s.gain ==0) zero.PB(s); else decr.PB(s); } sort(ALL(incr),cmpIncr); /*REP(i,SZ(incr)) { dprintf("%d %d %d %d\n",incr[i].d, incr[i].a, incr[i].gain, incr[i].idx); }*/ REP(i,SZ(incr)) { if(z>incr[i].d) { z+=incr[i].gain; res.PB(incr[i].idx); } else { printf("NIE\n"); return 0; } } /*dprintf("--- %d\n",z); REP(i,SZ(zero)) { dprintf("%d %d %d %d\n",zero[i].d, zero[i].a, zero[i].gain, zero[i].idx); }*/ REP(i,SZ(zero)) { if(z>zero[i].d) { res.PB(zero[i].idx); } else { printf("NIE\n"); return 0; } } //dprintf("--- %d\n",z); sort(ALL(decr),cmpDecr); ll sumDecr = 0; REP(i,SZ(decr))//ktorys zabiera wiecej niz mamy { if(z<=decr[i].d) { printf("NIE\n"); return 0; } sumDecr+=decr[i].gain; } if(-1*sumDecr>=z)//czy starczy zycia na all potwory { printf("NIE\n"); return 0; } dprintf("result: "); //REP(i,SZ(res)) // dprintf("%d ",res[i]); //dprintf("\n"); VI res2(res); VI res3(res); bool isok = try1(decr,z,n,res); if(isok) { printf("TAK\n"); REP(i,SZ(res)) printf("%d ",res[i]+1); } else { bool isok2 = try2(decr,z,n,res2); if(isok2) { printf("TAK\n"); REP(i,SZ(res2)) printf("%d ",res2[i]+1); } else { bool isok3 = try3(decr,z,n,res3); if(isok3) { printf("TAK\n"); REP(i,SZ(res3)) printf("%d ",res3[i]+1); } else { printf("NIE\n"); } } } //printf("---odp:\n"); //printf("TAK\n"); //REP(i,SZ(res)) // printf("%d ",res[i]+1); return 0; } |