hdu1950 最长上升子序列nlogn

 简单。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXN = 40010;
int a[MAXN],s[MAXN];
int serch(int left, int right, int v)
{
    int l, r, m;
    l = left, r = right;
    while(l <= r)
    {
        m = (l + r) / 2;
    
         if(v > s[m])
            l = m + 1;
        else r = m - 1;
    }
    return l;
}
int main()
{
    int i, j, t, n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
        }
        memset(s, 0, sizeof(s));
        s[1] = a[1];
        int len = 1;
        for(i = 2; i <= n; i++)
        {
            int ft = serch(1, len, a[i]);
            if(ft > len){
                len ++;
                s[len] = a[i];
            }
            else 
                s[ft] = a[i];
        }
        printf("%d\n", len);
    }
}

 

posted @ 2015-11-22 16:22  sweat123  阅读(228)  评论(0编辑  收藏  举报