#include <iostream> #include <set> #include <iomanip> #include <map> #include <string> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define REP(i,n) for(int i = 0 ; i < (n) ; i++) #define FOR(i,b,e) for(int i = (b) ; i <= (e) ; i++) #define FORD(i,b,e) for(int i = (b) ; i >= (e) ; i--) #define VAR(i,n) typeof(n) i = (n) #define FOREACH(i,c)for(VAR(i,(c).begin());i!=(c).end();i++) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) (int)(c).size() #define PB push_back #define st first #define nd second const int inf=1000000001; const double pi=3.141592653589793; typedef vector<int> VI; typedef long long LL; typedef long double LD; const int maxn=100001; int d[maxn],a[maxn]; int m=131072; int t[300000]; int kol[maxn]; int ind[300000]; vector<pair<int,int> > wek[maxn]; void Insert(int x, int val) { int v = m + x; t[v] = val; ind[v]=x; while (v != 1) { v /= 2; if(t[2*v]>t[2*v+1]) { t[v]=t[2*v]; ind[v]=ind[2*v]; } else { t[v]=t[2*v+1]; ind[v]=ind[2*v+1]; } } } int query(int a, int b) { if(b<a)return -1; int va = m + a, vb = m + b; int maks,in; if(t[va]>t[vb]) { maks=t[va]; in=ind[va]; } else { maks=t[vb]; in=ind[vb]; } while (va / 2 != vb / 2) { if (va % 2 == 0&&t[va+1]>maks) { maks=t[va+1]; in=ind[va+1]; } if (vb % 2 == 1&&t[vb-1]>maks) { maks=t[vb - 1]; in=ind[vb-1]; } va /= 2; vb /= 2; } return in; } int main() { ios::sync_with_stdio(0); int n,z; cin >> n >> z; REP(i,n)cin >> d[i] >> a[i]; REP(i,n) { wek[d[i]].PB(pair<int,int>(a[i],i)); } REP(i,maxn)sort(ALL(wek[i])); REP(i,maxn) { if(!wek[i].empty()) { Insert(i,wek[i].back().st); } else Insert(i,-inf); } REP(i,n) { int in=query(0,min(z-1,maxn-1)); if(in==-1) { cout << "NIE\n"; return 0; } else { z+=wek[in].back().st; kol[i]=wek[in].back().nd; wek[in].pop_back(); if(wek[in].empty())Insert(in,-inf); else Insert(in,wek[in].back().st); } } cout << "TAK\n"; REP(i,n)cout << kol[i]+1 << " "; }
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 | #include <iostream> #include <set> #include <iomanip> #include <map> #include <string> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define REP(i,n) for(int i = 0 ; i < (n) ; i++) #define FOR(i,b,e) for(int i = (b) ; i <= (e) ; i++) #define FORD(i,b,e) for(int i = (b) ; i >= (e) ; i--) #define VAR(i,n) typeof(n) i = (n) #define FOREACH(i,c)for(VAR(i,(c).begin());i!=(c).end();i++) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) (int)(c).size() #define PB push_back #define st first #define nd second const int inf=1000000001; const double pi=3.141592653589793; typedef vector<int> VI; typedef long long LL; typedef long double LD; const int maxn=100001; int d[maxn],a[maxn]; int m=131072; int t[300000]; int kol[maxn]; int ind[300000]; vector<pair<int,int> > wek[maxn]; void Insert(int x, int val) { int v = m + x; t[v] = val; ind[v]=x; while (v != 1) { v /= 2; if(t[2*v]>t[2*v+1]) { t[v]=t[2*v]; ind[v]=ind[2*v]; } else { t[v]=t[2*v+1]; ind[v]=ind[2*v+1]; } } } int query(int a, int b) { if(b<a)return -1; int va = m + a, vb = m + b; int maks,in; if(t[va]>t[vb]) { maks=t[va]; in=ind[va]; } else { maks=t[vb]; in=ind[vb]; } while (va / 2 != vb / 2) { if (va % 2 == 0&&t[va+1]>maks) { maks=t[va+1]; in=ind[va+1]; } if (vb % 2 == 1&&t[vb-1]>maks) { maks=t[vb - 1]; in=ind[vb-1]; } va /= 2; vb /= 2; } return in; } int main() { ios::sync_with_stdio(0); int n,z; cin >> n >> z; REP(i,n)cin >> d[i] >> a[i]; REP(i,n) { wek[d[i]].PB(pair<int,int>(a[i],i)); } REP(i,maxn)sort(ALL(wek[i])); REP(i,maxn) { if(!wek[i].empty()) { Insert(i,wek[i].back().st); } else Insert(i,-inf); } REP(i,n) { int in=query(0,min(z-1,maxn-1)); if(in==-1) { cout << "NIE\n"; return 0; } else { z+=wek[in].back().st; kol[i]=wek[in].back().nd; wek[in].pop_back(); if(wek[in].empty())Insert(in,-inf); else Insert(in,wek[in].back().st); } } cout << "TAK\n"; REP(i,n)cout << kol[i]+1 << " "; } |