首页 > 其他 > 详细

HDOJ1025(最长上升子序列)

时间:2015-07-31 18:09:44      阅读:130      评论:0      收藏:0      [点我收藏+]

求最长上升子序列的二分法。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX(a,b) (a>b)?a:b
const int SIZE=500000+16;
const int INF=1000000;
struct node{
    int rich;
    int poor;
};
node road[SIZE];
int dp[SIZE];
bool cmp(node no1,node no2)
{
    return no1.poor < no2.poor;
}

int main()
{
    int n;
    int t=0;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&road[i].poor,&road[i].rich);
        }
        memset(dp,INF,sizeof(int)*n);
        sort(road,road+n,cmp);
        int ans=-1;
        for(int i=0;i<n;i++)
            *upper_bound(dp,dp+n,road[i].rich)=road[i].rich;

        ans=upper_bound(dp,dp+n,INF)-dp;

        printf("Case %d:\n",++t);
        printf("My king, at most %d %s can be built.\n",ans,(ans>1)?"roads":"road");
        printf("\n");
    }
    return 0;
}

 

HDOJ1025(最长上升子序列)

原文:http://www.cnblogs.com/program-ccc/p/4692514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!