HDU 1025 Constructing Roads In JGShining's Kingdom (简单dp, 最长上升子序列LIS)

  • 题目链接 : Constructing Roads In JGShining’s Kingdom
  • 题意: 有rich city和poor city 两种,分别有n个,编号均为1-n, 一个poor city只会与一个rich city连接,两种city的布局(连边不允许交叉如下

    从左到右,均为编号为1-n.
    给定边(poor city 连接 rich city),问在不相交的情况下能连接几条边.
  • 解法:
    在poor city有序的情况下,只要求rich最长上升子序列即可(nlogn求解)
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a; i<=b; ++i)
#define mp make_pair
#define pb push_back
#define ms(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int>vi;

const int maxn = 5e5+7;
int n; 
struct node{
    int a, b;
}p[maxn];
bool cmp(node a, node b){
    if(a.a != b.a) return a.a<b.a;
    return a.b < b.b; 
}
int List[maxn];
int main(){
//  freopen("in.txt", "r", stdin);
    int cas=0;
    while(~scanf("%d", &n)){   
        printf("Case %d:\n", ++cas);
        rep(i, 1, n) scanf("%d %d", &p[i].a, &p[i].b);
        sort(p+1, p+1+n, cmp);
        int ans=0;
        int cnt=1;
        List[0] = p[1].b;
        rep(i, 2, n){
            int inx = lower_bound(List, List+cnt, p[i].b)-List;
            List[inx] = p[i].b;
            if(inx == cnt) cnt++;
        }
        if(cnt==1) printf("My king, at most %d road can be built.\n", cnt);
        else printf("My king, at most %d roads can be built.\n", cnt);
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值