题目
- 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;
}