题目链接:http://poj.org/problem?id=3903
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4263 | Accepted: 1523 |
Description
Input
Output
Sample Input
6 5 2 1 4 5 3 3 1 1 1 4 4 3 2 1
Sample Output
3 1 1
Hint
Source
思路:(1)如果n的规模比较小那么可以用朴素的O(n*n)的dp算法求解;(2)但是这里n的规模比较大,朴素算法会超时,得用另一种O(n*logn)的算法;参考自:http://blog.sina.com.cn/s/blog_76344aef0100scyq.html
附上代码:
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <stdio.h>
const int maxn=100100;
const int INF=99999999;
using namespace std;
int a[maxn];
int d[maxn];
int find(int l,int r,int key)
{
if(l==r)return l;
int mid=(l+r)/2;
if(key>d[mid])return find(mid+1,r,key);//这里就把a[]当作d[]wrong了好多次
else return find(1,mid,key);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)scanf("%d",&a[i]);
int len=0;
d[0]=-INF;
for(int i=0;i<n;i++)
{
if(a[i]>d[len])d[++len]=a[i];
else
{
int j=find(1,len,a[i]);
d[j]=a[i];
}
}
printf("%d\n",len);
}
return 0;
}
原文地址:http://blog.csdn.net/liusuangeng/article/details/38899155