#include <algorithm> #include <iostream> struct MonsterInfo { int damage; int healthBonus; int position; }; bool compareMonsters(const MonsterInfo &a, const MonsterInfo &b) { if (a.damage < b.damage) { return true; } else if (a.damage == b.damage) { if (a.healthBonus > b.healthBonus) return true; else return a.position < b.position; } } bool compare2(const MonsterInfo &a, const MonsterInfo &b) { if (a.healthBonus > b.healthBonus) { return true; } else if (a.healthBonus == b.healthBonus) { if (a.damage < b.damage) return true; else return a.position < b.position; } } int main() { int numOfMonsters, life; std::cin >> numOfMonsters >> life; std::vector<MonsterInfo> monstersPlus, monstersMinus; for (int i = 0; i < numOfMonsters; i++) { MonsterInfo monster; std::cin >> monster.damage >> monster.healthBonus; monster.position = i + 1; if (monster.damage < monster.healthBonus) monstersPlus.push_back(monster); else monstersMinus.push_back(monster); } std::sort(std::begin(monstersPlus), std::end(monstersPlus), compareMonsters); std::sort(std::begin(monstersMinus), std::end(monstersMinus), compare2); bool alive = true; for (auto monster : monstersPlus) { life -= monster.damage; if (life <= 0) { alive = false; break; } life += monster.healthBonus; } if (alive) { for (auto monster : monstersMinus) { life -= monster.damage; if (life <= 0) { alive = false; break; } life += monster.healthBonus; } } if (alive) { std::cout << "TAK\n"; for (auto monster : monstersPlus) std::cout << monster.position << " "; for (auto monster : monstersMinus) std::cout << monster.position << " "; } else { std::cout << "NIE\n"; } }
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 <algorithm> #include <iostream> struct MonsterInfo { int damage; int healthBonus; int position; }; bool compareMonsters(const MonsterInfo &a, const MonsterInfo &b) { if (a.damage < b.damage) { return true; } else if (a.damage == b.damage) { if (a.healthBonus > b.healthBonus) return true; else return a.position < b.position; } } bool compare2(const MonsterInfo &a, const MonsterInfo &b) { if (a.healthBonus > b.healthBonus) { return true; } else if (a.healthBonus == b.healthBonus) { if (a.damage < b.damage) return true; else return a.position < b.position; } } int main() { int numOfMonsters, life; std::cin >> numOfMonsters >> life; std::vector<MonsterInfo> monstersPlus, monstersMinus; for (int i = 0; i < numOfMonsters; i++) { MonsterInfo monster; std::cin >> monster.damage >> monster.healthBonus; monster.position = i + 1; if (monster.damage < monster.healthBonus) monstersPlus.push_back(monster); else monstersMinus.push_back(monster); } std::sort(std::begin(monstersPlus), std::end(monstersPlus), compareMonsters); std::sort(std::begin(monstersMinus), std::end(monstersMinus), compare2); bool alive = true; for (auto monster : monstersPlus) { life -= monster.damage; if (life <= 0) { alive = false; break; } life += monster.healthBonus; } if (alive) { for (auto monster : monstersMinus) { life -= monster.damage; if (life <= 0) { alive = false; break; } life += monster.healthBonus; } } if (alive) { std::cout << "TAK\n"; for (auto monster : monstersPlus) std::cout << monster.position << " "; for (auto monster : monstersMinus) std::cout << monster.position << " "; } else { std::cout << "NIE\n"; } } |