NOIP2005采药

来源:百度知道 编辑:UC知道 时间:2024/06/22 06:32:14
用二维DP做的,不知道为什么怎么样都2个对2个超时6个错
帮忙看下是程序有问题还是数据有问题
var f:array[0..200,0..200]of integer;
c:array[1..30]of integer;
w:array[1..30]of integer;
m,v,i,j:integer;

function max(a,b:integer):integer;
var tt:integer;
begin
if a>b then tt:=a else tt:=b;
max:=tt;
exit(tt);
end;

begin
assign(input,'medic.in');
assign(output,'medic.out');
reset(input); rewrite(output);

readln(v,m);

for i:=1 to m do
readln(c[i],w[i]);

for i:=1 to m do
for j:=1 to v do
begin
if j>=c[i]
then f[i,j]:=max(f[i-1,j-c[i]]+w[i],f[i-1,j])
else f[i,j]:=f[i-1,j];

end;
writeln(f[m,j]);
close(input);
close(output);
end.

有个地方错了
for i:=1 to m do
for j:=1 to v do
begin
f[i,j]:=f[i-1,j];
if j>=c[i]
then f[i,j]:=max(f[i-1,j-c[i]]+w[i],f[i-1,j])
这应该是 动态规划了!!

当然会超时,要用动规