#include <algorithm> //#include <iostream> #include <cstdio> #include <vector> #define DBG(X) using namespace std; struct X { int a, b, idx; bool operator<(X x) const { int d1 = 1 + max(x.a + a - b, a); int d2 = 1 + max(a + x.a - x.b, x.a); return d1 < d2; } }; int main() { vector<int> ret; int n; long long z, Z; scanf("%d%lld", &n, &z); vector<pair<int, pair<int, int> > > V1; vector<X> V2; //vector<pair<int, int> > D; Z = z; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); DBG(D.push_back(make_pair(a, b));) if (a <= b) { V1.push_back(make_pair(a, make_pair(b, i))); } else { X x; x.a = a; x.b = b; x.idx = i; V2.push_back(x); } } sort(V1.begin(), V1.end()); bool ok = true; for (int i = 0; i < V1.size(); i++) { //cout << V1[i].first << " " << V1[i].second.first << " " << V1[i].second.second<<endl; if (z > V1[i].first) { z -= V1[i].first; z += V1[i].second.first; // DBG(cout << "ret " << V1[i].second.second << endl;) ret.push_back(V1[i].second.second); } else { ok = false; break; } } if (ok) { long long s = 0; for (int i = 0; i < V2.size(); i++) { int d = V2[i].a - V2[i].b; s += d; } if (z - s <= 0) { ok = false; } } if (ok && V2.size()) { sort(V2.begin(), V2.end()); for (int i = 0; i < V2.size(); i++) { if (z - V2[i].a <= 0) { ok = false; break; } else { z -= V2[i].a; z += V2[i].b; ret.push_back(V2[i].idx); } } } //Z--; DBG( //cout << "ret.size() " << ret.size() << endl; set<int> Sret; bool okwer = true; for (int i = 0; i < ret.size(); i++) { if (ret[i] >= n) { cout << "blad indeksu" << endl; return 0; } Sret.insert(ret[i]); if (ok && Z - D[ret[i]].first <= 0) { cout << "zero" << endl; okwer = false; return 0; } Z -= D[ret[i]].first; Z += D[ret[i]].second; } //cout << "Sret.size() " << Sret.size() << endl; ) printf("%s\n", ok ? "TAK" : "NIE"); if (ok) { for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i] + 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <algorithm> //#include <iostream> #include <cstdio> #include <vector> #define DBG(X) using namespace std; struct X { int a, b, idx; bool operator<(X x) const { int d1 = 1 + max(x.a + a - b, a); int d2 = 1 + max(a + x.a - x.b, x.a); return d1 < d2; } }; int main() { vector<int> ret; int n; long long z, Z; scanf("%d%lld", &n, &z); vector<pair<int, pair<int, int> > > V1; vector<X> V2; //vector<pair<int, int> > D; Z = z; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); DBG(D.push_back(make_pair(a, b));) if (a <= b) { V1.push_back(make_pair(a, make_pair(b, i))); } else { X x; x.a = a; x.b = b; x.idx = i; V2.push_back(x); } } sort(V1.begin(), V1.end()); bool ok = true; for (int i = 0; i < V1.size(); i++) { //cout << V1[i].first << " " << V1[i].second.first << " " << V1[i].second.second<<endl; if (z > V1[i].first) { z -= V1[i].first; z += V1[i].second.first; // DBG(cout << "ret " << V1[i].second.second << endl;) ret.push_back(V1[i].second.second); } else { ok = false; break; } } if (ok) { long long s = 0; for (int i = 0; i < V2.size(); i++) { int d = V2[i].a - V2[i].b; s += d; } if (z - s <= 0) { ok = false; } } if (ok && V2.size()) { sort(V2.begin(), V2.end()); for (int i = 0; i < V2.size(); i++) { if (z - V2[i].a <= 0) { ok = false; break; } else { z -= V2[i].a; z += V2[i].b; ret.push_back(V2[i].idx); } } } //Z--; DBG( //cout << "ret.size() " << ret.size() << endl; set<int> Sret; bool okwer = true; for (int i = 0; i < ret.size(); i++) { if (ret[i] >= n) { cout << "blad indeksu" << endl; return 0; } Sret.insert(ret[i]); if (ok && Z - D[ret[i]].first <= 0) { cout << "zero" << endl; okwer = false; return 0; } Z -= D[ret[i]].first; Z += D[ret[i]].second; } //cout << "Sret.size() " << Sret.size() << endl; ) printf("%s\n", ok ? "TAK" : "NIE"); if (ok) { for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i] + 1); } return 0; } |