POJ_2385_Apple Catching_简单DP
2014-11-15 20:19
295 查看
听了一天古典钢琴曲真不错
题意:
农夫约翰又养了一头极吊的牛Bessie,他喜欢吃苹果,有两棵苹果树,从1到T秒,每分钟其中一棵树会掉苹果,Bessie每分钟站在其中一棵树下,如果此时掉苹果,就能吃到,他最多移动W次,移动不算时间,问T分钟后他最多吃到多少个苹果。
Input
* Line 1: Two space separated integers: T(1000) and W(30)
* 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.
如果用搜索,那么至少有2^w个状态,肯定TLE,动态规划,记录当前时间、当前位置、当前移动次数即可
代码如下:
题意:
农夫约翰又养了一头极吊的牛Bessie,他喜欢吃苹果,有两棵苹果树,从1到T秒,每分钟其中一棵树会掉苹果,Bessie每分钟站在其中一棵树下,如果此时掉苹果,就能吃到,他最多移动W次,移动不算时间,问T分钟后他最多吃到多少个苹果。
Input
* Line 1: Two space separated integers: T(1000) and W(30)
* 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.
如果用搜索,那么至少有2^w个状态,肯定TLE,动态规划,记录当前时间、当前位置、当前移动次数即可
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> using namespace std; #define mxt 1010 #define mxw 40 int t,w; int a[mxt]; int dp[2][mxt][mxw]; int main(){ scanf("%d%d",&t,&w); for(int i=1;i<=t;++i) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); int ans=0; for(int j=1;j<=t;++j) for(int i=0;i<2;++i) for(int k=0;k<=min(j-1,w);++k){ dp[i][j][k]=dp[i][j-1][k]+(a[j]==i+1 ? 1 : 0); if(k-1>=0) dp[i][j][k]=max(dp[i][j][k],dp[1-i][j-1][k-1]+(a[j]==i+1 ? 1 : 0)); ans=max(ans,dp[i][j][k]); } printf("%d\n",ans); return 0; }
相关文章推荐
- Apple Catching (poj 2385 简单dp)
- POJ2385-Apple Catching-简单dp
- poj 2385 Apple Catching 简单DP
- poj 2385Apple Catching(简单dp)
- POJ 2385 Apple Catching (DP)
- POJ 2385-Apple Catching (基础DP)
- Apple Catching poj 2385(简单dp)
- POJ 2385 - Apple Catching [DP]
- DP:Apple Catching(POJ 2385)
- POJ 2385 Apple Catching 接苹果 DP
- poj 2385 Apple Catching (DP)
- [POJ 2385]Apple Catching[DP]
- poj 2385 Apple Catching 经典dp
- poj--2385--Apple Catching(状态dp)
- POJ 2385 Apple Catching (DP)
- POJ - 2385 Apple Catching (DP)
- POJ 2385 Apple Catching(DP)
- POJ 2385 Apple Catching (dp)
- POJ_2385 Apple Catching(DP)
- poj 2385 Apple Catching(dp)