#include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; struct alien { int first; int second; int poz; }; long long n,m,mm; vector<alien> v1,v2,v3; queue <int> q,q2,q3; bool cmp(alien a, alien b) { if (a.first <b.first) return true; else if (a.first == b.first) return a.second>b.second; else { return false; } } bool cmp1 (alien a, alien b) { if (a.second -a.first > b.second-b.first) return true; else if (a.second -a.first < b.second-b.first) return false; else { return a.first>b.first; } } bool cmp2 (alien a,alien b) { return a.first >b.first; } bool cmp3 (alien a, alien b) { if (a.first <b.first) return false; else if (a.first == b.first) return a.second<b.second; else { return true; } } bool spr () { sort(v2.begin(),v2.end(),cmp3 ); for (int i=0;i<v2.size();i++) { if (v2[i].first <m) { m=m + v2[i].second - v2[i].first; q2.push(v2[i].poz); } else { return 1; } } return 0; } int main() { ios_base::sync_with_stdio(0); cin >>n>>m; for (int i=0;i<n;i++) { alien x; cin >>x.first>>x.second; x.poz=i+1; if (x.first<=x.second) v1.push_back(x); else if (x.second==0) v3.push_back(x); else v2.push_back(x); } sort (v1.begin(),v1.end(),cmp); for (int i=0;i<v1.size();i++) { if (v1[i].first < m) { m=m+v1[i].second -v1[i].first; q.push(v1[i].poz); } else { cout <<"NIE"<<endl; return 0; } } sort(v2.begin(),v2.end(),cmp1 ); mm=m; for (int i=0;i<v2.size();i++) { if (v2[i].first <m) { m=m + v2[i].second - v2[i].first; q2.push(v2[i].poz); } else { while (q2.empty()==false) q2.pop(); m=mm; if(spr()) { cout <<"NIE"<<endl; return 0; } } } sort (v3.begin(),v3.end(),cmp2 ); for (int i=0;i<v3.size();i++) { if (v3[i].first < m) { m=m-v3[i].first; q3.push(v3[i].poz); } else { cout <<"NIE"<<endl; return 0; } } cout <<"TAK"<<endl; while (q.empty()==false) { cout <<q.front()<<" "; q.pop(); } while (q2.empty()==false) { cout <<q2.front()<<" "; q2.pop(); } while (q3.empty()==false) { cout <<q3.front()<<" "; q3.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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; struct alien { int first; int second; int poz; }; long long n,m,mm; vector<alien> v1,v2,v3; queue <int> q,q2,q3; bool cmp(alien a, alien b) { if (a.first <b.first) return true; else if (a.first == b.first) return a.second>b.second; else { return false; } } bool cmp1 (alien a, alien b) { if (a.second -a.first > b.second-b.first) return true; else if (a.second -a.first < b.second-b.first) return false; else { return a.first>b.first; } } bool cmp2 (alien a,alien b) { return a.first >b.first; } bool cmp3 (alien a, alien b) { if (a.first <b.first) return false; else if (a.first == b.first) return a.second<b.second; else { return true; } } bool spr () { sort(v2.begin(),v2.end(),cmp3 ); for (int i=0;i<v2.size();i++) { if (v2[i].first <m) { m=m + v2[i].second - v2[i].first; q2.push(v2[i].poz); } else { return 1; } } return 0; } int main() { ios_base::sync_with_stdio(0); cin >>n>>m; for (int i=0;i<n;i++) { alien x; cin >>x.first>>x.second; x.poz=i+1; if (x.first<=x.second) v1.push_back(x); else if (x.second==0) v3.push_back(x); else v2.push_back(x); } sort (v1.begin(),v1.end(),cmp); for (int i=0;i<v1.size();i++) { if (v1[i].first < m) { m=m+v1[i].second -v1[i].first; q.push(v1[i].poz); } else { cout <<"NIE"<<endl; return 0; } } sort(v2.begin(),v2.end(),cmp1 ); mm=m; for (int i=0;i<v2.size();i++) { if (v2[i].first <m) { m=m + v2[i].second - v2[i].first; q2.push(v2[i].poz); } else { while (q2.empty()==false) q2.pop(); m=mm; if(spr()) { cout <<"NIE"<<endl; return 0; } } } sort (v3.begin(),v3.end(),cmp2 ); for (int i=0;i<v3.size();i++) { if (v3[i].first < m) { m=m-v3[i].first; q3.push(v3[i].poz); } else { cout <<"NIE"<<endl; return 0; } } cout <<"TAK"<<endl; while (q.empty()==false) { cout <<q.front()<<" "; q.pop(); } while (q2.empty()==false) { cout <<q2.front()<<" "; q2.pop(); } while (q3.empty()==false) { cout <<q3.front()<<" "; q3.pop(); } return 0; } |