#include<iostream> #include<cstdio> #include<string> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<algorithm> #include<utility> #include<tuple> using namespace std; #define FOR(I,A,B) for(int I=(A);I<=(B);I++) #define REP(I,N) for(int I=0;I<(N);I++) #define ALL(X) (X).begin(),(X).end() #define PB push_back #define MP make_pair #define f first #define s second typedef vector<int> VI; typedef pair<int,int> PII; typedef long long ll; typedef vector<string> VS; vector < tuple <int,int,int> > positive,negative; priority_queue < PII > Q; VI res,tmp; int main(){ ios_base::sync_with_stdio(0); int n; ll z; cin >> n >> z; FOR(i,1,n){ int a,b; cin >> a >> b; if(b>a) positive.PB(make_tuple(a,b-a,i)); else negative.PB(make_tuple(b,a-b,i)); } sort(ALL(positive)); sort(ALL(negative)); for(auto monster: positive) if(get<0>(monster) >= z){ cout << "NIE\n"; return 0; } else{ z += get<1>(monster); res.PB(get<2>(monster)); } for(auto monster: negative){ z -= get<1>(monster); if(z <= 0){ cout << "NIE\n"; return 0; } } int i = 0; for(auto monster: negative){ if(z > get<0>(monster)) Q.push(MP(get<1>(monster),get<2>(monster))); else break; ++i; } while(!Q.empty()){ PII p = Q.top(); Q.pop(); z += p.f; tmp.PB(p.s); while(i < negative.size() && z > get<0>(negative[i])){ Q.push(MP(get<1>(negative[i]),get<2>(negative[i]))); ++i; } } if(i != negative.size()){ cout << "NIE\n"; return 0; } reverse(ALL(tmp)); res.insert(res.end(), tmp.begin(), tmp.end()); cout << "TAK\n"; REP(i,res.size()) cout << res[i] << " "; cout << "\n"; 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 | #include<iostream> #include<cstdio> #include<string> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<algorithm> #include<utility> #include<tuple> using namespace std; #define FOR(I,A,B) for(int I=(A);I<=(B);I++) #define REP(I,N) for(int I=0;I<(N);I++) #define ALL(X) (X).begin(),(X).end() #define PB push_back #define MP make_pair #define f first #define s second typedef vector<int> VI; typedef pair<int,int> PII; typedef long long ll; typedef vector<string> VS; vector < tuple <int,int,int> > positive,negative; priority_queue < PII > Q; VI res,tmp; int main(){ ios_base::sync_with_stdio(0); int n; ll z; cin >> n >> z; FOR(i,1,n){ int a,b; cin >> a >> b; if(b>a) positive.PB(make_tuple(a,b-a,i)); else negative.PB(make_tuple(b,a-b,i)); } sort(ALL(positive)); sort(ALL(negative)); for(auto monster: positive) if(get<0>(monster) >= z){ cout << "NIE\n"; return 0; } else{ z += get<1>(monster); res.PB(get<2>(monster)); } for(auto monster: negative){ z -= get<1>(monster); if(z <= 0){ cout << "NIE\n"; return 0; } } int i = 0; for(auto monster: negative){ if(z > get<0>(monster)) Q.push(MP(get<1>(monster),get<2>(monster))); else break; ++i; } while(!Q.empty()){ PII p = Q.top(); Q.pop(); z += p.f; tmp.PB(p.s); while(i < negative.size() && z > get<0>(negative[i])){ Q.push(MP(get<1>(negative[i]),get<2>(negative[i]))); ++i; } } if(i != negative.size()){ cout << "NIE\n"; return 0; } reverse(ALL(tmp)); res.insert(res.end(), tmp.begin(), tmp.end()); cout << "TAK\n"; REP(i,res.size()) cout << res[i] << " "; cout << "\n"; return 0; } |