http://acm.hdu.edu.cn/showproblem.php?pid=1025

Constructing Roads In JGShining'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 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.
 
Hint
 

Huge input, scanf is recommended.

 
题意:输入有两边,为p,q,求的是所有p到q不相交的话最多可以有多少条边相连,那么只要从1到n枚举p,然后road[p]就是一个q,这样求road[p]的LIS就好了。
思路:直接LIS的复杂度为O(n^2),对于500000的数据来说是肯定TLE的,加上二分优化的话复杂度就可以降低到O(nlogn)了。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 500005 int dp[N],road[N];
int x; void cal(int a)
{
int l=,r=x,mid;
while(l<=r){
mid=(l+r)>>;
if(dp[mid]<a) l=mid+;
else r=mid-;
}
dp[l]=a;
} int main()
{
int n;
int cas=;
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
x=;
for(int i=;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
road[a]=b;
}
dp[]=road[];
for(int i=;i<=n;i++){
int a=road[i];
if(a>dp[x]) dp[++x]=a;
else cal(a);
}
printf("Case %d:\n",++cas);
if(x==) 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",x);
}
return ;
}

最新文章

  1. 转载扩展Windows Mobile模拟器存储空间的方法
  2. XGBoost参数调优完全指南(附Python代码)
  3. JavaSE复习_7 异常
  4. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析
  5. 转载:Raspberry Pi 树莓派入门
  6. c# ffmpeg视频转换【转载】
  7. java之SpringMVC配置!配置!配置!
  8. 数据接口测试工具 Postman 介绍
  9. 转:centos 7 安装音频视频解码器
  10. Windows下安装及使用NVM
  11. nuxt.js实战之用vue-i18n实现多语言
  12. 另一道不知道哪里来的FFT题
  13. Nginx – access_log格式及配置
  14. idea : shorten command line
  15. 20165310 《Java程序设计》课程总结
  16. 正睿 2018 提高组十连测 Day2 T2 B
  17. Strandbeest mechanism and Leg mechanism
  18. jconsole工具使用----jvm内存泄漏问题
  19. Yii 时间戳格式化显示的问题
  20. [转]Raanan Fattal - Image and Video Upscaling from Local Self-Examples 图像放大

热门文章

  1. C#.NET自定义报表数据打印
  2. JNDI(Java Naming and Directory Interface)
  3. 一款好用的视频转换gif的小软件——抠抠视频秀
  4. socket上http协议应用(使用socket进行http通信的例子,准备好报头以后,简单read/write就可以了)
  5. 微信小程序把玩(六)模块化
  6. Leaflet(Esri)初识
  7. 使用scratchbox2建立交叉编译环境
  8. JS浏览器滚轮事件实现横向滚动照片展
  9. Java Date Calendar DateFormat Details
  10. QT延时方法整理(QTimer::singleShot,QWaitCondition,QDateTime.secsTo三种新方法)