hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)

pid=1025">题目链接

第一次写nlogn复杂度的LIS,纪念一下。

题目意思是说。有两条平行线,两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市。输入n,接下来有n行,p,r表示穷城市p和富有城市r

之间能够建一条路(p的顺序是1--n,一个贫穷城市仅仅相应一个富有城市(弱爆的语文描写叙述能力T_T))。公路不能交叉。

问最多能够建多少条公路。

在别处看到的对nlogn解法的解释吧算是:

时间复杂度:(NlogN):

除了算法一的定义之外。添加一个数组b,b[i]用以表示长度为i最长子序列的最后一个数最小能够是多少。易证:i<j时。b[i]<b[j]。

在二分查找时,一直更新b[]内容。设此时b的总长度为k,

若1. arr[i] >= b[k], 则b[k+1] = arr[i];

若2. arr[i] < b[k], 则在b[1..k]中用二分搜索大于arr[i]的最小值,返回其位置pos。然后更新b[pos]=arr[i]。

code例如以下(自己手写了一个二分,又用了下STL里面upper_bound。都能够):

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
//#define local
using namespace std;
const int maxn=500010;
int poor[maxn],ro[maxn];
int n;
int Binary_search(int x,int k)
{
    int low=1,high=k;
    while(low<=high)
    {
        int mid=(low+high)/2;
        if(ro[mid]<=x)
            low=mid+1;
        else
            high=mid-1;
    }
    return low;
}
int lis()
{
    int k=1;
    ro[k]=poor[1];
    for(int i=2;i<=n;i++)
    {
        if(poor[i]>=ro[k])
            ro[++k]=poor[i];
        else
        {
            //int pos=Binary_search(poor[i],k);
            int pos=upper_bound(ro+1,ro+k+1,poor[i])-(ro);
            ro[pos]=poor[i];
        }
    }
    return k;
}
int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // local
    int cnt=0;
    while(~scanf("%d",&n))
    {
        int x,y;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            poor[x]=y;
        }
        int ans=lis();
        printf("Case %d:\n",++cnt);
        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);
    }
    return 0;
}

hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)

时间: 2024-10-23 04:52:57

hdu1025 Constructing Roads In JGShining&#39;s Kingdom (nlogn的LIS)的相关文章

hdu1025 Constructing Roads In JGShining&amp;#39;s Kingdom(二分+dp)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities a

HDU 1025 Constructing Roads In JGShining&amp;#39;s Kingdom (DP)

Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we ca

hdoj 1025 Constructing Roads In JGShining&amp;#39;s Kingdom 【最长递增子序列】

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16262    Accepted Submission(s): 4633 Problem Description JGShining's kingdom consists of 2n(n is no mo

HDU ACM 1025 Constructing Roads In JGShining&amp;#39;s Kingdom-&amp;gt;二分求解LIS+O(NlogN)

#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; //dp[i]保存的是长度为i的最长不降子序列的最小尾元素 int BS(int n,int x) //二分查找下标,当x比全部元素小时下标为1,比全部元素大时下标为n+1. { int low,high,mid; low=1,high=n; while(low<=high) { mid=(low+h

LIS问题---HDU1025 Constructing Roads In JGShining&#39;s Kingdom

发现这个说的比较通俗: 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.下面一步一步试着找出它.我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列.此外,我们用一个变量Len来记录现在最长算到多少了首先,把d[1]有序地放到B里,令B[1] = 2,就是说当只有1一个数字2的时候,长度为1的LIS的最小末尾是2.这时Len=1然后,把d[2]有序地放到B里,令B[1] = 1,就是说长度为1的LIS的最小末尾是1,d[1]=2

hdu-1025 Constructing Roads In JGShining&#39;s Kingdom(二分查找)

题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21045    Accepted Submission(s): 5950 Problem Description JGShining's kingdom consists of 2n(n is

hdu1025 Constructing Roads In JGShining&#39;s Kingdom

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities a

hdu1025 Constructing Roads In JGShining&#39;s Kingdom (nlogn的LIS)

题目链接 第一次写nlogn复杂度的LIS,纪念一下. 题目意思是说.有两条平行线,两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市.输入n,接下来有n行,p,r表示穷城市p和富有城市r 之间可以建一条路(p的顺序是1--n,一个贫穷城市只对应一个富有城市(弱爆的语文描述能力T_T)),公路不能交叉. 问最多可以建多少条公路. 在别处看到的对nlogn解法的解释吧算是: 时间复杂度:(NlogN): 除了算法一的定义之外,增加一个数组b,b[i]用以表示长度

HDU-1025 Constructing Roads In JGShining&#39;s Kingdom O(nlogn)的最长上升子序列

模板题,唯一问题是当长度为1是,road是单数,不然road是复数roads. #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> using namespace st