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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值