poj 2385
来源:互联网 发布:淘宝如何改实名认证 编辑:程序博客网 时间:2024/06/15 09:26
Apple Catching
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6637 Accepted: 3227
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, 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.
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.
* 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 22112211
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.
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.
例如样例;
2 1 1 2 2 1 1
则app[0] =0 , app[1] =1 , app[2]=2 , app[3]=2 , app[4]=2;
如果是:
1 1 1 2 2 1 1
则app[0] =3 , app[1]=1 , app[2] = 2;
2 1 1 2 2 1 1
则app[0] =0 , app[1] =1 , app[2]=2 , app[3]=2 , app[4]=2;
如果是:
1 1 1 2 2 1 1
则app[0] =3 , app[1]=1 , app[2] = 2;
#include <iostream> #include <cstdio> #include <vector> #include <algorithm>#include <cstring>using namespace std; const int maxn = 1010; int dp[maxn][maxn] , T , W; vector<int> app; void initial(){ memset(dp , 0 , sizeof dp); app.clear(); } void readcase(){ int t = 1 , sum = 0 , a; for(int i = 0;i < T;i++){ cin >> a; if(a != t){ t = a; app.push_back(sum); sum = 0; } sum++; } app.push_back(sum); } void computing(){ /*for(int i = 0;i < app.size();i++){ cout << app[i] << endl; }*/ int sum = 0, sum0 = 0, sum1 = 0; for(int i = 0;i < app.size();i++){ sum += app[i]; if(i%2 == 0){ sum0 += app[i]; dp[i][0] = sum0; }else{ sum1 += app[i]; dp[i][1] = sum1; } dp[i][i] = sum; } if(W >= app.size()-1){ cout << sum << endl; return; } if(W == 0){ cout << 0 << endl; return; } for(int i = 3;i < app.size();i++){ for(int k = i-2;k >= 1;k-=2){ dp[i][k] = max(dp[i-1][k-1]+app[i] , dp[i-2][k]+app[i]); /*if(i == 3 && k == 1){ cout << dp[i-1][k-1]+app[i] << " " << dp[i-2][k]+app[i] << endl; }*/ } } if(W%2 == 0){ if(app.size()%2 == 0){ cout << max(dp[app.size()-2][W] , dp[app.size()-1][W-1]); }else{ cout << max(dp[app.size()-1][W] , dp[app.size()-2][W-1]); } }else{ if(app.size()%2 == 1){ cout << max(dp[app.size()-2][W] , dp[app.size()-1][W-1]); }else{ cout << max(dp[app.size()-1][W] , dp[app.size()-2][W-1]); } } cout << endl; } int main(){ while(cin >> T >> W){ initial(); readcase(); computing(); } return 0; }
- poj 2385
- poj 2385
- poj-2385
- poj 2385
- poj 2385
- poj 2385
- poj 2385
- POJ-2385
- POJ-2385
- poj 2385
- poj 2385
- poj 2385 简单dp
- Poj 2385 (dp)
- poj 2385 动态规划
- POJ 2385 Apple Catching
- POJ-2385-Apple Catching
- DP 之 poj 2385
- poj 2385 Apple Catching
- cocos2d-x无限滚动背景
- C#学习总结2013.10.24—2013.10.30
- 编译代码报出Android library projects cannot be launched错误的解决
- MySQL 编码设置
- oracle监听配置讲解
- poj 2385
- MVC学习笔记二:实现简单的增删改查
- (ios7) 解决代码布局View, ios7 中 subView 高度增加StatusBar20dp的问题,保证Ios6,ios7代码一致
- 【IO流一些小细节】IO流中一些方法的使用
- 传统社区的评论结构和百度贴吧的层级回复结构,大家觉得各有什么有缺点呢?
- OCP-1Z0-053-V12.02-50题
- Linux CentOS Mysql修改默认端口
- CSDN第一天
- 3.4 creat函数-创建文件