专题训练第四周——基础DP

题目
  • HDU 1024 Max Sum Plus Plus
  • HDU 1029 Ignatius and the Princess IV
  • HDU 1069 Monkey and Banana
  • HDU 1074 Doing Homework
  • HDU 1087 Super Jumping! Jumping! Jumping!
  • HDU 1114 Piggy-Bank
  • HDU 1176 免费馅饼
  • HDU 1260 Tickets
  • HDU 1257 最少拦截系统
  • HDU 1160 FatMouse’s Speed
  • POJ 1015 Jury Compromise
  • POJ 1458 Common Subsequence
  • POJ 1661 Help Jimmy
  • POJ 2533 Longest Ordered Subsequence
  • POJ 3186 Treats for the Cows
  • HDU 1078 FatMouse and Cheese
  • HDU 2859 Phalanx
  • POJ 3616 Milking Time
  • POJ 3666 Making the Grade
HDU 1257 最少拦截系统&& POJ 2533 Longest Ordered Subsequence

拦截系统后一发的炮弹不能比前面的高,最开始想的是贪心,从后往前历遍一个子序列就设置一个拦截系统,但是这个思路8行
DP :在i的位置是否比i小的目标,如果有,那么相较于这个位置就要多设置一个拦截系统。

AC代码
#include<algorithm>
const int MAXN=3e4+5;
using namespace std;
int dp[MAXN],a[MAXN];
//最长上升子序列 
int main()
{
	int n;
	while(cin>>n){
		int ans=0;
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<n;i++) dp[i]=1;
		for(int i=0;i<n;i++){
			for(int j=0;j<i;j++)
				if(a[j]<a[i])  dp[i]=max(dp[j]+1,dp[i]);
			
			ans=max(ans,dp[i]);
		}
		cout<<ans<<endl;
	}
	return 0;
 } 
HDU 1176 免费馅饼

当前时间当前位置的数量与前一时间前一位置的数量有关系
我们可以把馅饼的位置和时间用数组储存,这样就转化成了数组问题
状态转移方程:
dp[i][j]+=max( dp[i+1][j+1] , max ( dp[i+1][j] , dp[i+1][j-1] ) )

AC代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> 
using namespace std;
int dp[100001][12];
int maxn(int a,int b,int c)
{
	int max1;
	max1=a>b?a:b;
	max1=max1>c?max1:c;
	return max1;
}
int main()
{
	int n,x,t;
	while(scanf("%d",&n)!=EOF&&n)
	{
		int m=0;
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&x,&t);
			dp[t][x]++;
			m=max(t,m);
		}
		for(int i=m-1;i>=0;i--)
		{
			for(int j=0;j<=10;j++)
			dp[i][j]+=maxn(dp[i+1][j+1],dp[i+1][j],dp[i+1][j-1]);
		} 
		printf("%d\n",dp[0][5]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值