1、POJ - 2533 Longest Ordered Subsequence
题目链接:http://poj.org/problem?id=2533
标准DP处理最长上升子序列问题
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[1100];
int dp[1100];
int maxv;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
dp[i]=1;
for(int j=1;j<i;j++)
if(a[i]>a[j])
dp[i]=max(dp[i],dp[j]+1);
maxv=max(maxv,dp[i]);
}
cout<<maxv<<endl;
return 0;
}
2、HDU1160 FatMouse’s Speed
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
二维的最长上升和最长下降子序列问题,先将第一维标准进行排序,然后对第二维标准进行最长上升/下降子序列处理即可。
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<functional>
#include<algorithm>
#include<stdlib.h>
using namespace std;
struct node{
int w;
int v;
int pos;
int pre;
bool operator < (node b){
return w==b.w? v>b.v:w<b.w;
}
}mice[1100];
int n=1;
int dp[1100];
void print(int x){
if(x==0)
return ;
print(mice[x].pre);
printf("%d\n",mice[x].pos);
return ;
}
int main(){
while(scanf("%d%d",&mice[n].w,&mice[n].v)!=EOF)
mice[n].pos=n,n++;
n--;
sort(mice+1,mice+n+1);
for(int i=1;i<=n;i++){
dp[i]=1;
for(int j=1;j<i;j++){
if(mice[i].w>mice[j].w&&mice[i].v<mice[j].v){
if(dp[i]<dp[j]+1){
mice[i].pre=j;
dp[i]=dp[j]+1;
}
}
}
}
int pos=0;
dp[0]=0;
for(int i=2;i<=n;i++)
if(dp[pos]<=dp[i])
pos=i;
cout<<dp[pos]<<endl;
print(pos);
return 0;
}
3、HDU - 1257 最少拦截系统
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257
这题利用的是最长上升/下降子序列的一个性质,最长上升子序列长度=不下降子序列个数,最长下降子序列长度=不上升子序列个数
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<functional>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int n;
int dp[33333];
int a[33333];
int ans;
int main(){
while(scanf("%d",&n)!=EOF){
memset(dp,0,sizeof(dp));
ans=1;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
dp[i]=1;
for(int j=1;j<i;j++)
if(a[i]>a[j]){
dp[i]=max(dp[i],dp[j]+1);
ans=max(ans,dp[i]);
}
}
printf("%d\n",ans);
}
return 0;
}