#include <stdio.h> #include <algorithm> using namespace std; struct monster_t { int id, damage, health; }; bool compareMonsters(const monster_t &m1, const monster_t &m2) { int diff1 = m1.health - m1.damage, diff2 = m2.health - m2.damage; if(diff1 > 0 && diff2 <= 0) // + and - return true; if(diff1 <= 0 && diff2 > 0) // - and + return false; if(diff1 <= 0) // both - return m1.health > m2.health; return m1.damage < m2.damage; // both + } int main() { int n; long long health; scanf("%d %lld", &n, &health); monster_t monsters[n]; for(int i = 0; i < n; ++i) { monsters[i].id = i + 1; scanf("%d %d", &monsters[i].damage, &monsters[i].health); } sort(monsters, monsters + n, compareMonsters); bool ok = true; for(int i = 0; i < n; ++i) { health -= monsters[i].damage; if(health <= 0) { ok = false; break; } health += monsters[i].health; } if(ok) { printf("TAK\n"); for(int i = 0; i < n; ++i) printf("%d ", monsters[i].id); printf("\n"); } else printf("NIE\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 | #include <stdio.h> #include <algorithm> using namespace std; struct monster_t { int id, damage, health; }; bool compareMonsters(const monster_t &m1, const monster_t &m2) { int diff1 = m1.health - m1.damage, diff2 = m2.health - m2.damage; if(diff1 > 0 && diff2 <= 0) // + and - return true; if(diff1 <= 0 && diff2 > 0) // - and + return false; if(diff1 <= 0) // both - return m1.health > m2.health; return m1.damage < m2.damage; // both + } int main() { int n; long long health; scanf("%d %lld", &n, &health); monster_t monsters[n]; for(int i = 0; i < n; ++i) { monsters[i].id = i + 1; scanf("%d %d", &monsters[i].damage, &monsters[i].health); } sort(monsters, monsters + n, compareMonsters); bool ok = true; for(int i = 0; i < n; ++i) { health -= monsters[i].damage; if(health <= 0) { ok = false; break; } health += monsters[i].health; } if(ok) { printf("TAK\n"); for(int i = 0; i < n; ++i) printf("%d ", monsters[i].id); printf("\n"); } else printf("NIE\n"); return 0; } |