#include <cstdio> #include <algorithm> #include <vector> #define FOREACH(n, i) for(int i=0;i<n;i++) #define size(a)(int)a.size() #define I -1 using namespace std; struct threes{ int i, x, y; }; bool comp(threes a, threes b){ return a.x < b.x; } bool comp1(threes *a, threes *b){ return (a->x-a->y) < (b->x-b->y); } int main(){ int n, naj=0, suma; threes temp; long long int z; scanf("%d%llu", &n, &z); threes *T = new threes[n]; vector<int>kol; vector<threes>T1; vector<threes*>T2; FOREACH(n, i){ T[i].i=i; scanf("%d%d", &T[i].x, &T[i].y); } sort(T, T+n, comp); FOREACH(n, i){ if(T[i].x - T[i].y <= 0){ if(T[i].x != -1 && z-T[i].x > 0){ z-=(T[i].x-T[i].y); kol.push_back(T[i].i); } } else { temp=T[i]; T1.push_back(temp); } } reverse(T1.begin(), T1.end()); FOREACH(size(T1), i) T2.push_back(&T1.at(i)); sort(T2.begin(), T2.end(), comp1); suma=0; int j=0, min=1000000; while(j < size(T2)){ suma+=T2[j]->x-T2[j]->y; j++; } if(suma <= z) FOREACH(size(T2), i){ while(naj < size(T1)-1 && T1[naj].x == -1)naj++; if((size(kol) == n-1 && T2[i]->x != -1 && T2[i]->x < z) || (T2[i]->x != -1 && T2[i]->x < z && T2[i]->x-T2[i]->y <= z-suma)){ z-=(T2[i]->x-T2[i]->y); kol.push_back(T2[i]->i); suma-=(T2[i]->x-T2[i]->y); T2[i]->x=-1; }else if(T1[naj].x != -1 && T1[naj].x-T1[naj].y > z-suma) if(T1[naj].x - T1[naj].y < z){ z-=(T1[naj].x-T1[naj].y); kol.push_back(T1[naj].i); suma-=(T1[naj].x-T1[naj].y); i--; T1[naj].x=-1; } } if(size(kol) == n){ printf("TAK\n"); FOREACH(size(kol)-1, i) printf("%d ", kol.at(i)+1); printf("%d", kol.at(size(kol)-1)+1); }else printf("NIE"); delete[] T; 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 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 | #include <cstdio> #include <algorithm> #include <vector> #define FOREACH(n, i) for(int i=0;i<n;i++) #define size(a)(int)a.size() #define I -1 using namespace std; struct threes{ int i, x, y; }; bool comp(threes a, threes b){ return a.x < b.x; } bool comp1(threes *a, threes *b){ return (a->x-a->y) < (b->x-b->y); } int main(){ int n, naj=0, suma; threes temp; long long int z; scanf("%d%llu", &n, &z); threes *T = new threes[n]; vector<int>kol; vector<threes>T1; vector<threes*>T2; FOREACH(n, i){ T[i].i=i; scanf("%d%d", &T[i].x, &T[i].y); } sort(T, T+n, comp); FOREACH(n, i){ if(T[i].x - T[i].y <= 0){ if(T[i].x != -1 && z-T[i].x > 0){ z-=(T[i].x-T[i].y); kol.push_back(T[i].i); } } else { temp=T[i]; T1.push_back(temp); } } reverse(T1.begin(), T1.end()); FOREACH(size(T1), i) T2.push_back(&T1.at(i)); sort(T2.begin(), T2.end(), comp1); suma=0; int j=0, min=1000000; while(j < size(T2)){ suma+=T2[j]->x-T2[j]->y; j++; } if(suma <= z) FOREACH(size(T2), i){ while(naj < size(T1)-1 && T1[naj].x == -1)naj++; if((size(kol) == n-1 && T2[i]->x != -1 && T2[i]->x < z) || (T2[i]->x != -1 && T2[i]->x < z && T2[i]->x-T2[i]->y <= z-suma)){ z-=(T2[i]->x-T2[i]->y); kol.push_back(T2[i]->i); suma-=(T2[i]->x-T2[i]->y); T2[i]->x=-1; }else if(T1[naj].x != -1 && T1[naj].x-T1[naj].y > z-suma) if(T1[naj].x - T1[naj].y < z){ z-=(T1[naj].x-T1[naj].y); kol.push_back(T1[naj].i); suma-=(T1[naj].x-T1[naj].y); i--; T1[naj].x=-1; } } if(size(kol) == n){ printf("TAK\n"); FOREACH(size(kol)-1, i) printf("%d ", kol.at(i)+1); printf("%d", kol.at(size(kol)-1)+1); }else printf("NIE"); delete[] T; return 0; } |