pku3661 Running
USACO月赛的银组题,dp性质比较明显,实现时有细节要注意
用f[i,j]表示第i分钟跑,疲劳度为j时的最远距离
g[i,j]表示第i分钟休息,疲劳度为j时的最远距离
有f[i,j]=max{f[i-1,j-1]+d[i] i>1 //上一秒跑
g[i-1,0]+d[i] j=1 //上一秒刚休息到疲劳为零}
g[i,j]=max{g[i-1,j+1] j<m //上一秒休息
f[i-1,j+1] j<m //上一秒跑
g[i-1,0] j=0 //注意这个情况,体力为零时仍可以再休息}
View Code
1 program pku3661(input,output);
2 var
3 h : array[0..10001] of longint;
4 f,g : array[0..10001,-1..501] of longint;
5 n,m : longint;
6 procedure init;
7 var
8 i : longint;
9 begin
10 readln(n,m);
11 for i:=1 to n do
12 read(h[i]);
13 end; { init }
14 function max(aa,bb :longint ):longint;
15 begin
16 if aa>bb then
17 exit(aa);
18 exit(bb);
19 end; { max }
20 function min(aa,bb :longint ):longint;
21 begin
22 if aa<bb then
23 exit(aa);
24 exit(bb);
25 end; { min }
26 procedure main;
27 var
28 i,j : longint;
29 begin
30 fillchar(f,sizeof(f),0);
31 fillchar(g,sizeof(g),0);
32 for i:=1 to n do
33 for j:=0 to m do
34 begin
35 if j>1 then
36 f[i,j]:=max(f[i-1,j-1]+h[i],f[i,j]);
37 if j=1 then
38 f[i,j]:=max(f[i,j],g[i-1,0]+h[i]);
39 if j<=m-1 then
40 g[i,j]:=max(g[i-1,j+1],f[i-1,j+1]);
41 if j=0 then
42 g[i,j]:=max(g[i-1,0],g[i,j]);
43 end;
44 end; { main }
45 procedure print;
46 begin
47 writeln(g[n,0]);
48 end; { print }
49 begin
50 assign(input,'cowrun.in');reset(input);
51 assign(output,'cowrun.out');rewrite(output);
52 init;
53 main;
54 print;
55 close(input);
56 close(output);
57 end.