HDU 1025 Constructing Roads In JGShining's Kingdom

题意:给你N组数,每组数有两个p,r。问在这2n个点中,连线,求最多可以连好多线在不相交的情况下。

分析:把p排序,求r的最长上升子序就行了

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e6+10;
int a[maxm];
int dp[maxm];
int n;
struct node
{
    int x,y;
}t[maxm];
int cmp(node p,node q)
{
    if(p.x==q.x)
    {
        return p.y<q.y;
    }
    else
    {
        return p.x<q.x;
    }
}
int main()
{
    int k=1;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&t[i].x,&t[i].y);
        }
        sort(t+1,t+n+1,cmp);
        dp[1]=t[1].y;
        int len=1;
        for(i=2;i<=n;i++)
        {
            if(t[i].y>dp[len])
            {
                dp[++len]=t[i].y;
            }
            else
            {
                j=upper_bound(dp+1,dp+len+1,t[i].y)-dp;
                dp[j]=t[i].y;
            }
        }
        printf("Case %d:\n",k++);
        if(len==1)
        {
            printf("My king, at most 1 road can be built.\n\n");
        }
        else
        {
            printf("My king, at most %d roads can be built.\n\n",len);
        }
    }
    return 0;
}


你可能感兴趣的:(HDU 1025 Constructing Roads In JGShining's Kingdom)