#include <cstdio> #include <vector> #include <algorithm> using namespace std; class triple { public: int f,s,n; }; bool cmp(triple i,triple j) { int di = (i.f - i.s < 0)?-1:1, dj = (j.f - j.s < 0)?-1:1; if (di * dj < 0) return di < dj; else if (di < 0) return i.f < j.f; else return i.f > j.f; } int main() { int n,N,Z,z; triple p; vector< triple > S; vector< triple >::const_iterator it; scanf("%d %d",&N,&Z); for (n = 0; n < N; n++) { scanf("%d %d",&(p.f),&(p.s)); p.n = n+1; S.push_back(p); } // for (it = S.begin(); it != S.end(); ++it) printf("%d %d %d\n",it->f,it->s,it->n); sort(S.begin(),S.end(),cmp); // for (it = S.begin(); it != S.end(); ++it) printf("%d %d %d\n",it->f,it->s,it->n); z = Z; for (it = S.begin(); it != S.end(); ++it) { z -= it->f; // printf("%d %d %d %d\n",it->f,it->s,it->n,z); if (z <= 0) break; else z += it->s; } if (it == S.end()) { printf("TAK\n"); for (it = S.begin(); it != S.end(); ++it) printf("%d ",it->n); printf("\n"); } else printf("NIE\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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; class triple { public: int f,s,n; }; bool cmp(triple i,triple j) { int di = (i.f - i.s < 0)?-1:1, dj = (j.f - j.s < 0)?-1:1; if (di * dj < 0) return di < dj; else if (di < 0) return i.f < j.f; else return i.f > j.f; } int main() { int n,N,Z,z; triple p; vector< triple > S; vector< triple >::const_iterator it; scanf("%d %d",&N,&Z); for (n = 0; n < N; n++) { scanf("%d %d",&(p.f),&(p.s)); p.n = n+1; S.push_back(p); } // for (it = S.begin(); it != S.end(); ++it) printf("%d %d %d\n",it->f,it->s,it->n); sort(S.begin(),S.end(),cmp); // for (it = S.begin(); it != S.end(); ++it) printf("%d %d %d\n",it->f,it->s,it->n); z = Z; for (it = S.begin(); it != S.end(); ++it) { z -= it->f; // printf("%d %d %d %d\n",it->f,it->s,it->n,z); if (z <= 0) break; else z += it->s; } if (it == S.end()) { printf("TAK\n"); for (it = S.begin(); it != S.end(); ++it) printf("%d ",it->n); printf("\n"); } else printf("NIE\n"); return 0; } |