地址: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 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.

思路:dp题+二分查找。。。依旧没独立做出来,好惨,最近几个dp题全挂==

  题意是找最长上升子序列,

  懒得写题解了,不是自己想出来的,上一篇写的比较好的题解:http://blog.csdn.net/dangwenliang/article/details/5728363

  这题有两种算法n^2和nlogn的,这题用前一个果断会超时。。。

  在二分法上卡半天,我也是服了我的智商。。

  

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; #define N 500009
struct point
{
int x,y;
}; struct point city[N];
int a[N]; bool cmp(point &a,point &b)
{
return a.x<b.x;
} int found(int x,int len)
{
int l,r,mid;
l = ;
r = len;
mid = (l+r)/;
while(l <= r)
{
if(a[mid] < x)
{
l = mid+; }
else if(a[mid] > x)
{
r = mid-;
}
mid = (l+r)/;
}
return l;
}
int main (void)
{
int n,k=;
while(scanf("%d",&n) == )
{
int len;
k++;
for(int i = ;i<=n;i++)
{
scanf("%d%d",&city[i].x,&city[i].y);
a[i]= N + ;
}
sort(city+,city+n+,cmp);
a[] = -;
a[] = city[].y;
len = ;
for(int i = ;i<=n;i++)
{
int j = found(city[i].y,len);
a[j] = city[i].y;
if(len < j)
len = j;
}
if(len == )
printf("Case %d:\nMy king, at most %d road can be built.\n\n",k,len);
else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",k,len);
}
return ;
}

最新文章

  1. POJ 2697 A Board Game(Trie判重+BFS)
  2. sqlite 删除表中重复数据(亲测可用)
  3. mysql大表如何优化
  4. html,body最顶层元素.
  5. Hello WPF!
  6. baseDao 使用spring3+hibernate4方式
  7. VMware系统运维(十七)部署虚拟化桌面 Horizon View Manager 5.2 配置池授权
  8. makefile使用
  9. 滑雪_poj_1088(记忆化搜索).java
  10. error: undefined reference to `XXX::XXX(type1, ypte2)
  11. GitLab 安装配置笔记(转)
  12. Example018主页加载时获取焦点
  13. Linux如何查找处理文件名后包含空格的文件
  14. Struts2(三) 配置struts.xml的提示(在不联网的情况下)
  15. 分布式协调服务Zookeeper集群之ACL篇
  16. springcloud第五步:使用Zuul搭建服务接口网关
  17. VUE2中使用mint-ui,日期选择picker
  18. 洛谷 P3965 [TJOI2013]循环格 解题报告
  19. 驱动文件中只有cat/inf/dll文件,怎么安装
  20. ESET NOD32 Antivirus – 免费 3个月/ 3PC

热门文章

  1. Control character in cookie value or attribute
  2. Oracle常用命令大全
  3. (转)reactor模式
  4. uva414 - Machined Surfaces
  5. 该如何将MathType公式粘贴到文档中
  6. php检测非法字符方法
  7. SQLServer中Partition By
  8. javajava持有对象(容器类)
  9. JavaWeb开发的一系列可能的配置
  10. Jmeter--CSV Data Set Config 参数化配置