hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)
题目链接:
Constructing Roads In JGShining's Kingdom
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21045 Accepted Submission(s): 5950
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. ^_^
You should tell JGShining what's the maximal number of road(s) can be built.
Huge input, scanf is recommended.
题意:给你两个条线上一对一对的点,要把这一对一对的点连接起来,且连线不能相交,问最多有多少条线;
思路:按其中的一组数的大小排序,然后另一列数的最长上升子序列的长度就是答案,然后用一个O(n*logn)的算法找到这个长度,O(n^2)的方法会超时;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int N=5e5+;
int ans[N];
struct node
{
int a,b;
};
node x[N];
int cmp(node c,node d)
{
// if(c.a==d.a)return c.b<d.b;
return c.a<d.a;
}
stack<int>s;
int main()
{
int n;
int cnt=;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d%d",&x[i].a,&x[i].b);
}
sort(x+,x+n+,cmp);
int num=;
ans[]=x[].b;
for(int j=;j<=n;j++)
{
if(x[j].b>ans[num])
{
num++;
ans[num]=x[j].b;
}
else
{
int pos=upper_bound(ans+,ans+num+,x[j].b)-ans;
ans[pos]=x[j].b;
} }
if(num==) printf("Case %d:\nMy king, at most %d road can be built.\n\n",cnt++,num);
else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",cnt++,num);
}
return ;
}
hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)的更多相关文章
- 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 ...
- [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(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+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- 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),去搜了题解,学习了一下,感觉不错,拿出来 ...
随机推荐
- 什么是Java Server Pages?
JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束. JSP是一种Java servlet ...
- 最小生成树——Prim(普利姆)算法
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解Prim算法的idea 并用 源代码加以实现: 0.2)最小生成树的基础知识,参见 http://blog. ...
- Lumen开发:lumen源码解读之初始化(5)——注册(register)与启动(boot)
版权声明:本文为博主原创文章,未经博主允许不得转载. register()是在服务容器注册服务, bootstrap/app.php /** * 注册外部服务 */ $app->register ...
- Root(hdu5777+扩展欧几里得+原根)
Root Time Limit: 30000/1500 ...
- (转)linux设备驱动之USB数据传输分析 一
三:传输过程的实现说到传输过程,我们必须要从URB开始说起,这个结构的就好比是网络子系统中的skb,好比是I/O中的bio.USB系统的信息传输就是打成URB结构,然后再过行传送的.URB的全称叫US ...
- 我自己曾经经历的CMMI3认证通过关于软件测试的访谈【转载】
转自<http://blog.csdn.net/jcy58/article/details/51908884> 因为当初我在公司里是负责软件测试工作的,所以CMMI3和测试相关的访谈,就是 ...
- Card Collector(期望+min-max容斥)
Card Collector(期望+min-max容斥) Card Collector woc居然在毫不知情的情况下写出一个min-max容斥 题意 买一包方便面有几率附赠一张卡,有\(n\)种卡,每 ...
- 让Editplus支持sql语法高亮显示
版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共同拥有,欢迎转载.但未经作者允许必须保留此段声明,且在文章页面 ...
- ABAP--关于字符串String到XString XString to String转换代码
转自http://guanhuaing.iteye.com/blog/1498891 代码如下 report zrich_0001. data: s type string, h(1) type x, ...
- activiti--5 -----------------Activiti 工作流 流程各个步骤所涉及到的表
ACT_RE_*: 'RE'表示repository. 这个前缀的表包含了流程定义和流程静态资源 (图片,规则,等等). ACT_RU_*: 'RU'表示runtime. 这些运行时的表,包含流程实例 ...