#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Hero { unsigned hp; }; struct Monster { unsigned id; unsigned deadPoints; unsigned healthPoints; int diff() const { return static_cast<int>(healthPoints) - static_cast<int>(deadPoints); } }; struct Result { bool positive; vector<int> order; }; struct SortByHealthPoints { bool operator()(Monster const& lhs, Monster const& rhs) { return lhs.diff() > rhs.diff(); } }; void fightPositiveMonsters(Hero & hero, vector<Monster> & positiveMonsters, Result & result) { sort(positiveMonsters.begin(), positiveMonsters.end(), SortByHealthPoints()); while (!positiveMonsters.empty()) { auto it = positiveMonsters.begin(); for (; it != positiveMonsters.end(); ++it) { if (it->diff() < static_cast<int>(hero.hp)) break; } if (it == positiveMonsters.end()) { if (positiveMonsters.empty()) { result.positive = false; return; } else { result.positive = true; return; } } else { int newHp = static_cast<int>(hero.hp) - it->deadPoints; if (0 >= newHp) { result.positive = false; return; } hero.hp = static_cast<unsigned>(newHp + it->healthPoints); result.order.push_back(it->id); positiveMonsters.erase(it); } } result.positive = true; } bool compareByDeadness(Monster const& m1, Monster const& m2) { if( m1.deadPoints != m2.deadPoints ) return m1.deadPoints < m2.deadPoints; else return m1.healthPoints < m2.healthPoints; } void fightNegativeMonsters(Hero & hero, vector<Monster> & negativeMonsters, Result & result) { sort(negativeMonsters.begin(), negativeMonsters.end(), compareByDeadness); while(!negativeMonsters.empty()) { auto it = negativeMonsters.end() - 1; int newHp = static_cast<int>(hero.hp) - it->deadPoints; if (0 >= newHp) { result.positive = false; return; } hero.hp = static_cast<unsigned>(newHp + it->healthPoints); result.order.push_back(it->id); negativeMonsters.erase(it); } result.positive = true; } void fightNeutralMonsters(vector<Monster> & neutralMonsters, Result & result) { for (auto it = neutralMonsters.begin(); it != neutralMonsters.end(); ++it) { result.order.push_back(it->id); } } int main(void) { cin.sync_with_stdio(false); unsigned numberOfMonsters; cin >> numberOfMonsters; unsigned initialHp; cin >> initialHp; vector<Monster> positiveMonsters; vector<Monster> negativeMonsters; vector<Monster> neutralMonsters; for (unsigned i = 0; i < numberOfMonsters; ++i) { Monster monster; monster.id = i + 1; cin >> monster.deadPoints >> monster.healthPoints; if (monster.healthPoints > monster.deadPoints) { positiveMonsters.push_back(monster); } else if (monster.healthPoints < monster.deadPoints) { negativeMonsters.push_back(monster); } else { neutralMonsters.push_back(monster); } } Hero hero; hero.hp = initialHp; Result result; result.positive = false; fightPositiveMonsters(hero, positiveMonsters, result); if (result.positive) { fightNegativeMonsters(hero, negativeMonsters, result); if (result.positive) { fightNeutralMonsters(neutralMonsters, result); cout << "TAK" << endl; for (unsigned i = 0; i < result.order.size(); ++i) { cout << result.order[i]; if (i != result.order.size()) { cout << " "; } } cout << endl; } else { cout << "NIE" << 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 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 152 153 154 155 156 157 158 159 160 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Hero { unsigned hp; }; struct Monster { unsigned id; unsigned deadPoints; unsigned healthPoints; int diff() const { return static_cast<int>(healthPoints) - static_cast<int>(deadPoints); } }; struct Result { bool positive; vector<int> order; }; struct SortByHealthPoints { bool operator()(Monster const& lhs, Monster const& rhs) { return lhs.diff() > rhs.diff(); } }; void fightPositiveMonsters(Hero & hero, vector<Monster> & positiveMonsters, Result & result) { sort(positiveMonsters.begin(), positiveMonsters.end(), SortByHealthPoints()); while (!positiveMonsters.empty()) { auto it = positiveMonsters.begin(); for (; it != positiveMonsters.end(); ++it) { if (it->diff() < static_cast<int>(hero.hp)) break; } if (it == positiveMonsters.end()) { if (positiveMonsters.empty()) { result.positive = false; return; } else { result.positive = true; return; } } else { int newHp = static_cast<int>(hero.hp) - it->deadPoints; if (0 >= newHp) { result.positive = false; return; } hero.hp = static_cast<unsigned>(newHp + it->healthPoints); result.order.push_back(it->id); positiveMonsters.erase(it); } } result.positive = true; } bool compareByDeadness(Monster const& m1, Monster const& m2) { if( m1.deadPoints != m2.deadPoints ) return m1.deadPoints < m2.deadPoints; else return m1.healthPoints < m2.healthPoints; } void fightNegativeMonsters(Hero & hero, vector<Monster> & negativeMonsters, Result & result) { sort(negativeMonsters.begin(), negativeMonsters.end(), compareByDeadness); while(!negativeMonsters.empty()) { auto it = negativeMonsters.end() - 1; int newHp = static_cast<int>(hero.hp) - it->deadPoints; if (0 >= newHp) { result.positive = false; return; } hero.hp = static_cast<unsigned>(newHp + it->healthPoints); result.order.push_back(it->id); negativeMonsters.erase(it); } result.positive = true; } void fightNeutralMonsters(vector<Monster> & neutralMonsters, Result & result) { for (auto it = neutralMonsters.begin(); it != neutralMonsters.end(); ++it) { result.order.push_back(it->id); } } int main(void) { cin.sync_with_stdio(false); unsigned numberOfMonsters; cin >> numberOfMonsters; unsigned initialHp; cin >> initialHp; vector<Monster> positiveMonsters; vector<Monster> negativeMonsters; vector<Monster> neutralMonsters; for (unsigned i = 0; i < numberOfMonsters; ++i) { Monster monster; monster.id = i + 1; cin >> monster.deadPoints >> monster.healthPoints; if (monster.healthPoints > monster.deadPoints) { positiveMonsters.push_back(monster); } else if (monster.healthPoints < monster.deadPoints) { negativeMonsters.push_back(monster); } else { neutralMonsters.push_back(monster); } } Hero hero; hero.hp = initialHp; Result result; result.positive = false; fightPositiveMonsters(hero, positiveMonsters, result); if (result.positive) { fightNegativeMonsters(hero, negativeMonsters, result); if (result.positive) { fightNeutralMonsters(neutralMonsters, result); cout << "TAK" << endl; for (unsigned i = 0; i < result.order.size(); ++i) { cout << result.order[i]; if (i != result.order.size()) { cout << " "; } } cout << endl; } else { cout << "NIE" << endl; } } else { cout << "NIE" << endl; } } |