#include <iostream> #include <stack> #include <vector> #include <queue> #include <set> #include <cmath> #include <fstream> #include <algorithm> #define endl "\n" using namespace std; const int MAX=100005; struct monster { int dmg,elix,numb; }; vector<monster>plusik,minusik; vector<int> ans; int all_dmg=0,mindmg=MAX,minelix=-MAX,minumb; //ifstream plik("dane.in"); bool cmplus (monster a, monster b) { if (a.dmg<b.dmg) return 1; if (a.dmg==b.dmg) return a.elix>b.elix; return false; } bool cmpminus (monster a, monster b) { if (a.elix==b.elix && a.elix==minelix) return a.dmg>b.dmg; return a.dmg<b.dmg; } void read(int n) { int p1,p2; for (int i=1;i<=n;i++) { cin>>p1>>p2; monster pom={p1,p2-p1,i}; if (p2<p1) { minusik.push_back(pom); if (minelix<=p2-p1) if (mindmg>p1) { mindmg=p1; minelix=p2-p1; minumb=i; } all_dmg-=p2-p1; } else plusik.push_back(pom); } } void solve(int n, int h) { bool p=1; sort (minusik.begin(), minusik.end(),cmpminus); sort (plusik.begin(), plusik.end(),cmplus); for (int i=0;i<plusik.size();i++) { h-=plusik[i].dmg; if (h<=0) { cout<<"NIE\n"; p=0; break; } h+=plusik[i].elix+plusik[i].dmg; ans.push_back(plusik[i].numb); } int j=minusik.size()-1,k=0; all_dmg-=minelix; all_dmg-=mindmg; //cout<<minelix<<" "<<mindmg<<" "<<all_dmg<<" "<<minumb<<" hehe "<<endl; for (int i=0;i<minusik.size()-1;i++) { if (minusik[k].numb==minumb) k++; if (minusik[j].numb==minumb) j--; if (h>minusik[j].dmg && all_dmg<h) { // cout<<"dmg: "<<all_dmg<<" "<<h<<" "<<minusik[j].elix<<" "<<minusik[j].dmg<<endl; h+=minusik[j].elix; all_dmg+=minusik[j].elix; ans.push_back(minusik[j].numb); // cout<<"dmg: "<<all_dmg<<" "<<h<<endl; j--; } else if (h>minusik[k].dmg && all_dmg<h) { // cout<<"admg: "<<all_dmg<<" "<<h<<" "<<minusik[j].elix<<endl; h+=minusik[k].elix; all_dmg+=minusik[k].elix; ans.push_back(minusik[k].numb); // cout<<"admg: "<<all_dmg<<" "<<h<<endl; k++; } else { cout<<"NIE\n"; p=0; break; } } h-=mindmg; ans.push_back(minumb); if (p) { if(h<=0) cout<<"NIE\n"; else { cout<<"TAK\n"; for (int i=0;i<ans.size();i++) cout<<ans[i]<<" "; cout<<"\n"; } } } int main() { int n,h; ios_base::sync_with_stdio(0); cin>>n>>h; read(n); solve(n,h); //for (int i=0;i<minusik.size();i++) // cout<<minusik[i].dmg<<" "<<minusik[i].elix<<" "<<minusik[i].numb<<endl; //plik.close(); 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 | #include <iostream> #include <stack> #include <vector> #include <queue> #include <set> #include <cmath> #include <fstream> #include <algorithm> #define endl "\n" using namespace std; const int MAX=100005; struct monster { int dmg,elix,numb; }; vector<monster>plusik,minusik; vector<int> ans; int all_dmg=0,mindmg=MAX,minelix=-MAX,minumb; //ifstream plik("dane.in"); bool cmplus (monster a, monster b) { if (a.dmg<b.dmg) return 1; if (a.dmg==b.dmg) return a.elix>b.elix; return false; } bool cmpminus (monster a, monster b) { if (a.elix==b.elix && a.elix==minelix) return a.dmg>b.dmg; return a.dmg<b.dmg; } void read(int n) { int p1,p2; for (int i=1;i<=n;i++) { cin>>p1>>p2; monster pom={p1,p2-p1,i}; if (p2<p1) { minusik.push_back(pom); if (minelix<=p2-p1) if (mindmg>p1) { mindmg=p1; minelix=p2-p1; minumb=i; } all_dmg-=p2-p1; } else plusik.push_back(pom); } } void solve(int n, int h) { bool p=1; sort (minusik.begin(), minusik.end(),cmpminus); sort (plusik.begin(), plusik.end(),cmplus); for (int i=0;i<plusik.size();i++) { h-=plusik[i].dmg; if (h<=0) { cout<<"NIE\n"; p=0; break; } h+=plusik[i].elix+plusik[i].dmg; ans.push_back(plusik[i].numb); } int j=minusik.size()-1,k=0; all_dmg-=minelix; all_dmg-=mindmg; //cout<<minelix<<" "<<mindmg<<" "<<all_dmg<<" "<<minumb<<" hehe "<<endl; for (int i=0;i<minusik.size()-1;i++) { if (minusik[k].numb==minumb) k++; if (minusik[j].numb==minumb) j--; if (h>minusik[j].dmg && all_dmg<h) { // cout<<"dmg: "<<all_dmg<<" "<<h<<" "<<minusik[j].elix<<" "<<minusik[j].dmg<<endl; h+=minusik[j].elix; all_dmg+=minusik[j].elix; ans.push_back(minusik[j].numb); // cout<<"dmg: "<<all_dmg<<" "<<h<<endl; j--; } else if (h>minusik[k].dmg && all_dmg<h) { // cout<<"admg: "<<all_dmg<<" "<<h<<" "<<minusik[j].elix<<endl; h+=minusik[k].elix; all_dmg+=minusik[k].elix; ans.push_back(minusik[k].numb); // cout<<"admg: "<<all_dmg<<" "<<h<<endl; k++; } else { cout<<"NIE\n"; p=0; break; } } h-=mindmg; ans.push_back(minumb); if (p) { if(h<=0) cout<<"NIE\n"; else { cout<<"TAK\n"; for (int i=0;i<ans.size();i++) cout<<ans[i]<<" "; cout<<"\n"; } } } int main() { int n,h; ios_base::sync_with_stdio(0); cin>>n>>h; read(n); solve(n,h); //for (int i=0;i<minusik.size();i++) // cout<<minusik[i].dmg<<" "<<minusik[i].elix<<" "<<minusik[i].numb<<endl; //plik.close(); return 0; } |