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);
    }
}


你可能感兴趣的:(dp,HDU,1025)