标签:for 独立 选择 cti 保留 des 个数 valentine output
先预处理出f1[i,j],为前i个物品,用了j钱的最大价值,
同理我们再预处理出f2[i,j],为i到n个物品,用了j钱的最大价值。
于是对于每次询问,我们可以枚举z,求max(f1[x-1,z]+f2[x+1,y-z])即可。
代码
1 var 2 n,m,ans:longint; 3 a,b,c:array [0..1001] of longint; 4 f1,f2:array [0..1001,0..1001] of longint; 5 function max(o,p:longint):longint; 6 begin 7 if o>p then exit(o); 8 exit(p); 9 end; 10 11 procedure init; 12 var 13 i,j,k:longint; 14 begin 15 readln(n); 16 for i:=1 to n do 17 readln(a[i],b[i],c[i]); 18 for i:=1 to n do 19 for j:=1000 downto 0 do 20 begin 21 f1[i,j]:=f1[i-1,j]; 22 for k:=1 to c[i] do 23 if j>=k*a[i] then 24 f1[i,j]:=max(f1[i,j],f1[i-1,j-k*a[i]]+k*b[i]) 25 else break; 26 end; 27 for i:=n downto 1 do 28 for j:=1000 downto 0 do 29 begin 30 f2[i,j]:=f2[i+1,j]; 31 for k:=1 to c[i] do 32 if j>=k*a[i] then 33 f2[i,j]:=max(f2[i,j],f2[i+1,j-k*a[i]]+k*b[i]) 34 else break; 35 end; 36 end; 37 38 procedure main; 39 var 40 i,j,x,y:longint; 41 begin 42 readln(m); 43 for i:=1 to m do 44 begin 45 readln(x,y); 46 ans:=0; 47 for j:=0 to y do 48 ans:=max(ans,f1[x,y-j]+f2[x+2,j]); 49 writeln(ans); 50 end; 51 end; 52 53 begin 54 init; 55 main; 56 end.
JZOJ_3223. 【HBOI2013】Ede的新背包问题 (Standard IO)
标签:for 独立 选择 cti 保留 des 个数 valentine output
原文地址:https://www.cnblogs.com/zyx-crying/p/9493707.html