HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p排序也是一样的。但这题的n的范围是5*10^5次方,所以用n^2算法求
最长上升子序列肯定不行,下面简单介绍一下nlogn时间内求的方法:
从序列里面每次插入一个数,插入到另外一个数组里面,这个数组初始状态是空的,插入一个数时,如果这个数比这个数组里面的任何一个数都大,则直接插入到最后面,否则判断这个数跟在这个数组里面值相差最小的一个数的位置,然后这里有一个细节,就是要尽量使得这里的数很小,因为这样可以使后来可以插入后面的数的个数越多,这个仔细想想应该知道。然后最后这个数组的长度就是最长上升子序列的长度,在这题中也就是最多可以修建的路的条数。
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<iostream>
- #include<cmath>
- #include<deque>
- #include<map>
- #include<queue>
- #include<cstdlib>
- using namespace std;
- const int maxn = ;
- struct node
- {
- int a,b;
- }city[maxn];
- int que[maxn];
- bool cmp(node a,node b)
- {
- return a.a < b.a;
- }
- int find(int d,int len)
- {
- int l = ,r = len - ;
- while(l < r)
- {
- int mid = (l + r) >> ;
- if(que[mid] >= d)
- r = mid;
- else l = mid + ;
- }
- return r;
- }
- int main()
- {
- int n,kase = ;
- while(scanf("%d",&n)!=EOF)
- {
- for(int i = ;i < n;++i)
- scanf("%d%d",&city[i].a,&city[i].b);
- sort(city,city+n,cmp);
- que[] = city[].b; //初始化,先把1个元素放进已经有顺序的集合里面
- int len = ;
- for(int i = ;i < n;++i)
- {
- int pos = find(city[i].b,len);
- if(pos == len - && que[pos] < city[i].b)
- que[len++] = city[i].b;
- else que[pos] = min(que[pos],city[i].b);
- }
- if(len > )
- printf("Case %d:\nMy king, at most %d roads can be built.\n",kase++,len);
- else printf("Case %d:\nMy king, at most %d road can be built.\n",kase++,len);
- puts("");
- }
- return ;
- }
HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)的更多相关文章
- [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 ...
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列)
题意:贫穷和富有的城市都按顺序1-n排列,需要在中间建造尽可能多的道路,最多可以建造多少条? 解:如果条件这样给出,贫穷的城市按顺序排列,并且按顺序给出每个贫穷城市需要的资源,那么能建造的最多的道路数 ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- 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 ...
- 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 ...
- 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 ...
- hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】
主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...
- hdu 1025 Constructing Roads In JGShining's Kingdom
本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
随机推荐
- EntityFramework系列:Repository模式与单元测试
1.依赖IRepository接口而不是直接使用EntityFramework 使用IRepository不只是架构上解耦的需要,更重要的意义在于Service的单元测试,Repository模式本身 ...
- 几种Boost算法的比较(Discrete AdaBoost, Real AdaBoost, LogitBoost, Gentle Adaboost)
关于boost算法 boost算法是基于PAC学习理论(probably approximately correct)而建立的一套集成学习算法(ensemble learning).其根本思想在于通过 ...
- css边框阴影
<style type="text/css">.mydiv{width:250px;height:auto;border:#909090 1px solid;backg ...
- usefull-url
http://www.johnlewis.com/ http://codepen.io/francoislesenne/pen/aIuko http://www.rand.org/site_info/ ...
- 用SQL打印乘法口诀表
--用SQL打印出乘法口诀表 declare @i int ,@j int --@i是乘法口诀的行数 --一共九行 begin --每次都是从1*开始,j每循环一次递增 )--print每次输出都会换 ...
- Eclipse属性文件编辑器---Properties Editor
今天在用 Eclipse 来编辑 .properties 文件时,写的中文会自动转为 Unicode 编码,完全不知道自己的中文写的是什么!! 于是查了一下,网上推荐,在Eclipse 中 安装一个 ...
- rpm常用选项
httpd-2.2.15-39.el6.centos.x86_64.rpmhttpd - 2.2.15- 39.el6.centos. x86_64 .rpm软件名称- ...
- Java编程思想学习(十六) 并发编程
线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...
- 从TP、FP、TN、FN到ROC曲线、miss rate、行人检测评估
从TP.FP.TN.FN到ROC曲线.miss rate.行人检测评估 想要在行人检测的evaluation阶段要计算miss rate,就要从True Positive Rate讲起:miss ra ...
- 【poj1090】 Chain
http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...