地址: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的,这题用前一个果断会超时。。。

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

  

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <vector>
  10.  
  11. #define PI acos((double)-1)
  12. #define E exp(double(1))
  13. using namespace std;
  14.  
  15. #define N 500009
  16. struct point
  17. {
  18. int x,y;
  19. };
  20.  
  21. struct point city[N];
  22. int a[N];
  23.  
  24. bool cmp(point &a,point &b)
  25. {
  26. return a.x<b.x;
  27. }
  28.  
  29. int found(int x,int len)
  30. {
  31. int l,r,mid;
  32. l = ;
  33. r = len;
  34. mid = (l+r)/;
  35. while(l <= r)
  36. {
  37. if(a[mid] < x)
  38. {
  39. l = mid+;
  40.  
  41. }
  42. else if(a[mid] > x)
  43. {
  44. r = mid-;
  45. }
  46. mid = (l+r)/;
  47. }
  48. return l;
  49. }
  50. int main (void)
  51. {
  52. int n,k=;
  53. while(scanf("%d",&n) == )
  54. {
  55. int len;
  56. k++;
  57. for(int i = ;i<=n;i++)
  58. {
  59. scanf("%d%d",&city[i].x,&city[i].y);
  60. a[i]= N + ;
  61. }
  62. sort(city+,city+n+,cmp);
  63. a[] = -;
  64. a[] = city[].y;
  65. len = ;
  66. for(int i = ;i<=n;i++)
  67. {
  68. int j = found(city[i].y,len);
  69. a[j] = city[i].y;
  70. if(len < j)
  71. len = j;
  72. }
  73. if(len == )
  74. printf("Case %d:\nMy king, at most %d road can be built.\n\n",k,len);
  75. else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",k,len);
  76. }
  77. return ;
  78. }

杭电1025Constructing Roads In JGShining's Kingdom的更多相关文章

  1. HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)

    分析: 最长不降子序列,n很大o(n^2)肯定超,想到了小明序列那个题用线段树维护前面的最大值即可 该题也可用二分搜索来做. 注意问题输出时的坑,路复数后加s #include <map> ...

  2. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  3. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  4. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  5. hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  6. Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  7. hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  8. HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)

    HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...

  9. hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)

    题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Li ...

随机推荐

  1. C0301 代码块{}的使用,重定向, 从文件中读取行

    #!/bin/bash # 从 /etc/fstab 中读行 File=/etc/fstab {     read line1     read line2 } < $File # {}代码块, ...

  2. 使用NPOI导入Excel注意日期格式和数字格式

    //使用NPOI导入Excel public static DataTable importExcelToDataSetUsingNPOI(string FilePath, string fileNa ...

  3. iOS开发-你真的会用SDWebImage?

    SDWebImage作为眼下最受欢迎的图片下载第三方框架,使用率非常高.可是你真的会用吗?本文接下来将通过例子分析怎样合理使用SDWebImage. 使用场景:自己定义的UITableViewCell ...

  4. 你的企业是否须要开发APP?

    移动互联网时代的到来,粗分出"新兴行业"与"传统行业".除了互联网公司,其它似乎都被归到了"传统行业".连传统行业中最传统的房地产公司代表人 ...

  5. Page_ClientValidate 用法

    JS script function ConfirmMe(){   return confirm("Do you want to proceed?");} ASPX <asp ...

  6. 复合文档(Compound Document)读写栗子

    复合文件是把磁盘文件系统的管理方式移植到文件中---复合文件. 复合文档是由 Windows 系统通过 COM 提供的, 它能完成像 Windows 目录结构一样复杂的文件结构的存取:提示一下 Win ...

  7. poj 3498(最大流+拆点)

    题目链接:http://poj.org/problem?id=3498 思路:首先设一个超级源点,将源点与各地相连,边容量为各点目前的企鹅数量,然后就是对每个冰块i进行拆点了(i,i+n),边容量为能 ...

  8. Angular2 初识

    AppComponent 壳的三个实现文件: app.component.ts— 组件的类代码,这是用 TypeScript 写的. app.component.html— 组件的模板,这是用 HTM ...

  9. Segmented 标签栏 切换效果

    转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0512/1615.html http://www.it165.net/pr ...

  10. dlut1188(wanghang的迷宫)

    题目链接:传送门 题目大意:从起点到终点需要最少多少步(必须要关掉所有开关才能出去) 题目思路:用一个3维数组   dp[x][y][t]表示到达当前位置x,y,已经关掉了t个开关走的最少步数,然后就 ...