#include <iostream> #include <vector> #include <algorithm> using namespace std; struct enemy { int down, up, nr; enemy(int a, int b, int c): down(a), up(b), nr(c){} }; bool pordown (enemy a, enemy b){return a.down<b.down;} bool porup (enemy a, enemy b){return a.up<b.up;} bool pordiff(enemy a, enemy b){return a.up-a.down<b.up-b.down;} void wypisz(); int check(vector<enemy> t, int hp); vector<enemy>good; vector<enemy>bad; int main() { ios_base::sync_with_stdio(0); int ile, hp; cin>>ile>>hp; int tmpa, tmpb; for(int n=0; n<ile; n++) { cin>>tmpa>>tmpb; if(tmpa>tmpb) { bad.push_back(enemy(tmpa, tmpb, n+1)); } else { good.push_back(enemy(tmpa, tmpb, n+1)); } } sort(good.begin(), good.end(), pordown); hp=check(good, hp); if(hp>0) { sort(bad.begin(), bad.end(), pordown); if(check(bad, hp)>0) {wypisz(); return 0;} reverse(bad.begin(), bad.end()); if(check(bad, hp)>0) {wypisz(); return 0;} sort(bad.begin(), bad.end(), porup); if(check(bad, hp)>0) {wypisz(); return 0;} reverse(bad.begin(), bad.end()); if(check(bad, hp)>0) {wypisz(); return 0;} sort(bad.begin(), bad.end(), pordiff); if(check(bad, hp)>0) {wypisz(); return 0;} reverse(bad.begin(), bad.end()); if(check(bad, hp)>0) {wypisz(); return 0;} //jeszcze jakies pomysly? } cout<<"NIE"<<endl; return 0; } int check(vector<enemy> t, int hp) { for(int n=0; n<t.size(); n++) { hp-=t[n].down; if(hp<=0) return 0; hp+=t[n].up; } return hp; } void wypisz() { cout<<"TAK"<<endl; for(int n=0; n<good.size(); n++) { cout<<good[n].nr<<" "; } for(int n=0; n<bad.size(); n++) { cout<<bad[n].nr<<" "; } cout<<endl; }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct enemy { int down, up, nr; enemy(int a, int b, int c): down(a), up(b), nr(c){} }; bool pordown (enemy a, enemy b){return a.down<b.down;} bool porup (enemy a, enemy b){return a.up<b.up;} bool pordiff(enemy a, enemy b){return a.up-a.down<b.up-b.down;} void wypisz(); int check(vector<enemy> t, int hp); vector<enemy>good; vector<enemy>bad; int main() { ios_base::sync_with_stdio(0); int ile, hp; cin>>ile>>hp; int tmpa, tmpb; for(int n=0; n<ile; n++) { cin>>tmpa>>tmpb; if(tmpa>tmpb) { bad.push_back(enemy(tmpa, tmpb, n+1)); } else { good.push_back(enemy(tmpa, tmpb, n+1)); } } sort(good.begin(), good.end(), pordown); hp=check(good, hp); if(hp>0) { sort(bad.begin(), bad.end(), pordown); if(check(bad, hp)>0) {wypisz(); return 0;} reverse(bad.begin(), bad.end()); if(check(bad, hp)>0) {wypisz(); return 0;} sort(bad.begin(), bad.end(), porup); if(check(bad, hp)>0) {wypisz(); return 0;} reverse(bad.begin(), bad.end()); if(check(bad, hp)>0) {wypisz(); return 0;} sort(bad.begin(), bad.end(), pordiff); if(check(bad, hp)>0) {wypisz(); return 0;} reverse(bad.begin(), bad.end()); if(check(bad, hp)>0) {wypisz(); return 0;} //jeszcze jakies pomysly? } cout<<"NIE"<<endl; return 0; } int check(vector<enemy> t, int hp) { for(int n=0; n<t.size(); n++) { hp-=t[n].down; if(hp<=0) return 0; hp+=t[n].up; } return hp; } void wypisz() { cout<<"TAK"<<endl; for(int n=0; n<good.size(); n++) { cout<<good[n].nr<<" "; } for(int n=0; n<bad.size(); n++) { cout<<bad[n].nr<<" "; } cout<<endl; } |