hdu1025 最长上升子序列 (nlogn)

 水,坑。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 500010;
struct node
{
    int x;
    int y;
}a[MAXN];
int s[MAXN];
bool cmp(node t1,node t2)
{
    return t1.x<t2.x;
}

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] && v < s[m])
            return m;
        else if(v > s[m])
            l = m + 1;
        else r = m - 1;
    }
    return l;
}
int main()
{
    int n, i, j, ff=0;
    while(scanf("%d",&n)!=EOF)
    {
        memset(s, 0, sizeof(s));
        for(i = 1; i <= n; i++)
            scanf("%d%d",&a[i].x,&a[i].y);
        sort(a + 1, a + n + 1, cmp);
        int len = 1;
        s[1] = a[1].y;
        for(i = 2; i <= n; i++)
        {
            int t = serch(1, len, a[i].y);
            if(t > len)
            {
                len ++;
                s[len] = a[i].y;
            }
            else s[t]=a[i].y;
        }
        printf("Case %d:\n", ++ ff);
        if(len==1)
            printf("My king, at most %d road can be built.\n",len);
        else
            printf("My king, at most %d roads can be built.\n",len);
        cout<<endl;
    }
}

 

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