HDU1025---(LIS 最长上升子序列 的应用)

分析:

n行

每行包含两个整数p r;意思是p从到r

不能有交叉的路

p刚好从1->n,

可看做下标,到的地方看做值

就转化为了最长上升子序列的问题

此题难点,怎么将其转化为LIS问题

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int inf=0x7fffffff;
const int maxn=500005;
int road[maxn];
int dp[maxn];

int main()
{
    int n;
    int from,to;
    int c=1;
    while(scanf("%d",&n)!=EOF)
    {

        fill(dp,dp+n,inf);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&from,&to);
            road[from]=to;
        }
        for(int i=1;i<=n;i++)//因为题目输入的原因,这里的下标从1开始。
            *lower_bound(dp,dp+n,road[i])=road[i];
        int len=lower_bound(dp,dp+n,inf)-dp;
        if(len==1)
        {
            cout<<"Case "<<c++<<":"<<endl;
            cout<<"My king, at most 1 road can be built."<<endl;
        }
        else
        {
            cout<<"Case "<<c++<<":"<<endl;
            cout<<"My king, at most "<<len<<" roads can be built."<<endl;
        }
        cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kimsimple/p/6661710.html