题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025

解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p排序也是一样的。但这题的n的范围是5*10^5次方,所以用n^2算法求

最长上升子序列肯定不行,下面简单介绍一下nlogn时间内求的方法:

从序列里面每次插入一个数,插入到另外一个数组里面,这个数组初始状态是空的,插入一个数时,如果这个数比这个数组里面的任何一个数都大,则直接插入到最后面,否则判断这个数跟在这个数组里面值相差最小的一个数的位置,然后这里有一个细节,就是要尽量使得这里的数很小,因为这样可以使后来可以插入后面的数的个数越多,这个仔细想想应该知道。然后最后这个数组的长度就是最长上升子序列的长度,在这题中也就是最多可以修建的路的条数。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<cmath>
  6. #include<deque>
  7. #include<map>
  8. #include<queue>
  9. #include<cstdlib>
  10. using namespace std;
  11. const int maxn = ;
  12. struct node
  13. {
  14. int a,b;
  15. }city[maxn];
  16. int que[maxn];
  17.  
  18. bool cmp(node a,node b)
  19. {
  20. return a.a < b.a;
  21. }
  22. int find(int d,int len)
  23. {
  24. int l = ,r = len - ;
  25. while(l < r)
  26. {
  27. int mid = (l + r) >> ;
  28. if(que[mid] >= d)
  29. r = mid;
  30. else l = mid + ;
  31. }
  32. return r;
  33. }
  34. int main()
  35. {
  36. int n,kase = ;
  37. while(scanf("%d",&n)!=EOF)
  38. {
  39. for(int i = ;i < n;++i)
  40. scanf("%d%d",&city[i].a,&city[i].b);
  41. sort(city,city+n,cmp);
  42. que[] = city[].b; //初始化,先把1个元素放进已经有顺序的集合里面
  43. int len = ;
  44. for(int i = ;i < n;++i)
  45. {
  46. int pos = find(city[i].b,len);
  47. if(pos == len - && que[pos] < city[i].b)
  48. que[len++] = city[i].b;
  49. else que[pos] = min(que[pos],city[i].b);
  50. }
  51. if(len > )
  52. printf("Case %d:\nMy king, at most %d roads can be built.\n",kase++,len);
  53. else printf("Case %d:\nMy king, at most %d road can be built.\n",kase++,len);
  54. puts("");
  55. }
  56. return ;
  57. }

HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)的更多相关文章

  1. [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 ...

  2. hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列)

    题意:贫穷和富有的城市都按顺序1-n排列,需要在中间建造尽可能多的道路,最多可以建造多少条? 解:如果条件这样给出,贫穷的城市按顺序排列,并且按顺序给出每个贫穷城市需要的资源,那么能建造的最多的道路数 ...

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

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

  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 + 二分优化)

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

  6. HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/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+二分法】

    主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...

  8. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  9. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

    点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...

随机推荐

  1. EntityFramework系列:Repository模式与单元测试

    1.依赖IRepository接口而不是直接使用EntityFramework 使用IRepository不只是架构上解耦的需要,更重要的意义在于Service的单元测试,Repository模式本身 ...

  2. 几种Boost算法的比较(Discrete AdaBoost, Real AdaBoost, LogitBoost, Gentle Adaboost)

    关于boost算法 boost算法是基于PAC学习理论(probably approximately correct)而建立的一套集成学习算法(ensemble learning).其根本思想在于通过 ...

  3. css边框阴影

    <style type="text/css">.mydiv{width:250px;height:auto;border:#909090 1px solid;backg ...

  4. usefull-url

    http://www.johnlewis.com/ http://codepen.io/francoislesenne/pen/aIuko http://www.rand.org/site_info/ ...

  5. 用SQL打印乘法口诀表

    --用SQL打印出乘法口诀表 declare @i int ,@j int --@i是乘法口诀的行数 --一共九行 begin --每次都是从1*开始,j每循环一次递增 )--print每次输出都会换 ...

  6. Eclipse属性文件编辑器---Properties Editor

    今天在用 Eclipse 来编辑 .properties 文件时,写的中文会自动转为 Unicode 编码,完全不知道自己的中文写的是什么!! 于是查了一下,网上推荐,在Eclipse 中 安装一个 ...

  7. rpm常用选项

    httpd-2.2.15-39.el6.centos.x86_64.rpmhttpd   -      2.2.15-    39.el6.centos.       x86_64 .rpm软件名称- ...

  8. Java编程思想学习(十六) 并发编程

    线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...

  9. 从TP、FP、TN、FN到ROC曲线、miss rate、行人检测评估

    从TP.FP.TN.FN到ROC曲线.miss rate.行人检测评估 想要在行人检测的evaluation阶段要计算miss rate,就要从True Positive Rate讲起:miss ra ...

  10. 【poj1090】 Chain

    http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...