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.
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.
题目大意:
有一号树和二号树两棵树以及一头牛,起初牛在1号树下,每秒钟两棵树中的某一棵会掉下一颗苹果,牛最多能在这两棵树间移动W次,求N秒钟内牛最多能接到多少颗苹果。(牛在初始时也可以消耗一次移动次数进而移动到二号树下)
解题思路:
起初我按照DFS思路解题的,但是由于数据量较大,会超时,后来发现这是一道典型的动态规划题目
动态规划方程:
//dp[i][j]是指在i时间时用了j次移动所获得的苹果数目
//tmp是用来判定位置i上有没有接到苹果
//现在移动次数为j,可能是从j-1步移动过来的,也可能是和上一步一样没有动
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + tmp;
AC代码
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[1001][31];//dp[i][j]是指在i时间时用了j次移动所获得的苹果数目
int map[1001];
int main() {
int n, t, tmp; dp[0][0] = 0;
while (cin >> n >> t) {
for (int i = 1; i <= n; i++) {
scanf("%d", &map[i]);
if (map[i] == 1) { tmp = 1; }
else tmp = 0;
dp[i][0] = dp[i-1][0] + tmp;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= t; j++) {//j是移动的次数
if (j % 2 == 0) {//移动次数为偶数,位置为1
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + 2 - map[i];
}
else {//移动次数为奇数,位置为2
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + map[i] - 1;
}
}
}
int ans = 0;
for (int j = 0; j <= t; j++) { ans = max(ans, dp[n][j]); }
cout << ans << endl;
}
}
顺便贴上DFS思路的代码,尽管会超时 (⊙_⊙;) 哈哈
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int dp[1001][31];
int map[1001];
int n, t;
int ans;
void dfs(int l, int time, int pos, int tmp) {
//cout << "l " << l << "time "<<time<< "pos "<<pos<<"tmp "<<tmp<< endl;
if (l > n) { ans = max(ans, tmp); return; }
if (map[l] == pos) {
if (time > 0) { dfs(l + 1, time - 1, (pos + 1) % 2, tmp + 1); }//换位置
dfs(l + 1, time, pos, tmp + 1);//不换位置
}
if (map[l] != pos) {
if (time > 0) { dfs(l + 1, time - 1, (pos + 1) % 2, tmp); }//换位置
dfs(l + 1, time, pos, tmp);//不换位置
}
return;
}
int main() {
while (cin >> n >> t) {
ans = 0;
for (int i = 1; i <= n; i++) { scanf("%d", &map[i]); map[i] %= 2; }
dfs(1, t, 1, 0);
//bfs(1, t - 1, 0, 0);
cout << ans << endl;
}
}