#include <cstdio> #include <vector> #include <algorithm> #include <functional> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define MP make_pair #define FI first #define SE second #define PII pair<int, int> #define PB push_back #define LL long long using namespace std; int n; LL z; vector<PII> good; vector<int> aa; vector<int> dd; vector<pair<PII, int> > badDiff; vector<pair<PII, int> > badD; vector<bool> used; vector<int> res; bool solve() { sort(good.begin(), good.end()); int gn = good.size(); REP(i, gn) { if (z <= (LL)good[i].FI) return false; z += (LL)aa[good[i].SE] - (LL)dd[good[i].SE]; res.PB(good[i].SE); } sort(badDiff.begin(), badDiff.end()); sort(badD.begin(), badD.end(), greater<pair<PII, int> >()); int bn = badDiff.size(); int pd = 0; int pdiff = 0; while (pd < bn && pdiff < bn) { if ((LL)dd[badDiff[pdiff].SE] < z && (LL)badD[pd].FI.FI < (z - (LL)badDiff[pdiff].FI.FI)) { res.PB(badDiff[pdiff].SE); z -= (LL)badDiff[pdiff].FI.FI; used[badDiff[pdiff].SE] = true; ++pdiff; } else if ((LL)badD[pd].FI.FI < z) { res.PB(badD[pd].SE); z -= badD[pd].FI.FI - aa[badD[pd].SE]; used[badD[pd].SE] = true; ++pd; } else return false; while (pdiff < bn && used[badDiff[pdiff].SE]) ++pdiff; while (pd < bn && used[badD[pd].SE]) ++pd; } return true; } int main() { scanf("%d%lld", &n, &z); REP(i, n) { int d, a; scanf("%d%d", &d, &a); aa.PB(a); dd.PB(d); used.PB(false); if (a >= d) { good.PB(MP(d, i)); } else { badDiff.PB(MP(MP(d - a, -d), i)); badD.PB(MP(MP(d, -(d - a)), i)); } } bool solution = solve(); if (!solution || res.size() != n) printf("NIE\n"); else { printf("TAK\n"); REP(i, n) printf("%s%d", i == 0 ? "" : " ", res[i] + 1); printf("\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 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 | #include <cstdio> #include <vector> #include <algorithm> #include <functional> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define MP make_pair #define FI first #define SE second #define PII pair<int, int> #define PB push_back #define LL long long using namespace std; int n; LL z; vector<PII> good; vector<int> aa; vector<int> dd; vector<pair<PII, int> > badDiff; vector<pair<PII, int> > badD; vector<bool> used; vector<int> res; bool solve() { sort(good.begin(), good.end()); int gn = good.size(); REP(i, gn) { if (z <= (LL)good[i].FI) return false; z += (LL)aa[good[i].SE] - (LL)dd[good[i].SE]; res.PB(good[i].SE); } sort(badDiff.begin(), badDiff.end()); sort(badD.begin(), badD.end(), greater<pair<PII, int> >()); int bn = badDiff.size(); int pd = 0; int pdiff = 0; while (pd < bn && pdiff < bn) { if ((LL)dd[badDiff[pdiff].SE] < z && (LL)badD[pd].FI.FI < (z - (LL)badDiff[pdiff].FI.FI)) { res.PB(badDiff[pdiff].SE); z -= (LL)badDiff[pdiff].FI.FI; used[badDiff[pdiff].SE] = true; ++pdiff; } else if ((LL)badD[pd].FI.FI < z) { res.PB(badD[pd].SE); z -= badD[pd].FI.FI - aa[badD[pd].SE]; used[badD[pd].SE] = true; ++pd; } else return false; while (pdiff < bn && used[badDiff[pdiff].SE]) ++pdiff; while (pd < bn && used[badD[pd].SE]) ++pd; } return true; } int main() { scanf("%d%lld", &n, &z); REP(i, n) { int d, a; scanf("%d%d", &d, &a); aa.PB(a); dd.PB(d); used.PB(false); if (a >= d) { good.PB(MP(d, i)); } else { badDiff.PB(MP(MP(d - a, -d), i)); badD.PB(MP(MP(d, -(d - a)), i)); } } bool solution = solve(); if (!solution || res.size() != n) printf("NIE\n"); else { printf("TAK\n"); REP(i, n) printf("%s%d", i == 0 ? "" : " ", res[i] + 1); printf("\n"); } return 0; } |