poj1887 - Testing the CATCHER(动归)

最长不上升子序列。

把最长上升子序列的代码稍微改动一下即可

详解请见:http://wenku.baidu.com/view/fe0deecea1c7aa00b52acb71.html

#include <cstdio>
#include <cstring>
#define N 10005
int n, a[N], c[N], dp[N];
int search(int l, int r, int x)
{
    int mid;
    while(l<=r)
    {
        mid = (r+l)/2;
        if(c[mid]==x) return mid;
        else if(x<c[mid]) l = mid+1;
        else r = mid-1;
    }
    return l;
}
int main()
{
    int t = 0, tt;
    while(1)
    {
        n = 0;
        while(1)
        {
            scanf("%d",&tt);
            if(tt!=-1) a[++n] = tt;
            else break;
        }
        if(n==0) break;
        c[1] = a[1]; dp[1] = 1;
        int size = 1;
        for(int i = 2, j; i <= n; i++)
        {
            if(a[i]>c[1]) j = 1;
            else if(a[i]<c[size]) j = ++size;
            else j = search(1,size, a[i]);
            c[j] = a[i];
        }
        if(t) printf("\n");
        printf("Test #%d:\n  maximum possible interceptions: %d\n",++t,size);
    }
    return 0;
}


 

posted on 2013-06-13 09:48  Primo...  阅读(147)  评论(0编辑  收藏  举报