#include <iostream> #include <algorithm> #include <sstream> #include <string> using namespace std; struct monster { long id; long d; long a; monster(){}; monster(long i_, long d_, long a_):id(i_), d(d_), a(a_){}; }; monster min_monsters[100000]; monster add_monsters[100000]; struct tree{ long z; tree* trees; }; bool g_ok = false; bool pu[100000]; long zt[100001]; long result[100000]; long res_len = 0; long iter = 0; long g_z, n; int compare (const void * a, const void * b) { if ( ((monster*)a)->d < ((monster*)b)->d ) return 1; if ( ((monster*)a)->d == ((monster*)b)->d ) return 0; if ( ((monster*)a)->d > ((monster*)b)->d ) return -1; } void fight(long z, monster* monsters, long len, long count) { iter++; if(0 == count) { g_z = z; g_ok = true; return; } for(long i=0; i<len; i++) { if(!pu[i] && z-monsters[i].d>0) { pu[i] = 1; fight(z-monsters[i].d+monsters[i].a, monsters, len, count-1); if(g_ok) { result[res_len++] = monsters[i].id; return; } pu[i] = 0; } } } int main() { long i, add_i, min_i, d, a, all_d, all_a; ostringstream str_monters; cin.sync_with_stdio(false); cin>>n>>g_z; all_a = g_z; all_d = 0; add_i=min_i=0; for(i=0; i<n; ++i) { cin>>d>>a; all_d+=d; all_a+=a; if(a-d>=0) { if(d<g_z) { g_z = g_z-d+a; str_monters<<i+1<<" "; }else { add_monsters[add_i].id = i+1; add_monsters[add_i].d = d; add_monsters[add_i].a = a; add_i++; } } else { min_monsters[min_i].id = i+1; min_monsters[min_i].d = d; min_monsters[min_i].a = a; min_i++; } } if(all_a<=all_d) { cout<<"NIE"<<endl; return 0; } else { fight(g_z, add_monsters, add_i, add_i); if(!g_ok) cout<<"NIE"<<endl; else { g_ok = false; fight(g_z, min_monsters, min_i, min_i); if(!g_ok) cout<<"NIE"<<endl; else { cout<<"TAK"<<endl; for(i=res_len-1; i>=0; --i) str_monters<<result[i]<<" "; cout<<str_monters.str()<<endl; } } } 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 <iostream> #include <algorithm> #include <sstream> #include <string> using namespace std; struct monster { long id; long d; long a; monster(){}; monster(long i_, long d_, long a_):id(i_), d(d_), a(a_){}; }; monster min_monsters[100000]; monster add_monsters[100000]; struct tree{ long z; tree* trees; }; bool g_ok = false; bool pu[100000]; long zt[100001]; long result[100000]; long res_len = 0; long iter = 0; long g_z, n; int compare (const void * a, const void * b) { if ( ((monster*)a)->d < ((monster*)b)->d ) return 1; if ( ((monster*)a)->d == ((monster*)b)->d ) return 0; if ( ((monster*)a)->d > ((monster*)b)->d ) return -1; } void fight(long z, monster* monsters, long len, long count) { iter++; if(0 == count) { g_z = z; g_ok = true; return; } for(long i=0; i<len; i++) { if(!pu[i] && z-monsters[i].d>0) { pu[i] = 1; fight(z-monsters[i].d+monsters[i].a, monsters, len, count-1); if(g_ok) { result[res_len++] = monsters[i].id; return; } pu[i] = 0; } } } int main() { long i, add_i, min_i, d, a, all_d, all_a; ostringstream str_monters; cin.sync_with_stdio(false); cin>>n>>g_z; all_a = g_z; all_d = 0; add_i=min_i=0; for(i=0; i<n; ++i) { cin>>d>>a; all_d+=d; all_a+=a; if(a-d>=0) { if(d<g_z) { g_z = g_z-d+a; str_monters<<i+1<<" "; }else { add_monsters[add_i].id = i+1; add_monsters[add_i].d = d; add_monsters[add_i].a = a; add_i++; } } else { min_monsters[min_i].id = i+1; min_monsters[min_i].d = d; min_monsters[min_i].a = a; min_i++; } } if(all_a<=all_d) { cout<<"NIE"<<endl; return 0; } else { fight(g_z, add_monsters, add_i, add_i); if(!g_ok) cout<<"NIE"<<endl; else { g_ok = false; fight(g_z, min_monsters, min_i, min_i); if(!g_ok) cout<<"NIE"<<endl; else { cout<<"TAK"<<endl; for(i=res_len-1; i>=0; --i) str_monters<<result[i]<<" "; cout<<str_monters.str()<<endl; } } } return 0; } |