POJ-2385 Apple Catching DP 鶸的DP解题报告

题目:

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.

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.

题意:有两个苹果树,和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;
}

POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值