#include <iostream> #include <list> using namespace std; typedef struct enemy_struct { int num, loss, gain; } enemy; bool compare_enemies(const enemy& e1, const enemy& e2) { int e1diff = e1.gain - e1.loss; int e2diff = e2.gain - e2.loss; if (e1diff <= 0 && e2diff <= 0) { return e2.loss < e1.loss; } int diff = e2diff - e1diff; if (diff != 0) { return diff < 0; } else { return e2.loss > e1.loss; } } int main() { int count, life; cin >> count >> life; list<int> moves; list<enemy> enemies; for (int i = 1; i <= count; i++) { enemy e; e.num = i; cin >> e.loss >> e.gain; enemies.push_back(e); } enemies.sort(compare_enemies); list<enemy>::iterator iter = enemies.begin(); while (iter != enemies.end()) { if (iter->loss < life) { life -= iter->loss; life += iter->gain; moves.push_back(iter->num); enemies.erase(iter); iter = enemies.begin(); } else { iter++; } } if (enemies.empty()) { cout << "TAK" << endl; for (list<int>::iterator it = moves.begin(); it != moves.end(); ++it) { cout << *it << " "; } cout << endl; } else { cout << "NIE" << endl; } 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 | #include <iostream> #include <list> using namespace std; typedef struct enemy_struct { int num, loss, gain; } enemy; bool compare_enemies(const enemy& e1, const enemy& e2) { int e1diff = e1.gain - e1.loss; int e2diff = e2.gain - e2.loss; if (e1diff <= 0 && e2diff <= 0) { return e2.loss < e1.loss; } int diff = e2diff - e1diff; if (diff != 0) { return diff < 0; } else { return e2.loss > e1.loss; } } int main() { int count, life; cin >> count >> life; list<int> moves; list<enemy> enemies; for (int i = 1; i <= count; i++) { enemy e; e.num = i; cin >> e.loss >> e.gain; enemies.push_back(e); } enemies.sort(compare_enemies); list<enemy>::iterator iter = enemies.begin(); while (iter != enemies.end()) { if (iter->loss < life) { life -= iter->loss; life += iter->gain; moves.push_back(iter->num); enemies.erase(iter); iter = enemies.begin(); } else { iter++; } } if (enemies.empty()) { cout << "TAK" << endl; for (list<int>::iterator it = moves.begin(); it != moves.end(); ++it) { cout << *it << " "; } cout << endl; } else { cout << "NIE" << endl; } return 0; } |