#include <cstdio> #include <vector> #include <algorithm> struct Monster { int hp; int elixir; int num; }; std::vector<Monster> monsters; long long HP; bool cmp(const Monster & a, const Monster & b) { long long diffA = a.elixir - a.hp; long long diffB = b.elixir - b.hp; if (diffA * diffB <= 0) //sa roznego znaku return diffA > diffB; //chcemy dodani przodem. if (diffA < 0) //oba sa ujemne return a.hp > b.hp;//najpierw to co wymaga wiecej //oba dodatnie return a.hp < b.hp; //najpierw to co mniej wymaga. } bool isPossible() { std::sort(monsters.begin(), monsters.end(), cmp); for (int i = 0; i < monsters.size(); ++i) { //printf("Mam %lld hp. Potwor : %d %d\n", HP, monsters[i].hp, monsters[i].elixir); HP -= monsters[i].hp; if (HP <= 0) return false; HP += monsters[i].elixir; } return true; } int main() { int n; scanf("%d%lld", &n, &HP); monsters.resize(n); for (int i = 0; i < n; ++i) { scanf("%d%d", &monsters[i].hp, &monsters[i].elixir); monsters[i].num = i + 1; } bool possible = isPossible(); printf(possible ? "TAK\n" : "NIE\n"); if (possible) { for(int i = 0; i < n; ++i) printf("%d ", monsters[i].num); printf("\n"); } 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 | #include <cstdio> #include <vector> #include <algorithm> struct Monster { int hp; int elixir; int num; }; std::vector<Monster> monsters; long long HP; bool cmp(const Monster & a, const Monster & b) { long long diffA = a.elixir - a.hp; long long diffB = b.elixir - b.hp; if (diffA * diffB <= 0) //sa roznego znaku return diffA > diffB; //chcemy dodani przodem. if (diffA < 0) //oba sa ujemne return a.hp > b.hp;//najpierw to co wymaga wiecej //oba dodatnie return a.hp < b.hp; //najpierw to co mniej wymaga. } bool isPossible() { std::sort(monsters.begin(), monsters.end(), cmp); for (int i = 0; i < monsters.size(); ++i) { //printf("Mam %lld hp. Potwor : %d %d\n", HP, monsters[i].hp, monsters[i].elixir); HP -= monsters[i].hp; if (HP <= 0) return false; HP += monsters[i].elixir; } return true; } int main() { int n; scanf("%d%lld", &n, &HP); monsters.resize(n); for (int i = 0; i < n; ++i) { scanf("%d%d", &monsters[i].hp, &monsters[i].elixir); monsters[i].num = i + 1; } bool possible = isPossible(); printf(possible ? "TAK\n" : "NIE\n"); if (possible) { for(int i = 0; i < n; ++i) printf("%d ", monsters[i].num); printf("\n"); } return 0; } |