标签:
这题我得到一个经验,bool型的dp一定要想办法把bool去掉来表示更多的东西(1933也是这个道理)
暴力大家都会,这里有两个限制条件
一个限制条件我们可以排序不断加入,另一个呢
我们可以用f[i]表示c[]的和等于i时,最小的b[]最大是多少
然后转移判断就很容易解决了
1 var sum,a,b,c:array[0..1010] of longint; 2 w,s,h,f,d:array[0..1000010] of longint; 3 ans:array[0..1000010] of boolean; 4 mx,i,n,j,k,q:longint; 5 6 function min(a,b:longint):longint; 7 begin 8 if a>b then exit(b) else exit(a); 9 end; 10 11 function max(a,b:longint):longint; 12 begin 13 if a>b then exit(a) else exit(b); 14 end; 15 16 procedure swap(var a,b:longint); 17 var c:longint; 18 begin 19 c:=a; 20 a:=b; 21 b:=c; 22 end; 23 24 procedure sorta(l,r:longint); 25 var i,j,x:longint; 26 begin 27 i:=l; 28 j:=r; 29 x:=a[(l+r) shr 1]; 30 repeat 31 while a[i]<x do inc(i); 32 while x<a[j] do dec(j); 33 if not(i>j) then 34 begin 35 swap(a[i],a[j]); 36 swap(b[i],b[j]); 37 swap(c[i],c[j]); 38 inc(i); 39 dec(j); 40 end; 41 until i>j; 42 if l<j then sorta(l,j); 43 if i<r then sorta(i,r); 44 end; 45 46 procedure sortq(l,r:longint); 47 var i,j,x:longint; 48 begin 49 i:=l; 50 j:=r; 51 x:=w[(l+r) shr 1]; 52 repeat 53 while w[i]<x do inc(i); 54 while x<w[j] do dec(j); 55 if not(i>j) then 56 begin 57 swap(w[i],w[j]); 58 swap(s[i],s[j]); 59 swap(h[i],h[j]); 60 swap(d[i],d[j]); 61 inc(i); 62 dec(j); 63 end; 64 until i>j; 65 if l<j then sortq(l,j); 66 if i<r then sortq(i,r); 67 end; 68 69 begin 70 readln(n); 71 for i:=1 to n do 72 readln(c[i],a[i],b[i]); 73 sorta(1,n); 74 for i:=1 to n do 75 sum[i]:=sum[i-1]+c[i]; 76 77 readln(q); 78 for i:=1 to q do 79 begin 80 readln(w[i],h[i],s[i]); 81 d[i]:=i; 82 mx:=max(mx,h[i]); 83 end; 84 sortq(1,q); 85 j:=1; 86 f[0]:=1000000007; 87 for i:=1 to q do 88 begin 89 while (j<=n) and (a[j]<=w[i]) do 90 begin 91 for k:=min(mx,sum[j]) downto c[j] do 92 if f[k-c[j]]>0 then f[k]:=max(f[k],min(f[k-c[j]],b[j])); 93 inc(j); 94 end; 95 ans[d[i]]:=(f[h[i]]>w[i]+s[i]); 96 end; 97 for i:=1 to q do 98 if ans[i] then writeln(‘TAK‘) else writeln(‘NIE‘); 99 end.
标签:
原文地址:http://www.cnblogs.com/phile/p/4533470.html