(HDU1025)

Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18804    Accepted Submission(s): 5311

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 call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.

Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II.

The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones.

But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.

For example, the roads in Figure I are forbidden.

In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^

 
Input
Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.
 
Output
For each test case, output the result in the form of sample. 
You should tell JGShining what's the maximal number of road(s) can be built. 
 
Sample Input
2
1 2
2 1
3
1 2
2 3
3 1
 
Sample Output
Case 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads can be built.

 题意:上面n个点,下面n个点,然后在这2n个点之间随意连线,一个点只能被连一次,问最多有多少条线不交叉。

方法一:upper_bound()容器

 #include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
int a[M],b[M],n;
int finde()
{
int t=;
b[t]=a[];
t++;
for(int i=;i<=n;i++)
{
int id=upper_bound(b,b+t,a[i])-b;
//在b数组中弹出比ai大的最左边的元素,然后返回下标,否则返回last的下标
if(id==t)
t++;
b[id]=a[i];
}
return t;
}
int main()
{
int kk=;
while(scanf("%d",&n)!=-)
{ for(int i=;i<=n;i++)
{
int k,p;
scanf("%d%d",&k,&p);
a[k]=p;
}
int leng=finde();
printf("Case %d:\n",kk++);
if(leng==)
cout<<"My king, at most "<< leng <<" road can be built."<<endl;
else
cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
cout<<endl;
}
return ;
}

方法二:二分查找

 #include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
int a[M],b[M],c[M],n;
int finde(int n,int k)
{
int l=;
int r=n;
while(l<=r)
{
int mid=(l+r)/;
if(c[mid]<k)
l=mid+;
else
r=mid-;
}
return l;
}
int main()
{
int kk=;
while(scanf("%d",&n)!=-)
{ for(int i=;i<=n;i++)
{
int k,p;
scanf("%d%d",&k,&p);
a[k]=p;
}
memset(c,inf,sizeof(c));
b[]=;
c[]=a[];
for(int i=;i<=n;i++)
{
int id=finde(n,a[i]);
c[id]=a[i];
b[i]=id;
}
int leng=;
for(int i=;i<=n;i++)
leng=max(leng,b[i]); printf("Case %d:\n",kk++);
if(leng==)
cout<<"My king, at most "<< leng <<" road can be built."<<endl;
else
cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
cout<<endl;
}
return ;
}

 方法三:模拟upper_bound

 #include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
int a[M],b[M],c[M],n;
int binary_find(int n,int k)
{
int l=;
int r=n-;
while(l<=r)
{
int mid=(l+r)/;
if(b[mid]>=k)
r=mid-;
else
l=mid+;
}
return l;
}
int fun(int n)
{
int t=;
b[t]=a[];
t++;
for(int i=;i<=n;i++)
{
int id=binary_find(t,a[i]);
if(id==t)
t++;
b[id]=a[i];
}
return t;
}
int main()
{
int kk=;
while(scanf("%d",&n)!=-)
{ for(int i=;i<=n;i++)
{
int k,p;
scanf("%d%d",&k,&p);
a[k]=p;
}
int leng=fun(n);
printf("Case %d:\n",kk++);
if(leng==)
cout<<"My king, at most "<< leng <<" road can be built."<<endl;
else
cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
cout<<endl;
}
return ;
}

最长上升子序列(N*log(N))hdu1025的更多相关文章

  1. 【51NOD-0】1134 最长递增子序列

    [算法]动态规划 [题解]经典模型:最长上升子序列(n log n) #include<cstdio> #include<algorithm> #include<cstr ...

  2. 最长下降子序列O(n^2)及O(n*log(n))解法

    求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...

  3. 最长上升子序列的变形(N*log(N))hdu5256

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. hdu1025 dp(最长上升子序列LIS)

    题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...

  5. Bridging signals---hdu1950(最长上升子序列复杂度n*log(n) )

     题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 一直只知道有除n*n的算法之外的求LIS,但是没学过,也没见过,今天终于学了一下,dp[i]表 ...

  6. 最长上升子序列(LIS)的n*log(n)求法

    方法: 对于某个序列,设一个数组,将序列第一个数放入,然后再一个一个判断序列下一位,如果大于当前数组的末尾元素,则加入数组,否则利用二分法找到第一个大于等于当前数的元素并替换,最后这个数组的长度len ...

  7. O(n log n)求最长上升子序列与最长不下降子序列

    考虑dp(i)表示新上升子序列第i位数值的最小值.由于dp数组是单调的,所以对于每一个数,我们可以二分出它在dp数组中的位置,然后更新就可以了,最终的答案就是dp数组中第一个出现正无穷的位置. 代码非 ...

  8. BZOJ 3173: [Tjoi2013]最长上升子序列

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1524  Solved: 797[Submit][St ...

  9. LCS最长公共子序列(最优线性时间O(n))

    这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...

  10. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

随机推荐

  1. 关于C和C++动态链接库的几个问题

    问题: 1.写一段C++程序,编译成动态链接库后,C程序怎么访问? 2.写一段C程序,编译成动态链接库后,C++程序怎么访问? 3.写一个类,编译成动态链接库后,里面的public变量能否访问? 对于 ...

  2. backbone-todo案例分析

    todo案例可以到这个地址下载 https://github.com/jashkenas/backbone 添加数据后 todo案例不涉及Router,仅有Model.Collection.View的 ...

  3. mysql select 练习题

    10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree  in(select max(degree) f ...

  4. oracle常用的SQL语句

    一些常用的SQL语句: --建表 create table adolph (id number(10,0),              name varchar2(20),              ...

  5. Haskell解决逆波兰式

    摘自<Haskell趣学指南- Learn You a Haskell for Great Good> {- 逆波兰式(revese polish notation, RPN): 操作符出 ...

  6. ”靠谱的C#“单例模式

    //静态构造函数的单例模式 public sealed class Singleton { private static readonly Singleton _instance = new Sing ...

  7. (leetcode)Reverse Linked List 脑子已经僵住

    Reverse a singly linked list. 参考http://www.2cto.com/kf/201110/106607.html 方法1: 讲每个节点的指针指向前面就可以. /** ...

  8. C# HttpWebRequest 绝技

    http://www.sufeinet.com/thread-6-1-1.html 万能框架.分布式......

  9. SQL Server select 将类型相同的行合并,并将对应金额相加

    select Category,REPLACE(sum(Amount),'-','')  as Amountfrom T_Detail WHERE CREATED_BY='6123EC14-50E2- ...

  10. java第一课堂

    java第一课堂视频请在优酷搜索java第一课堂即可