//: Piotr Skotnicki // Potyczki Algorytmiczne 2014 // BOH #include <algorithm> #include <functional> #include <cstdio> #include <cstdint> #include <vector> #include <set> #include <utility> using namespace std; #define X first #define Y second typedef pair<int, int> PI; typedef pair<int64_t, int> PLI; const int INF = 1000000000; int main() { int n, z; scanf("%d %d", &n, &z); vector<int> d(n), a(n), gain(n), order; order.reserve(n); set<PI, greater<PI>> killable; set<PLI, less<PLI>> next; set<PI, greater<PI>> last; for (int i = 0; i < n; ++i) { scanf("%d %d", &d[i], &a[i]); gain[i] = a[i] - d[i]; if (gain[i] > 0) { if (d[i] < z) { killable.insert(PI(gain[i], i)); } else { next.insert(PLI(d[i], i)); } } else { if (gain[i] == 0 && d[i] < z) { order.emplace_back(i); } else { last.insert(PI(a[i], i)); } } } bool ok = true; int64_t cz = z; while (!killable.empty()) { const PI u = *killable.begin(); killable.erase(killable.begin()); cz += u.X; order.emplace_back(u.Y); auto it = next.begin(), itend = next.upper_bound(PLI(cz, INF)); while (it != itend) { killable.insert(PI(gain[it->Y], it->Y)); next.erase(it++); } } if (!next.empty()) { ok = false; } else { if (!last.empty()) { for (auto& p : last) { if (d[p.Y] >= cz) { ok = false; break; } cz += gain[p.Y]; if (cz <= 0) { ok = false; break; } order.emplace_back(p.Y); } } } printf("%s\n", ok ? "TAK" : "NIE"); if (ok) { for (int u : order) { printf("%d ", u + 1); } } 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 | //: Piotr Skotnicki // Potyczki Algorytmiczne 2014 // BOH #include <algorithm> #include <functional> #include <cstdio> #include <cstdint> #include <vector> #include <set> #include <utility> using namespace std; #define X first #define Y second typedef pair<int, int> PI; typedef pair<int64_t, int> PLI; const int INF = 1000000000; int main() { int n, z; scanf("%d %d", &n, &z); vector<int> d(n), a(n), gain(n), order; order.reserve(n); set<PI, greater<PI>> killable; set<PLI, less<PLI>> next; set<PI, greater<PI>> last; for (int i = 0; i < n; ++i) { scanf("%d %d", &d[i], &a[i]); gain[i] = a[i] - d[i]; if (gain[i] > 0) { if (d[i] < z) { killable.insert(PI(gain[i], i)); } else { next.insert(PLI(d[i], i)); } } else { if (gain[i] == 0 && d[i] < z) { order.emplace_back(i); } else { last.insert(PI(a[i], i)); } } } bool ok = true; int64_t cz = z; while (!killable.empty()) { const PI u = *killable.begin(); killable.erase(killable.begin()); cz += u.X; order.emplace_back(u.Y); auto it = next.begin(), itend = next.upper_bound(PLI(cz, INF)); while (it != itend) { killable.insert(PI(gain[it->Y], it->Y)); next.erase(it++); } } if (!next.empty()) { ok = false; } else { if (!last.empty()) { for (auto& p : last) { if (d[p.Y] >= cz) { ok = false; break; } cz += gain[p.Y]; if (cz <= 0) { ok = false; break; } order.emplace_back(p.Y); } } } printf("%s\n", ok ? "TAK" : "NIE"); if (ok) { for (int u : order) { printf("%d ", u + 1); } } return 0; } |