题目:
Apple Catching
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14359 | Accepted: 7031 |
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.
题意:有两个苹果树,和T秒时间。每一秒都将有会有其中一棵苹果树掉落一个苹果。Bessie初始站在第1棵树下,若一棵树掉落了一个苹果,他只有在那棵树下时,才能接到。他共有w次移动的机会,问他最多可以接到多少苹果。
思路:dp[i][j]表示第i秒时,移动了j次,能接到苹果的最大值。则状态转移为:dp[i][j] = max(dp[i-1][j] , dp[i-1][j-1]) ,当然,若移动j次后,在第i秒停留的树掉落了一棵苹果,我们还要在+1。
如何判断移动j次后在哪棵树下?由于初始时在树1,那么移动奇数次一定在树2下,移动偶数次一定在树1下。
还有需要补充的初始化要分情况:如果在第一秒钟是第一棵树落苹果,那么dp[1][0]=1 , dp[1][1] = 0 ; 如果在第一秒钟第二棵树落苹果,那么dp[1][0] = 0 , dp[1][1] = 1.
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define eps 1.0E-8
// #define Reast1nPeace
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;
int a[1010];
int dp[1010][35];
int main(){
#ifdef Reast1nPeace
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
int t,w;
cin>>t>>w;
for(int i = 1 ; i<=t ; i++){
cin>>a[i];
}
if(a[1]==1){
dp[1][0] = 1 ; dp[1][1] = 0;
}
else{
dp[1][0] = 0 ; dp[1][1] = 1;
}
for(int i = 2 ; i<=t ; i++){
for(int j = 0 ; j<=w ; j++){
if(j==0){
dp[i][j] = dp[i-1][j] + a[i]%2;
}
else{
dp[i][j] = max(dp[i-1][j] , dp[i-1][j-1]) + (a[i]==j%2+1? 1:0);
}
}
}
int maxn = 0;
for(int i = 0 ; i<=35 ; i++){
maxn = max(maxn , dp[t][i]);
}
cout<<maxn<<endl;
return 0;
}