#include <climits> #include <cstdio> #include <vector> #include <iostream> #include <tuple> #include <cstdlib> #include <string> #include <algorithm> using namespace std; typedef tuple<int, int, int> monster; struct algorithm { int n, z; vector<monster> v_add; vector<monster> v_sub; vector<int> results; void read_data() { cin >> n >> z; for (int d, a, pos = 1; pos <= n; ++pos) { cin >> d >> a; if (a-d >= 0) { v_add.push_back(make_tuple(d, a, pos)); } else { v_sub.push_back(make_tuple(-d, a, pos)); } } } bool doit() { sort(v_add.begin(), v_add.end()); sort(v_sub.begin(), v_sub.end()); for (int i = 0; i < v_add.size(); ++i) { monster &m = v_add[i]; if (z-get<0>(m) > 0) { results.push_back(get<2>(m)); z = z - get<0>(m) + get<1>(m); } else { cout << "NIE" << endl; return false; } } for (int i = 0; i < v_sub.size(); ++i) { monster &m = v_sub[i]; if (z+get<0>(m) > 0) { results.push_back(get<2>(m)); z = z + get<0>(m) + get<1>(m); } else { cout << "NIE" << endl; return false; } } print(); return true; } void print() { cout << "TAK" << endl; for (int pos = 1; pos <= results.size(); ++pos) { cout << results[pos-1]; if (pos < results.size()) cout << " "; } cout << endl; } }; int main(void) { //freopen("input", "r", stdin); size_t n_of_testcases; algorithm a; //cin >> n_of_testcases; n_of_testcases = 1; for (size_t test_idx = 1; test_idx <= n_of_testcases; ++test_idx) { a.read_data(); a.doit(); } 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 | #include <climits> #include <cstdio> #include <vector> #include <iostream> #include <tuple> #include <cstdlib> #include <string> #include <algorithm> using namespace std; typedef tuple<int, int, int> monster; struct algorithm { int n, z; vector<monster> v_add; vector<monster> v_sub; vector<int> results; void read_data() { cin >> n >> z; for (int d, a, pos = 1; pos <= n; ++pos) { cin >> d >> a; if (a-d >= 0) { v_add.push_back(make_tuple(d, a, pos)); } else { v_sub.push_back(make_tuple(-d, a, pos)); } } } bool doit() { sort(v_add.begin(), v_add.end()); sort(v_sub.begin(), v_sub.end()); for (int i = 0; i < v_add.size(); ++i) { monster &m = v_add[i]; if (z-get<0>(m) > 0) { results.push_back(get<2>(m)); z = z - get<0>(m) + get<1>(m); } else { cout << "NIE" << endl; return false; } } for (int i = 0; i < v_sub.size(); ++i) { monster &m = v_sub[i]; if (z+get<0>(m) > 0) { results.push_back(get<2>(m)); z = z + get<0>(m) + get<1>(m); } else { cout << "NIE" << endl; return false; } } print(); return true; } void print() { cout << "TAK" << endl; for (int pos = 1; pos <= results.size(); ++pos) { cout << results[pos-1]; if (pos < results.size()) cout << " "; } cout << endl; } }; int main(void) { //freopen("input", "r", stdin); size_t n_of_testcases; algorithm a; //cin >> n_of_testcases; n_of_testcases = 1; for (size_t test_idx = 1; test_idx <= n_of_testcases; ++test_idx) { a.read_data(); a.doit(); } return 0; } |