首页 > 移动平台 > 详细

POJ 2385 Apple Catching

时间:2015-03-08 15:37:20      阅读:282      评论:0      收藏:0      [点我收藏+]

Apple Catching

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2385
64-bit integer IO format: %lld      Java class name: Main
 
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, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds. 

Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples). 

Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
 

Input

* Line 1: Two space separated integers: T and W 

* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
 

Output

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.
 

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

INPUT DETAILS: 

Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice. 

OUTPUT DETAILS: 

Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
 

Source

 

解题:动态规划。。。有点像01背包。。。

技术分享
 1 /*
 2 @author: Lev
 3 @date:
 4 */
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <cstdlib>
11 #include <algorithm>
12 #include <map>
13 #include <set>
14 #include <queue>
15 #include <climits>
16 #include <deque>
17 #include <sstream>
18 #include <fstream>
19 #include <bitset>
20 #include <iomanip>
21 #define LL long long
22 #define INF 0x3f3f3f3f
23 
24 using namespace std;
25 const int maxn = 1010;
26 int d[maxn],dp[2][40];
27 int main() {
28     int T,W;
29     while(~scanf("%d %d",&T,&W)) {
30         for(int i = 1; i <= T; ++i)
31             scanf("%d",d+i);
32         memset(dp,0,sizeof(dp));
33         dp[0][W] = 1;
34         for(int i = 1; i <= T; ++i)
35             for(int j = W; j >= 0; --j) {
36                 if(d[i] == 1) {
37                     dp[0][j] = max(dp[0][j]+1,dp[1][j-1]+1);
38                     dp[1][j] = max(dp[0][j-1],dp[1][j]);
39                 }else{
40                     dp[1][j] = max(dp[1][j]+1,dp[0][j-1]+1);
41                     dp[0][j] = max(dp[0][j],dp[1][j-1]);
42                 }
43             }
44         printf("%d\n",max(dp[0][W],dp[1][W]));
45     }
46     return 0;
47 }
View Code

 

噢、。。貌似会有出界的风险。。好吧。。再偏移下。。

技术分享
 1 /*
 2 @author: Lev
 3 @date:
 4 */
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <cstdlib>
11 #include <algorithm>
12 #include <map>
13 #include <set>
14 #include <queue>
15 #include <climits>
16 #include <deque>
17 #include <sstream>
18 #include <fstream>
19 #include <bitset>
20 #include <iomanip>
21 #define LL long long
22 #define INF 0x3f3f3f3f
23 
24 using namespace std;
25 const int maxn = 1010;
26 int d[maxn],dp[2][40];
27 int main() {
28     int T,W;
29     while(~scanf("%d %d",&T,&W)) {
30         for(int i = 1; i <= T; ++i)
31             scanf("%d",d+i);
32         memset(dp,0,sizeof(dp));
33         for(int i = 1; i <= T; ++i)
34             for(int j = W+1; j > 0; --j) {
35                 if(d[i] == 1) {
36                     dp[0][j] = max(dp[0][j]+1,dp[1][j-1]+1);
37                     dp[1][j] = max(dp[0][j-1],dp[1][j]);
38                 }else{
39                     dp[1][j] = max(dp[1][j]+1,dp[0][j-1]+1);
40                     dp[0][j] = max(dp[0][j],dp[1][j-1]);
41                 }
42             }
43         printf("%d\n",max(dp[0][W+1],dp[1][W+1]));
44     }
45     return 0;
46 }
View Code

 

POJ 2385 Apple Catching

原文:http://www.cnblogs.com/crackpotisback/p/4321651.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!