#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<stdint.h>
using namespace std;
//#define T_RUN
struct Entry {
int i;
int d,a;
inline int dif() { return a-d; }
};
bool pSort(Entry* a, Entry* b) {
return (a->d) < (b->d);
}
bool nSort(Entry* a, Entry* b) {
return (a->a) < (b->a);
}
void log(const char* name, vector<Entry*>& l) {
#ifdef DEBUG
printf("%s: \n",name);
for(int i=0;i<l.size();++i) printf("[%d] %d %d\n",l[i]->i,l[i]->d,l[i]->a);
#endif
}
void log(const char* name, int64_t life, Entry* i) {
#ifdef DEBUG
printf("%s: [%d] %lld -> [%d %d] -> %lld -> %lld\n",name,i->i, life, i->d, i->a, life- i->d, life - i->d + i->a);
#endif
}
int main() {
int n,z;
Entry* data;
vector<Entry*> ep;
vector<Entry*> en;
vector<int> wayp;
vector<int> wayn;
scanf("%d %d",&n,&z);
data=(Entry*)malloc(sizeof(Entry)*n);
int64_t endz=z;
for(int i=0;i<n;++i) {
Entry* v=&data[i];
scanf("%d %d",&v->d,&v->a);
endz+=v->a-v->d;
v->i=i+1;
if(v->d<=v->a) ep.push_back(v);
else en.push_back(v);
}
sort(ep.begin(),ep.end(), pSort);
sort(en.begin(),en.end(), nSort);
log("Positive",ep);
log("Negative",en);
#ifdef DEBUG
printf("End sum: %lld\n",endz);
#endif
if(endz<=0) {
printf("NIE\n");
return 0;
}
// przejscie z jednej strony
int64_t game=z;
for(int i=0;i<ep.size();++i) {
Entry* v=ep[i];
log("ProcessingP",game, v);
game-=v->d;
if(game<=0) {
printf("NIE\n");
return 0;
}
game+=v->a;
wayp.push_back(v->i);
}
game=endz; // przejście w drugą stronę
for(int i=0;i<en.size();++i) {
Entry* v=en[i];
log("ProcessingN",game,v);
game-=v->a;
if(game<=0) {
printf("NIE\n");
return 0;
}
game+=v->d;
wayn.push_back(v->i);
}
printf("TAK\n");
#ifdef T_RUN
{
int64_t l=z;
for(int i=0;i<wayp.size();++i) {
Entry* v=&data[wayp[i]-1];
l-=v->d;
if(l<=0) {
printf("FATAL!\n");
return -1;
}
l+=v->a;
}
for(int i=0;i<wayn.size();++i) {
Entry* v=&data[wayn[wayn.size()-i-1]-1];
l-=v->d;
if(l<=0) {
printf("FATAL!\n");
return -1;
}
l+=v->a;
}
}
#else
for(int i=0;i<wayp.size();++i) {
printf("%d ",wayp[i]);
}
for(int i=0;i<wayn.size();++i) {
printf("%d ",wayn[wayn.size()-i-1]);
}
printf("\n");
#endif
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #include<stdio.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<stdint.h> using namespace std; //#define T_RUN struct Entry { int i; int d,a; inline int dif() { return a-d; } }; bool pSort(Entry* a, Entry* b) { return (a->d) < (b->d); } bool nSort(Entry* a, Entry* b) { return (a->a) < (b->a); } void log(const char* name, vector<Entry*>& l) { #ifdef DEBUG printf("%s: \n",name); for(int i=0;i<l.size();++i) printf("[%d] %d %d\n",l[i]->i,l[i]->d,l[i]->a); #endif } void log(const char* name, int64_t life, Entry* i) { #ifdef DEBUG printf("%s: [%d] %lld -> [%d %d] -> %lld -> %lld\n",name,i->i, life, i->d, i->a, life- i->d, life - i->d + i->a); #endif } int main() { int n,z; Entry* data; vector<Entry*> ep; vector<Entry*> en; vector<int> wayp; vector<int> wayn; scanf("%d %d",&n,&z); data=(Entry*)malloc(sizeof(Entry)*n); int64_t endz=z; for(int i=0;i<n;++i) { Entry* v=&data[i]; scanf("%d %d",&v->d,&v->a); endz+=v->a-v->d; v->i=i+1; if(v->d<=v->a) ep.push_back(v); else en.push_back(v); } sort(ep.begin(),ep.end(), pSort); sort(en.begin(),en.end(), nSort); log("Positive",ep); log("Negative",en); #ifdef DEBUG printf("End sum: %lld\n",endz); #endif if(endz<=0) { printf("NIE\n"); return 0; } // przejscie z jednej strony int64_t game=z; for(int i=0;i<ep.size();++i) { Entry* v=ep[i]; log("ProcessingP",game, v); game-=v->d; if(game<=0) { printf("NIE\n"); return 0; } game+=v->a; wayp.push_back(v->i); } game=endz; // przejście w drugą stronę for(int i=0;i<en.size();++i) { Entry* v=en[i]; log("ProcessingN",game,v); game-=v->a; if(game<=0) { printf("NIE\n"); return 0; } game+=v->d; wayn.push_back(v->i); } printf("TAK\n"); #ifdef T_RUN { int64_t l=z; for(int i=0;i<wayp.size();++i) { Entry* v=&data[wayp[i]-1]; l-=v->d; if(l<=0) { printf("FATAL!\n"); return -1; } l+=v->a; } for(int i=0;i<wayn.size();++i) { Entry* v=&data[wayn[wayn.size()-i-1]-1]; l-=v->d; if(l<=0) { printf("FATAL!\n"); return -1; } l+=v->a; } } #else for(int i=0;i<wayp.size();++i) { printf("%d ",wayp[i]); } for(int i=0;i<wayn.size();++i) { printf("%d ",wayn[wayn.size()-i-1]); } printf("\n"); #endif return 0; } |
English