#include <iostream> #include <algorithm> using namespace std; struct mon { int cost; int income; int id; }; bool operator <(const mon &a, const mon &b) { return a.cost < b.cost; } bool compare_2 (const mon &a, const mon &b) { return a.cost == b.cost ? a.income > b.income : a.cost > b.cost; } int main () { mon mon_i[100000]; mon mon_l[100000]; int mi = 0, ml = 0; int n; long long z; cin >> n >> z; long long rl = 0; for (int i = 0; i < n; i++) { int d, g; cin >> d >> g; int income = g - d; if (income > 0) { mon_i[mi].cost = d; mon_i[mi].id = i + 1; mon_i[mi++].income = income; } else { mon_l[ml].cost = d; mon_l[ml].id = i + 1; rl += (-income); mon_l[ml++].income = g; } } sort(mon_i, mon_i + mi); sort(mon_l, mon_l + ml, compare_2); for (int i = 0; i < mi; i++) { int cost = mon_i[i].cost; if (cost < z) { z += mon_i[i].income; } else { cout << "NIE" << endl; return 0; } } if (rl < z) { cout << "TAK" << endl; for (int i = 0; i < mi; i++) { cout << mon_i[i].id << " "; } for (int i = 0; i < ml; i++) { cout << mon_l[i].id << " "; } cout << endl; } else { cout << "NIE" << endl; } }
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 | #include <iostream> #include <algorithm> using namespace std; struct mon { int cost; int income; int id; }; bool operator <(const mon &a, const mon &b) { return a.cost < b.cost; } bool compare_2 (const mon &a, const mon &b) { return a.cost == b.cost ? a.income > b.income : a.cost > b.cost; } int main () { mon mon_i[100000]; mon mon_l[100000]; int mi = 0, ml = 0; int n; long long z; cin >> n >> z; long long rl = 0; for (int i = 0; i < n; i++) { int d, g; cin >> d >> g; int income = g - d; if (income > 0) { mon_i[mi].cost = d; mon_i[mi].id = i + 1; mon_i[mi++].income = income; } else { mon_l[ml].cost = d; mon_l[ml].id = i + 1; rl += (-income); mon_l[ml++].income = g; } } sort(mon_i, mon_i + mi); sort(mon_l, mon_l + ml, compare_2); for (int i = 0; i < mi; i++) { int cost = mon_i[i].cost; if (cost < z) { z += mon_i[i].income; } else { cout << "NIE" << endl; return 0; } } if (rl < z) { cout << "TAK" << endl; for (int i = 0; i < mi; i++) { cout << mon_i[i].id << " "; } for (int i = 0; i < ml; i++) { cout << mon_l[i].id << " "; } cout << endl; } else { cout << "NIE" << endl; } } |