#include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; const int MX = 100005; struct Monster { int id; int d, a; bool operator < (const Monster& m) const { if (a - d == m.a - m.d) return id < m.id; return a - d > m.a - m.d; } }; bool cmp(const Monster &m1, const Monster &m2) { return m1.d < m2.d; } int n; long long z1, z2; vector<Monster> pos, neg; vector<Monster> respos, resneg; void solve(vector<Monster>& v, vector<Monster>& res, long long z) { sort(v.begin(), v.end(), cmp); set<Monster> S; int i = 0; while (true) { while (i < (int) v.size() && (long long) v[i].d < z) { S.insert(v[i++]); } if (!S.empty()) { Monster best = *S.begin(); S.erase(S.begin()); z += (long long) (best.a - best.d); res.push_back(best); } else { break; } } } int main() { scanf("%d%lld", &n, &z1); z2 = z1; for (int i = 0; i < n; ++i) { Monster m; scanf("%d%d", &m.d, &m.a); m.id = i+1; z2 += (long long) (m.a - m.d); if (m.a - m.d >= 0) { pos.push_back(m); } else { swap(m.d, m.a); neg.push_back(m); } } solve(pos, respos, z1); solve(neg, resneg, z2); if ((int) respos.size() + (int) resneg.size() < n) { printf("NIE\n"); return 0; } printf("TAK\n"); for (int i = 0; i < (int) respos.size(); ++i) printf("%d ", respos[i].id); for (int i = (int) resneg.size() - 1; i >= 0; --i) printf("%d ", resneg[i].id); 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 | #include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; const int MX = 100005; struct Monster { int id; int d, a; bool operator < (const Monster& m) const { if (a - d == m.a - m.d) return id < m.id; return a - d > m.a - m.d; } }; bool cmp(const Monster &m1, const Monster &m2) { return m1.d < m2.d; } int n; long long z1, z2; vector<Monster> pos, neg; vector<Monster> respos, resneg; void solve(vector<Monster>& v, vector<Monster>& res, long long z) { sort(v.begin(), v.end(), cmp); set<Monster> S; int i = 0; while (true) { while (i < (int) v.size() && (long long) v[i].d < z) { S.insert(v[i++]); } if (!S.empty()) { Monster best = *S.begin(); S.erase(S.begin()); z += (long long) (best.a - best.d); res.push_back(best); } else { break; } } } int main() { scanf("%d%lld", &n, &z1); z2 = z1; for (int i = 0; i < n; ++i) { Monster m; scanf("%d%d", &m.d, &m.a); m.id = i+1; z2 += (long long) (m.a - m.d); if (m.a - m.d >= 0) { pos.push_back(m); } else { swap(m.d, m.a); neg.push_back(m); } } solve(pos, respos, z1); solve(neg, resneg, z2); if ((int) respos.size() + (int) resneg.size() < n) { printf("NIE\n"); return 0; } printf("TAK\n"); for (int i = 0; i < (int) respos.size(); ++i) printf("%d ", respos[i].id); for (int i = (int) resneg.size() - 1; i >= 0; --i) printf("%d ", resneg[i].id); printf("\n"); return 0; } |