码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ1025(最长上升子序列)

时间:2015-07-31 18:12:15      阅读:164      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!