#include <iostream> using namespace std; int *dt[100001][2], dd[100000][3],dz[100000],du[100000],nn[100000]; int main() { int z,n,d,a,jd = 0,ju = 0,jz =0, jnn = 0; int i,next; int sz,so = 0; int nf = 0; cin>>n>>z; sz = z; for(i = 0; i < n ; i++) { cin>>d>>a; sz += a; so += d; if (d > a) { du[ju++] = i + 1; } else if (d = a) { dz[jz++] = i + 1; } else { dd[jd][0] = i + 1; dd[jd][1] = a; if(dt[d][0]) { (dt[d][1])[2] = jd + 1; dt[d][1] = dd[jd++]; } else { dt[d][0] = dt[d][1] = dd[jd++]; } } } if(sz > so) { i = 0; while (i < 100001) { while(i < 100001 && !dt[i][0]) i++; if( i == 100001) break; if(i >= z) { cout<<"NIE"; return 0; } nn[jnn++] = dt[i][0][0]; z += dt[i][0][1] - i; next = dt[i][0][2]; while (next) { next--; nn[jnn++] = dd[next][0]; z += dd[next][1] - i; next = dd[next][2]; } i++; } cout<<"TAK\n"; for(i =0; i <jnn ; i++) { if(nf) { cout<<" "; } else { nf = 1; } cout<<nn[i]; } for(i =0; i <jz; i++) { if(nf) { cout<<" "; } else { nf = 1; } cout<<dz[i]; } for(i =0; i <ju; i++) { if(nf) { cout<<" "; } else { nf = 1; } cout<<du[i]; } } else { cout<<"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 | #include <iostream> using namespace std; int *dt[100001][2], dd[100000][3],dz[100000],du[100000],nn[100000]; int main() { int z,n,d,a,jd = 0,ju = 0,jz =0, jnn = 0; int i,next; int sz,so = 0; int nf = 0; cin>>n>>z; sz = z; for(i = 0; i < n ; i++) { cin>>d>>a; sz += a; so += d; if (d > a) { du[ju++] = i + 1; } else if (d = a) { dz[jz++] = i + 1; } else { dd[jd][0] = i + 1; dd[jd][1] = a; if(dt[d][0]) { (dt[d][1])[2] = jd + 1; dt[d][1] = dd[jd++]; } else { dt[d][0] = dt[d][1] = dd[jd++]; } } } if(sz > so) { i = 0; while (i < 100001) { while(i < 100001 && !dt[i][0]) i++; if( i == 100001) break; if(i >= z) { cout<<"NIE"; return 0; } nn[jnn++] = dt[i][0][0]; z += dt[i][0][1] - i; next = dt[i][0][2]; while (next) { next--; nn[jnn++] = dd[next][0]; z += dd[next][1] - i; next = dd[next][2]; } i++; } cout<<"TAK\n"; for(i =0; i <jnn ; i++) { if(nf) { cout<<" "; } else { nf = 1; } cout<<nn[i]; } for(i =0; i <jz; i++) { if(nf) { cout<<" "; } else { nf = 1; } cout<<dz[i]; } for(i =0; i <ju; i++) { if(nf) { cout<<" "; } else { nf = 1; } cout<<du[i]; } } else { cout<<"NIE"; } return 0; } |