#include<cstdio> #include<algorithm> #include<queue> #include<vector> using namespace std; #define ft first #define sc second #define mp make_pair vector<int> ans; struct node { int m, p; priority_queue<pair<int,int> > q; }; int inf = 1000000007; int i0 = 1<<17; ///////////////////////////////////////// struct tree { node v[1<<18]; ///////////////////////////////////////// int find(int p) { int m = -inf; int i = p+i0; int pos = -inf; //printf("%d ",i); //puts(""); while(i > 1) { if (i%2 == 1 and v[i-1].m > m) m = v[i-1].m, pos = v[i-1].p; i /= 2; //printf("%d ",i); } //puts(""); return pos; } void del(int p) { int i = p; v[i].q.pop(); if (v[i].q.empty()) v[i].m = -inf; else v[i].m = v[i].q.top().ft; i /= 2; while(i) { if (v[2*i].m > v[2*i+1].m) v[i].m = v[2*i].m, v[i].p = v[2*i].p; else v[i].m = v[2*i+1].m, v[i].p = v[2*i+1].p; i /= 2; } } void make() { for (int i = i0; i < 2*i0; i++) { v[i].p = i; if (v[i].q.empty()) v[i].m = -inf; else v[i].m = v[i].q.top().ft; } for (int i = i0-1; i >= 1; i--) { if (v[2*i].m > v[2*i+1].m) v[i].m = v[2*i].m, v[i].p = v[2*i].p; else v[i].m = v[2*i+1].m, v[i].p = v[2*i+1].p; } } int find2(int p, int x, int beg, int end, int i) { //printf("%d ",i); if (i >= i0 and v[i].m > x) return i; else if (i >= i0) return 0; int mid = (beg+end)/2; int a = -1; //printf("%d ",mid); if (p > mid+1 and v[2*i+1].m > x) { a = find2(p,x,mid+1,end,2*i+1); } if (a == -1 and v[2*i].m > x) return find2(p,x,beg,mid,2*i); else return a; } int find2(int p) { return find2(p,-inf,0,i0-1,1); } }; tree T; int main() { int n; long long z; scanf("%d%lld",&n,&z); for (int i = 1; i <= n; i++) { int d,a; scanf("%d%d",&d,&a); T.v[d+i0].q.push(mp(a-d,i)); } T.make(); //for (int i = 0; i <= 10; i++) printf("%d ",T.v[i0+i].m); //puts(""); for(int i = 1; i <= n; i++) { //printf("%lld\n",z); //if (i%100 == 0) printf("%d\n",i); int x = T.find(min(z,100003ll)); if (x == -inf) {puts("NIE"); return 0;} if (T.v[x].m == -inf) {puts("NIE"); return 0;} //printf("%d\n",x); long long z1 = z + T.v[x].m; int y = T.find2(min(z,100003ll)); //puts(""); int w2 = T.find2(y-i0)-i0; //printf("%d %d\n",x-i0,y-i0); long long z2 = z + T.v[y].m; int w1 = y-i0; //printf("%lld %lld\n",z1,z2); //printf("%d %d\n",w1,w2); if (z1-w1 > z2-w2 or x == y) { //puts("N"); z = z1; ans.push_back(T.v[x].q.top().sc); T.del(x); } else { //puts("T"); z = z2; ans.push_back(T.v[y].q.top().sc); T.del(y); } //z += T.v[x].m; //z -= x-i0; //printf("%d %d\n",T.v[i0].p,T.v[i0+1].p); //puts(""); } puts("TAK"); //printf("\n%d %d\n",n,ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d ",ans[i]); //system("pause"); 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 | #include<cstdio> #include<algorithm> #include<queue> #include<vector> using namespace std; #define ft first #define sc second #define mp make_pair vector<int> ans; struct node { int m, p; priority_queue<pair<int,int> > q; }; int inf = 1000000007; int i0 = 1<<17; ///////////////////////////////////////// struct tree { node v[1<<18]; ///////////////////////////////////////// int find(int p) { int m = -inf; int i = p+i0; int pos = -inf; //printf("%d ",i); //puts(""); while(i > 1) { if (i%2 == 1 and v[i-1].m > m) m = v[i-1].m, pos = v[i-1].p; i /= 2; //printf("%d ",i); } //puts(""); return pos; } void del(int p) { int i = p; v[i].q.pop(); if (v[i].q.empty()) v[i].m = -inf; else v[i].m = v[i].q.top().ft; i /= 2; while(i) { if (v[2*i].m > v[2*i+1].m) v[i].m = v[2*i].m, v[i].p = v[2*i].p; else v[i].m = v[2*i+1].m, v[i].p = v[2*i+1].p; i /= 2; } } void make() { for (int i = i0; i < 2*i0; i++) { v[i].p = i; if (v[i].q.empty()) v[i].m = -inf; else v[i].m = v[i].q.top().ft; } for (int i = i0-1; i >= 1; i--) { if (v[2*i].m > v[2*i+1].m) v[i].m = v[2*i].m, v[i].p = v[2*i].p; else v[i].m = v[2*i+1].m, v[i].p = v[2*i+1].p; } } int find2(int p, int x, int beg, int end, int i) { //printf("%d ",i); if (i >= i0 and v[i].m > x) return i; else if (i >= i0) return 0; int mid = (beg+end)/2; int a = -1; //printf("%d ",mid); if (p > mid+1 and v[2*i+1].m > x) { a = find2(p,x,mid+1,end,2*i+1); } if (a == -1 and v[2*i].m > x) return find2(p,x,beg,mid,2*i); else return a; } int find2(int p) { return find2(p,-inf,0,i0-1,1); } }; tree T; int main() { int n; long long z; scanf("%d%lld",&n,&z); for (int i = 1; i <= n; i++) { int d,a; scanf("%d%d",&d,&a); T.v[d+i0].q.push(mp(a-d,i)); } T.make(); //for (int i = 0; i <= 10; i++) printf("%d ",T.v[i0+i].m); //puts(""); for(int i = 1; i <= n; i++) { //printf("%lld\n",z); //if (i%100 == 0) printf("%d\n",i); int x = T.find(min(z,100003ll)); if (x == -inf) {puts("NIE"); return 0;} if (T.v[x].m == -inf) {puts("NIE"); return 0;} //printf("%d\n",x); long long z1 = z + T.v[x].m; int y = T.find2(min(z,100003ll)); //puts(""); int w2 = T.find2(y-i0)-i0; //printf("%d %d\n",x-i0,y-i0); long long z2 = z + T.v[y].m; int w1 = y-i0; //printf("%lld %lld\n",z1,z2); //printf("%d %d\n",w1,w2); if (z1-w1 > z2-w2 or x == y) { //puts("N"); z = z1; ans.push_back(T.v[x].q.top().sc); T.del(x); } else { //puts("T"); z = z2; ans.push_back(T.v[y].q.top().sc); T.del(y); } //z += T.v[x].m; //z -= x-i0; //printf("%d %d\n",T.v[i0].p,T.v[i0+1].p); //puts(""); } puts("TAK"); //printf("\n%d %d\n",n,ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d ",ans[i]); //system("pause"); return 0; } |