首先明显会想到贪心
对于那些怪物回血比耗血多的,我们显然应该先打耗血少的
那些回血比耗血多的怎么办呢?由于不管怎么打(假设体力负数了还能打),最终体力是一定,
我们从最终体力倒推,相当于先吃药掉血,打怪物回血,这样就转变为第一种情况了
显然我们因先打带的药回血少的,即从正序想,我们因先打所带药物回血多的怪物
1 type node=record 2 key,num,loc:longint; 3 end; 4 list=array[0..100010] of node; 5 var a,b:list; 6 ans:array[0..100010] of longint; 7 x,y,t,t1,t2,i,n:longint; 8 m:int64; 9 10 procedure swap(var a,b:node); 11 var c:node; 12 begin 13 c:=a; 14 a:=b; 15 b:=c; 16 end; 17 18 procedure qsort(var a:list;m:longint); 19 procedure sort(l,r: longint); 20 var i,j,x,y: longint; 21 begin 22 i:=l; 23 j:=r; 24 x:=a[(l+r) div 2].key; 25 repeat 26 while a[i].key<x do inc(i); 27 while x<a[j].key do dec(j); 28 if not(i>j) then 29 begin 30 swap(a[i],a[j]); 31 inc(i); 32 j:=j-1; 33 end; 34 until i>j; 35 if l<j then sort(l,j); 36 if i<r then sort(i,r); 37 end; 38 39 begin 40 sort(1,m); 41 end; 42 43 begin 44 readln(n,m); 45 for i:=1 to n do 46 begin 47 readln(x,y); 48 if x<=y then 49 begin 50 inc(t1); 51 a[t1].key:=x; 52 a[t1].num:=y; 53 a[t1].loc:=i; 54 end 55 else begin 56 inc(t2); 57 b[t2].key:=y; 58 b[t2].num:=x; 59 b[t2].loc:=i; 60 end; 61 end; 62 qsort(a,t1); 63 for i:=1 to t1 do 64 if a[i].key>=m then 65 begin 66 writeln('NIE'); 67 halt; 68 end 69 else begin 70 m:=m-a[i].key+a[i].num; 71 inc(t); 72 ans[t]:=a[i].loc; 73 end; 74 qsort(b,t2); 75 for i:=t2 downto 1 do 76 if b[i].num>=m then 77 begin 78 writeln('NIE'); 79 halt; 80 end 81 else begin 82 inc(t); 83 ans[t]:=b[i].loc; 84 m:=m-b[i].num+b[i].key; 85 end; 86 writeln('TAK'); 87 for i:=1 to t do 88 write(ans[i],' '); 89 writeln; 90 end.