#include <iostream> #include <string> #include <vector> #include <utility> #include <iterator> #include <algorithm> using namespace std; struct Pair { int id; int first; int second; int third; }; inline bool less_than_first(const Pair& n1, const Pair& n2) { return n1.first < n2.first; } inline bool greater_than_second(const Pair& n1, const Pair& n2) { return n1.second > n2.second; } inline bool greater_than_first(const Pair& n1, const Pair& n2) { return n1.first > n2.first; } inline bool less_than_second(const Pair& n1, const Pair& n2) { return n1.second < n2.second; } inline bool greater_than_third(const Pair& n1, const Pair& n2) { return n1.third > n2.third; } inline bool less_than_third(const Pair& n1, const Pair& n2) { return n1.third < n2.third; } int main() { std::ios_base::sync_with_stdio (false); int count; long long hp; cin >> count; cin >> hp; vector<Pair> pluses; vector<Pair> minuses; vector<int> matches; int id = 1; for (int i = 0; i < count; ++i) { int d, a; cin >> d; cin >> a; Pair p; p.id = id++; p.first = d; p.second = a - d; p.third = a; if (p.second >= 0) { pluses.push_back(p); } else { minuses.push_back(p); } } stable_sort( pluses.begin(), pluses.end(), greater_than_second ); stable_sort( pluses.begin(), pluses.end(), less_than_first ); stable_sort( minuses.begin(), minuses.end(), greater_than_first ); stable_sort( minuses.begin(), minuses.end(), greater_than_third ); for (int i = 0; i < pluses.size(); ++i) { if (hp <= pluses[i].first) { cout << "NIE" << endl; return 0; } matches.push_back(pluses[i].id); hp += pluses[i].second; } if(minuses.size() >= 2) { for (int i = 0; i < minuses.size() - 2; ++i) { if (hp <= minuses[i].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[i].id); hp += minuses[i].second; } int s = minuses.size(); int s2 = minuses.size() - 2; int s1 = minuses.size() - 1; if (hp + minuses[s2].second - minuses[s1].first <= 0) { int tmp = s2; s2 = s1; s1 = tmp; } if (hp <= minuses[s2].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[s2].id); hp += minuses[s2].second; if (hp <= minuses[s1].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[s1].id); } else { for (int i = 0; i < minuses.size(); ++i) { if (hp <= minuses[i].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[i].id); hp += minuses[i].second; } } cout << "TAK" << endl; for (int i = 0; i < matches.size(); ++i) { if (i > 0) cout << " "; cout << matches[i]; } cout << 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 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 | #include <iostream> #include <string> #include <vector> #include <utility> #include <iterator> #include <algorithm> using namespace std; struct Pair { int id; int first; int second; int third; }; inline bool less_than_first(const Pair& n1, const Pair& n2) { return n1.first < n2.first; } inline bool greater_than_second(const Pair& n1, const Pair& n2) { return n1.second > n2.second; } inline bool greater_than_first(const Pair& n1, const Pair& n2) { return n1.first > n2.first; } inline bool less_than_second(const Pair& n1, const Pair& n2) { return n1.second < n2.second; } inline bool greater_than_third(const Pair& n1, const Pair& n2) { return n1.third > n2.third; } inline bool less_than_third(const Pair& n1, const Pair& n2) { return n1.third < n2.third; } int main() { std::ios_base::sync_with_stdio (false); int count; long long hp; cin >> count; cin >> hp; vector<Pair> pluses; vector<Pair> minuses; vector<int> matches; int id = 1; for (int i = 0; i < count; ++i) { int d, a; cin >> d; cin >> a; Pair p; p.id = id++; p.first = d; p.second = a - d; p.third = a; if (p.second >= 0) { pluses.push_back(p); } else { minuses.push_back(p); } } stable_sort( pluses.begin(), pluses.end(), greater_than_second ); stable_sort( pluses.begin(), pluses.end(), less_than_first ); stable_sort( minuses.begin(), minuses.end(), greater_than_first ); stable_sort( minuses.begin(), minuses.end(), greater_than_third ); for (int i = 0; i < pluses.size(); ++i) { if (hp <= pluses[i].first) { cout << "NIE" << endl; return 0; } matches.push_back(pluses[i].id); hp += pluses[i].second; } if(minuses.size() >= 2) { for (int i = 0; i < minuses.size() - 2; ++i) { if (hp <= minuses[i].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[i].id); hp += minuses[i].second; } int s = minuses.size(); int s2 = minuses.size() - 2; int s1 = minuses.size() - 1; if (hp + minuses[s2].second - minuses[s1].first <= 0) { int tmp = s2; s2 = s1; s1 = tmp; } if (hp <= minuses[s2].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[s2].id); hp += minuses[s2].second; if (hp <= minuses[s1].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[s1].id); } else { for (int i = 0; i < minuses.size(); ++i) { if (hp <= minuses[i].first) { cout << "NIE" << endl; return 0; } matches.push_back(minuses[i].id); hp += minuses[i].second; } } cout << "TAK" << endl; for (int i = 0; i < matches.size(); ++i) { if (i > 0) cout << " "; cout << matches[i]; } cout << endl; } |