poj2533

最长递增子序列

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;

#define maxn 40004

int n;
int f[maxn];
int d[maxn];
int m;

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < n ;i++)
        scanf("%d", &f[i]);
}

int binarysearch(int a)
{
    int l = 0;
    int r = m;
    while (l < r)
    {
        int mid = (l + r) / 2;
        if (d[mid] < a)
            l = mid + 1;
        else
            r = mid;
    }
    if (l >= m)
        m++;
    return l;
}

int work()
{
    m = 0;
    for (int i = 0; i < n; i++)
        d[binarysearch(f[i])] = f[i];
    return m;
}

int main()
{
    input();
    printf("%d\n", work());
    return 0;
}
View Code

 

posted @ 2013-06-12 16:50  undefined2024  阅读(98)  评论(0编辑  收藏  举报