HDU 1025 Constructing Roads In JGShining's Kingdom(LIS)

Description
河岸两旁分别有n个村庄,他们之间要互相修路,并且同一边的不互相修,在保证不交叉的情况下,最多能修多少条路
Input
多组输入,每组用例第一行为一整数n表示村庄数,之后n行每行两个整数a和b表示要从河岸一旁的a村庄到河岸另一旁的b村庄修路,以文件尾结束输入
Output
对于每组用例,输出最多能修多少条路
Sample Input
2
1 2
2 1
3
1 2
2 3
3 1
Sample Output
Case 1:
My king, at most 1 road can be built.

Case 2:
My king, at most 2 roads can be built.

Solution
最长上升子序列,模版题
Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<string>
using namespace std;
#define maxn 511111
#define INF 1<<29
int dp[maxn],n;
int get_lower_bound(int x)
{
    int l=0,r=n-1;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(dp[mid]>=x)
            r=mid-1;
        else
            l=mid+1;
    }
    return l;
}
int LIS(int a[])
{
    for(int i=0;i<n;i++)
        dp[i]=INF;
    for(int i=0;i<n;i++)
        dp[get_lower_bound(a[i])]=a[i];
    return get_lower_bound(INF);
}
int a[maxn];
int main()
{
    int res=1;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[x-1]=y;
        }
        int ans=LIS(a);
        printf("Case %d:\n",res++);
        if(ans==1)
            printf("My king, at most 1 road can be built.\n\n");
        else
            printf("My king, at most %d roads can be built.\n\n",ans);
    }
}

你可能感兴趣的:(HDU 1025 Constructing Roads In JGShining's Kingdom(LIS))