- 关路灯
- 2014-07-15 13:39:18 @
本人比较新手,刚学习dp没多久……好不容易有一两题会写……结果还是wa了。其他的数据都过,就是卡死在第4个数据。求大牛帮看看我下面这个程序出错在哪……
program p1150;
var
f:array[1..100,1..100] of longint;
a,p:array[1..100] of longint;
i,k,n:longint;
procedure dp(v,b:longint);
var
ad,left,right:longint;
begin
if b=n then exit;
if (a[v]>a[k]) and (v>=b) then begin
left:=v-b;
right:=v+1;
end;
if (a[v]<=a[k]) and (v+b<=n) then begin
right:= v+b;
left :=v-1;
end;
ad:=0;
if left>0 then begin {dp to left}
for i := 1 to left do ad:=ad+p[i]*(a[v]-a[left]);
for i := right to n do ad:=ad+p[i]*(a[v]-a[left]);
if (f[left,b+1]=0) or (f[left,b+1]>(ad+f[v,b])) then begin
f[left,b+1]:=ad+f[v,b];
dp(left,b+1);
end;
end;
ad:=0;
if right<=n then begin {dp to right}
for i := 1 to left do ad:=ad+p[i]*(a[right]-a[v]);
for i := right to n do ad:=ad+p[i]*(a[right]-a[v]);
if (f[right,b+1]=0) or (f[right,b+1]>(ad+f[v,b])) then begin
f[right,b+1]:=ad+f[v,b];
dp(right,b+1);
end;
end;
end;
begin
read(n,k);
for i := 1 to n do
read(a[i],p[i]);
dp(k,1);
if f[1,n]>f[n,n] then write(f[n,n])
else write(f[1,n]);
end.
1 条评论
-
zxu22 LV 10 @ 2014-07-15 13:39:59
好吧……程序看不出来的……只求给点指引
- 1