hdu1025

#include<stdio.h>
const int MAXN=500010;
int a[MAXN],b[MAXN];

//用二分查找的方法找到一个位置,使得num>b[i-1] 并且num<b[i],并用num代替b[i]
int Search(int num,int low,int high)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(num>=b[mid]) low=mid+1;
else high=mid-1;
}
return low;
}
int DP(int n)
{
int i,len,pos;
b[1]=a[1];
len=1;
for(i=2;i<=n;i++)
{
if(a[i]>=b[len])//如果a[i]比b[]数组中最大还大直接插入到后面即可
{
len=len+1;
b[len]=a[i];
}
else//用二分的方法在b[]数组中找出第一个比a[i]大的位置并且让a[i]替代这个位置
{
pos=Search(a[i],1,len);
b[pos]=a[i];
}
}
return len;
}
int main()
{

int n;
int iCase=0;
int i,j;
int x,y;
while(scanf("%d",&n)!=EOF)
{
iCase++;
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
a[x]=y;
}
int res=DP(n);
printf("Case %d:\n",iCase);
if(res==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",res);
}
return 0;
}

时间: 2024-10-14 23:11:43

hdu1025的相关文章

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——LIS——Constructing Roads In JGShining&#39;s Kingdom

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

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

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]用以表示长度

ACM.hdu1025

to get the ans of how many roads at most that can be built between two line without intersection of roads,we need sort the input sequence at ont edge and then deal with anther line with LIS way,meanwhile, not forget using the nlogn way. 1 #include<io

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

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

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&#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