3384/1750: [Usaco2004 Nov]Apple Catching 接苹果

3384/1750: [Usaco2004 Nov]Apple Catching 接苹果

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 18  Solved: 16
[Submit][Status][Discuss]

Description

很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2),每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果从树上落下.但是,由于苹果掉到地上会摔烂,贝茜必须在半空中接住苹果(没有人爱吃摔烂的苹果).贝茜吃东西很快,所以她接到苹果后仅用几秒钟就能吃完.每一分钟,两棵苹果树其中的一棵会掉落一个苹果.贝茜已经过了足够的训练,只要站在树下就一定能接住这棵树上掉落的苹果.同时,贝茜能够在两棵树之间快速移动(移动时间远少于1分钟),因此当苹果掉落时,她必定站在两棵树其中的一棵下面.此外,奶牛不愿意不停地往返于两棵树之间,因此会错过一些苹果, 苹果每分钟掉落一个,共T(1≤T≤1000)分钟,贝茜最多愿意移动W(I≤w≤30)次.现给

出每分钟掉落苹果的树的编号,要求判定贝茜能够接住的最多苹果数.开始时贝茜在1号树下.

Input

第1行:由空格隔开的两个整数T和W.

第2到T+1行:1或2(每分钟掉落苹果的树的编号).

Output

在贝茜移动次数不超过W的前提下她能接到的最多苹果数

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

HINT

7分钟内共掉落7个苹果一一第1个从第2棵树上掉落,接下来的2个苹果从第1棵树上掉落,再接下来的2个从第2棵树上掉落,最后2个从第1棵树上掉落.   贝茜不移动直到接到从第1棵树上掉落的两个苹果,然后移动到第2棵树下,直到接到从第2棵

树上掉落的两个苹果,最后移动到第1棵树下,接住最后两个从第1棵树上掉落的苹果.这样贝茜共接住6个苹果.

Source

Gold

题解:一道有趣的DP问题,设f[i,j,k]表示现在时间为i,已经跑了j次,当前在k树下面的最优解,于是f[i,j,k]:=longint(a[i]=k)+min(a[i-1,j,k],a[i-1,j-1,3-k]),这两种情况分别代表直接继承上一时间位置和本次进行了一次走动,然后当a[i]=k时,longint(a[i]=k)=1,也就是苹果数+1,否则不加。。。(循环数组就是萌萌哒不解释)

 1 /**************************************************************
 2     Problem: 3384
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:4 ms
 7     Memory:236 kb
 8 ****************************************************************/
 9
10 var
11    i,j,k,l,m,n:longint;
12    a:array[0..2000] of longint;
13    b:array[0..1,0..30,1..2] of longint;
14 function max(x,y:longint):longint;
15          begin
16               if x>y then max:=x else max:=y;
17          end;
18 begin
19      readln(n,m);
20      for i:=1 to n do readln(a[i]);
21      fillchar(b,sizeof(b),0);
22      for i:=1 to n do
23          for j:=0 to m do
24              for k:=1 to 2 do
25                  begin
26                       l:=b[(i+1) mod 2,j,k];
27                       if j>0 then l:=max(b[(i+1) mod 2,j-1,3-k],l);
28                       b[i mod 2,j,k]:=longint(a[i]=k)+l;
29                  end;
30      l:=0;
31      for i:=0 to m do
32          for j:=1 to 2 do
33              l:=max(l,b[n mod 2,i,j]);
34      writeln(l);
35      readln;
36 end.      

呵呵呵我会告诉你下面还有一遍?么么哒我是有多无聊= =(HansBug:不过双倍经验还是好评如潮^_^)

 1 /**************************************************************
 2     Problem: 1750
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:4 ms
 7     Memory:236 kb
 8 ****************************************************************/
 9
10 var
11    i,j,k,l,m,n:longint;
12    a:array[0..2000] of longint;
13    b:array[0..1,0..30,1..2] of longint;
14 function max(x,y:longint):longint;
15          begin
16               if x>y then max:=x else max:=y;
17          end;
18 begin
19      readln(n,m);
20      for i:=1 to n do readln(a[i]);
21      fillchar(b,sizeof(b),0);
22      for i:=1 to n do
23          for j:=0 to m do
24              for k:=1 to 2 do
25                  begin
26                       l:=b[(i+1) mod 2,j,k];
27                       if j>0 then l:=max(b[(i+1) mod 2,j-1,3-k],l);
28                       b[i mod 2,j,k]:=longint(a[i]=k)+l;
29                  end;
30      l:=0;
31      for i:=0 to m do
32          for j:=1 to 2 do
33              l:=max(l,b[n mod 2,i,j]);
34      writeln(l);
35      readln;
36 end.      
时间: 2024-10-08 11:37:51

3384/1750: [Usaco2004 Nov]Apple Catching 接苹果的相关文章

BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )

dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1750双倍经验) ------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm>

P3384: [Usaco2004 Nov]Apple Catching 接苹果

一道DP题, f[i,j,k] 表示 第 k 时刻 由 1 位置 变换 j 次 到达 当前 i 棵树 注意也要维护 变换 0 次的情况. 1 var i,j,k,t,w,now:longint; 2 tree:array[1..2,0..1001] of longint; 3 f:array[1..2,0..50,0..1001] of longint; 4 function max(a,b:longint):longint; 5 begin 6 if a>b then exit(a) 7 el

POJ 2385 Apple Catching 接苹果 DP

题目链接:POJ 2385 Apple Catching Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7858   Accepted: 3846 Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently number

Apple Catching(POJ 2385)

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9978   Accepted: 4839 Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, eac

POJ2385 Apple Catching 【DP】

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8018   Accepted: 3922 Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, eac

Apple Catching(dp)

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9831   Accepted: 4779 Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, eac

POJ2385——Apple Catching

Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8062   Accepted: 3951 Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, eac

3385: [Usaco2004 Nov]Lake Counting 数池塘

3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 21[Submit][Status][Discuss] Description 农夫约翰的农场可以表示成N×M(1≤N,M≤100)个方格组成的矩形.由于近日的降雨, 在约翰农场上的不同地方形成了池塘.每一个方格或者有积水(’W’)或者没有积水(’.’).农夫约翰打算数出他的农场上共形成了多少池塘.一个

BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘

题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MB Description 农夫约翰的农场可以表示成N×M(1≤N,M≤100)个方格组成的矩形.由于近日的降雨, 在约翰农场上的不同地方形成了池塘.每一个方格或者有积水(’W’)或者没有积水(’.’).农夫约翰打算数出他的农场上共形成了多少池塘.一个池塘是一系列相连的有积水的方格,每一个方格周围的八个方格都被认为是与这个方格相连的. 现