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

这题读懂了就是一个LIS问题, 早就在刘汝佳的大白书上见过LIS的O(n*logn)的方法,这次算是碰上模板题了。。。其中STL的( lower_bound(array, array+n, key)-array )返回array数组中array[i]大于等于key的最小i。不过这题的输出很坑人。。。ans=1的时候是“road”,其他是“roads”。。。

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

const int maxn = 500001;
const int INF = 1e9;
int s[maxn], g[maxn], d[maxn], n, ans;

int main()
{
    int kase = 0;
    while(~scanf("%d", &n))
    {
        int a, b;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d", &a, &b);
            s[--a] = --b;
        }
        //计算s[n]的LIS, g[n]和d[n]辅助
        for(int i=1; i<=n; i++) g[i] = INF;
        ans = 0;
        for(int i=0; i<n; i++)
        {
            int k = lower_bound(g+1, g+n+1, s[i]) - g;
            d[i] = k;
            g[k] = s[i];
            ans = max(ans, d[i]);
        }
        printf("Case %d:\n", ++kase);
        if(ans == 1)
            printf("My king, at most %d road can be built.\n\n", ans);
        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、付费专栏及课程。

余额充值