#include <cstdio> #include <algorithm> #include <queue> using namespace std; const int MAXN = 100009; struct creature { int id; int kill, heal; } t[MAXN]; queue<int> q; int n, life; int start; int m; bool used[MAXN]; creature pom[MAXN]; bool cmp1(creature a, creature b) { if(a.heal - a.kill == b.heal - b.kill) return a.kill > b.kill; return a.heal - a.kill > b.heal - b.kill; } bool cmp2(creature a, creature b) { return a.kill > b.kill; } bool cmp3(creature a, creature b) { return a.kill < b.kill; } void read() { scanf("%d%d", &n, &life); for(int i = 0; i < n; i++) { scanf("%d%d", &t[i].kill, &t[i].heal); t[i].id = i; } } bool tryChange() { for(int i = 0; i < m; i++) { start -= pom[i].kill; if(start <= 0) return 0; start += pom[i].heal; } printf("TAK\n"); for(int i = 0; i < n - m; i++) printf("%d ", t[i].id + 1); for(int i = 0; i < m; i++) printf("%d ", pom[i].id + 1); return 1; } int main() { read(); sort(t, t + n, cmp1); for(int i = 0; i < n; i++) if(t[i].heal - t[i].kill < 0) { for(int j = i; j < n; j++) pom[m++] = t[j]; break; } sort(pom, pom + m, cmp2); sort(t, t + n - m, cmp3); //printf("m = %d, n-m = %d\n", m, n - m); for(int i = 0; i < n - m; i++) { life -= t[i].kill; if(life <= 0) { printf("NIE"); return 0; } life += t[i].heal; q.push(t[i].id); used[t[i].id] = 1; //printf(" %d\n", t[i].id); } start = life; t[n].id = pom[m].id = MAXN; int k = n - m, l = 0; for(int i = 0; i < m; i++) { while(used[t[k].id] == 1) k++; while(used[pom[l].id] == 1) l++; //printf("life: %d\n", life); //printf("pom[l].kill %d .id %d\n", pom[l].kill, pom[l].id); //printf("k: %d\n", k); if(life <= t[k].kill) { if(!tryChange()) printf("NIE"); return 0; } if(life - t[k].kill + t[k].heal <= pom[l].kill) { //printf(" %d [pom]\n", pom[l].id); life -= pom[l].kill; if(life <= 0) { if(!tryChange()) printf("NIE"); return 0; } life += pom[l].heal; q.push(pom[l].id); used[pom[l].id] = 1; } else { //printf(" %d [t]\n", t[k].id); life -= t[k].kill; if(life <= 0) { if(!tryChange()) printf("NIE"); return 0; } life += t[k].heal; q.push(t[k].id); used[t[k].id] = 1; } } printf("TAK\n"); while(!q.empty()) { printf("%d ", q.front() + 1); q.pop(); } 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 | #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int MAXN = 100009; struct creature { int id; int kill, heal; } t[MAXN]; queue<int> q; int n, life; int start; int m; bool used[MAXN]; creature pom[MAXN]; bool cmp1(creature a, creature b) { if(a.heal - a.kill == b.heal - b.kill) return a.kill > b.kill; return a.heal - a.kill > b.heal - b.kill; } bool cmp2(creature a, creature b) { return a.kill > b.kill; } bool cmp3(creature a, creature b) { return a.kill < b.kill; } void read() { scanf("%d%d", &n, &life); for(int i = 0; i < n; i++) { scanf("%d%d", &t[i].kill, &t[i].heal); t[i].id = i; } } bool tryChange() { for(int i = 0; i < m; i++) { start -= pom[i].kill; if(start <= 0) return 0; start += pom[i].heal; } printf("TAK\n"); for(int i = 0; i < n - m; i++) printf("%d ", t[i].id + 1); for(int i = 0; i < m; i++) printf("%d ", pom[i].id + 1); return 1; } int main() { read(); sort(t, t + n, cmp1); for(int i = 0; i < n; i++) if(t[i].heal - t[i].kill < 0) { for(int j = i; j < n; j++) pom[m++] = t[j]; break; } sort(pom, pom + m, cmp2); sort(t, t + n - m, cmp3); //printf("m = %d, n-m = %d\n", m, n - m); for(int i = 0; i < n - m; i++) { life -= t[i].kill; if(life <= 0) { printf("NIE"); return 0; } life += t[i].heal; q.push(t[i].id); used[t[i].id] = 1; //printf(" %d\n", t[i].id); } start = life; t[n].id = pom[m].id = MAXN; int k = n - m, l = 0; for(int i = 0; i < m; i++) { while(used[t[k].id] == 1) k++; while(used[pom[l].id] == 1) l++; //printf("life: %d\n", life); //printf("pom[l].kill %d .id %d\n", pom[l].kill, pom[l].id); //printf("k: %d\n", k); if(life <= t[k].kill) { if(!tryChange()) printf("NIE"); return 0; } if(life - t[k].kill + t[k].heal <= pom[l].kill) { //printf(" %d [pom]\n", pom[l].id); life -= pom[l].kill; if(life <= 0) { if(!tryChange()) printf("NIE"); return 0; } life += pom[l].heal; q.push(pom[l].id); used[pom[l].id] = 1; } else { //printf(" %d [t]\n", t[k].id); life -= t[k].kill; if(life <= 0) { if(!tryChange()) printf("NIE"); return 0; } life += t[k].heal; q.push(t[k].id); used[t[k].id] = 1; } } printf("TAK\n"); while(!q.empty()) { printf("%d ", q.front() + 1); q.pop(); } return 0; } |