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

Constructing Roads In JGShining's Kingdom

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 500005

int n;
int bakup[N];
struct node {
    int p;
    int r;
} a[N];

int dp[N];
int cmp(node b, node c) {
    return b.p < c.p;
}

void LIS() {
    //dp[i]表示前i个数字中加上第i个数字后,有几个数字是递增的,即前i个数字中有多少个比第i个数字小的+1
    //int bakup[N];
    memset(bakup, 0, sizeof(bakup));
    memset(dp, 0, sizeof(dp));
    int top = 0;

    bakup[top] = 0xbfbfbfbf;
    for(int i = 1; i <= n; i++) {
        //如果a[i]>栈顶部元素,则压栈
        if(a[i].r > bakup[top]) {
            bakup[++top] = a[i].r;
            dp[i] = top;
        }

        //如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素
        else {
            int l = 1, r = top;
            while(l <= r) {
                int mid = (l+r) >> 1;
                if(a[i].r > bakup[mid]) {
                    l = mid + 1;
                } else
                    r = mid - 1;
            }
            //替换a[i]
            bakup[l] = a[i].r;
            dp[i] = l;
        }
    }
}

int main() {

    //freopen("data.in", "r", stdin);
    int cas = 0;
    while(scanf("%d", &n) != EOF) {
        cas++;
        memset(a, 0, sizeof(a));

        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &a[i].p, &a[i].r);
        }

        sort(a+1, a+n+1, cmp);
        LIS();

        int maxx = 0xbfbfbfbf;
        for(int i = 1; i <= n; i++) {
            if(dp[i] > maxx)
                maxx = dp[i];
        }

        if(maxx == 1) {
            printf("Case %d:\n", cas);
            printf("My king, at most %d road can be built.\n\n",maxx);
        } else {
            printf("Case %d:\n", cas);
            printf("My king, at most %d roads can be built.\n\n",maxx);
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值