题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1950
其实就是求最长上升子序列,不过要用nlogn的算法n*n会超时。学习最长子序列算法见博客:
1:http://blog.csdn.net/z_zhangyinqian/article/details/47980725
2:http://blog.csdn.net/z_zhangyinqian/article/details/47859617
HDU 1950代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=40005;
const int INF=100000000;
int a[maxn],dp[maxn];
int find(int l,int r,int x)
{
if(l==r) return l;
while(l<r)
{
int mid=(l+r)/2;
if(dp[mid]<x) l=mid+1;
else r=mid;
}
return l;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i]=INF;
}
int j=0,len=0;
dp[0]=-INF;
for(int i=1;i<=n;i++)
{
if(a[i]>dp[len]) j=++len;
else j=find(1,len,a[i]);
dp[j]=min(a[i],dp[j]);
}
printf("%d\n",len);
}
return 0;
}