hdu 1025 最大上升子序列

最大上升子序列 二分

参考解释http://blog.sina.com.cn/s/blog_7d3ee9f501012q83.html

#include<stdio.h>
const int N = 500005;
int num[N], ans[N];
int main()
{
    int n,k=1;
    int len,low,up,mid;
    int i, temp1,temp2;
    while(~scanf("%d", &n))
    {
        for( i = 0 ; i < n ; i++)
        {
            scanf("%d%d",&temp1,&temp2);
            num[temp1]=temp2;
        }
        ans[1]=num[1];
        len =1;
        for( i = 2; i <= n ;i++)
        {
            low = 1;
            up = len ;
            while( low <= up)
            {
                mid = (low + up)>>1;
                if(ans[mid]< num[i])low = mid +1;
                else up = mid -1;
            }
            ans[low]= num[i];
            if(low > len)len ++;
        }
          printf("Case %d:\n",k);
        if(len==1)    printf("My king, at most 1 road can be built.");
        else    printf("My king, at most %d roads can be built.",len);
        printf("\n");
        printf("\n");
        k++;
    }
}
View Code

 

posted on 2013-07-31 16:14  blieveboy  阅读(55)  评论(0编辑  收藏  举报

导航