#include <stdio.h> #include <stdlib.h> typedef struct { int d,a,ix; } monster; int cmp(const void * a, const void * b) { const monster *m1 = a; const monster *m2 = b; if (m1->a - m1->d > 0) return -1; if (m2->a - m2->d > 0) return 1; if (m1->d == m2->d) return (m2->a - m1->a); return (m2->d - m1->d); } int main() { int n,z; scanf("%d %d", &n, &z); monster monsters[n]; int i; for (i=0;i<n;i++) { scanf("%d %d", &(monsters[i].d), &(monsters[i].a)); monsters[i].ix=i+1; } qsort(monsters, n, sizeof(monster), cmp); int isOk=1; for (i=0;i<n;i++) { z-=monsters[i].d; if (z<=0) { isOk=0; break; } z+=monsters[i].a; } if (isOk) { printf("TAK\n"); for (i=0;i<n;i++) { printf("%d ", monsters[i].ix); } } 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include <stdlib.h> typedef struct { int d,a,ix; } monster; int cmp(const void * a, const void * b) { const monster *m1 = a; const monster *m2 = b; if (m1->a - m1->d > 0) return -1; if (m2->a - m2->d > 0) return 1; if (m1->d == m2->d) return (m2->a - m1->a); return (m2->d - m1->d); } int main() { int n,z; scanf("%d %d", &n, &z); monster monsters[n]; int i; for (i=0;i<n;i++) { scanf("%d %d", &(monsters[i].d), &(monsters[i].a)); monsters[i].ix=i+1; } qsort(monsters, n, sizeof(monster), cmp); int isOk=1; for (i=0;i<n;i++) { z-=monsters[i].d; if (z<=0) { isOk=0; break; } z+=monsters[i].a; } if (isOk) { printf("TAK\n"); for (i=0;i<n;i++) { printf("%d ", monsters[i].ix); } } else { printf("NIE\n"); } return 0; } |