#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class potwor
{public:
int d;
int h;
int numer;
potwor (int D, int H, int num): d(D),h(H),numer(num) {}
bool operator < (const potwor & other) const { return d<other.d ;}
};
bool test(const vector<potwor>potw, int zycko)
{
for (int i=0;i<potw.size();i++)
{
zycko -= potw[i].d;
if (zycko<=0) return false;
zycko += potw[i].h;
}
return true;
}
int main()
{
cin.sync_with_stdio(false);
vector<potwor> dodatnie, ujemne;
int zycko, n;
int dochod=0, strata=0;
cin>>n>>zycko;
for(int i=1;i<=n;i++)
{
int d,h;
cin>>d>>h;
if (h-d>=0)
{
dodatnie.emplace_back(d,h,i);
dochod+=h-d;
}else
{
ujemne.emplace_back(h,d,i);
strata+=d-h;
}
}
sort(dodatnie.begin(),dodatnie.end());
sort(ujemne.begin(), ujemne.end());
if ( test(dodatnie,zycko) && test(ujemne, zycko+dochod-strata) )
{
cout<<"TAK"<<endl;
for (int i=0;i<dodatnie.size();i++)
cout<<dodatnie[i].numer<<" ";
for (int i=ujemne.size()-1;i>=0;i--)
cout<<ujemne[i].numer<<" ";
}
else
cout<<"NIE"<<endl;
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; class potwor {public: int d; int h; int numer; potwor (int D, int H, int num): d(D),h(H),numer(num) {} bool operator < (const potwor & other) const { return d<other.d ;} }; bool test(const vector<potwor>potw, int zycko) { for (int i=0;i<potw.size();i++) { zycko -= potw[i].d; if (zycko<=0) return false; zycko += potw[i].h; } return true; } int main() { cin.sync_with_stdio(false); vector<potwor> dodatnie, ujemne; int zycko, n; int dochod=0, strata=0; cin>>n>>zycko; for(int i=1;i<=n;i++) { int d,h; cin>>d>>h; if (h-d>=0) { dodatnie.emplace_back(d,h,i); dochod+=h-d; }else { ujemne.emplace_back(h,d,i); strata+=d-h; } } sort(dodatnie.begin(),dodatnie.end()); sort(ujemne.begin(), ujemne.end()); if ( test(dodatnie,zycko) && test(ujemne, zycko+dochod-strata) ) { cout<<"TAK"<<endl; for (int i=0;i<dodatnie.size();i++) cout<<dodatnie[i].numer<<" "; for (int i=ujemne.size()-1;i>=0;i--) cout<<ujemne[i].numer<<" "; } else cout<<"NIE"<<endl; return 0; } |
English