hdu1025(dp+二分)

参考自:http://blog.csdn.net/ice_crazy/article/details/7536332

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int n,s[999999],head[999999];
int fun()
{
    int len,i,mid,l,r;
    memset(head,0,sizeof(head));
    head[1]=s[1];
    len=1;
    for (i=2;i<=n;++i)//采用二分查找
    {
        l=1;
        r=len;
        while(l<=r)
        {
            mid=(l+r)/2;
            if (s[i]>head[mid])//从中开始比较并插入
            l=mid+1;
            else
            r=mid-1;
        }
        head[l]=s[i];//把小元素放入,但未改变现在所得到的子序列,其中head数组是动态变化的
        if (l>len)//当head数组的长度变大后,更新最大长度
        len=l;
    }
    return len;
}
int main()
{
    int k=1,a,b,i,ans;
    while(scanf("%d",&n)!=EOF)
    {
        for (i=1;i<=n;++i)
        {
            scanf("%d%d",&a,&b);
            s[a]=b;//建立一一对应关系,抽象成最长子序列模型
        }
        int ans=fun();
        printf ("Case %d:\n",k++);
        if (ans==1)
        printf ("My king, at most 1 road can be built.\n\n");
        else//大于1时,后面要加s
        printf ("My king, at most %d roads can be built.\n\n",ans);
    }
    return 0;
}

 

posted @ 2013-05-02 22:30  一线添  阅读(130)  评论(0编辑  收藏  举报