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: 65536/32768 K (Java/Others)
Total Submission(s): 16047 Accepted Submission(s): 4580
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. ^_^
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.
You should tell JGShining what's the maximal number of road(s) can be built.
1 2
2 1
3
1 2
2 3
3 1
My king, at most 1 road can be built.
Case 2:
My king, at most 2 roads can be built.
Huge input, scanf is recommended.
//#define LOCAL
#include<cstdio>
#include<cstring>
const int maxn=;
const int inf=0x3f3f3f3f;
int str[maxn],dp[maxn],sac[maxn];
int res; int binary(int v,int n)
{
int ll=,rr=n,mid;
while(ll<=rr)
{
mid=ll+(rr-ll)/;
if(sac[mid]<=v&&sac[mid]!=-)
ll=mid+;
else
rr=mid-;
}
return ll;
}
void LIS(int n)
{
res=;
// for(int i=1;i<=n;i++)
// sac[i]=inf;
memset(sac,-,sizeof(int)*(n+));
for(int i=;i<=n;i++)
{
dp[i]=binary(str[i],res);
if(res<dp[i])
res=dp[i];
if(str[i]<sac[dp[i]]||sac[dp[i]]==-)
sac[dp[i]]=str[i];
}
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif int n,i,p,r,ct=;
while(scanf("%d",&n)!=EOF)
{
for( i=;i<=n;i++){
scanf("%d%d",&p,&r);
str[p]=r;
}
LIS(n);
printf("Case %d:\n",ct++);
if(res==)
printf("My king, at most 1 road can be built.\n");
else
printf("My king, at most %d roads can be built.\n",res);
printf("\n");
}
return ;
}
hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)的更多相关文章
- 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+二分优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Problem Des ...
- 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),去搜了题解,学习了一下,感觉不错,拿出来 ...
随机推荐
- VMware ESXI5.0的安装配置 zz
http://www.hotxf.com/thread-297-1-1.html 1, Vmware ESXI 光盘一张文件大小290M,本教程是以 5.0为案例. 2, 所需要安装的操作 ...
- Cheatsheet: 2013 08.01 ~ 08.13
Mobile Objective C Blocks: Summary, Syntax & Best Practices Android SDK: Create an Arithmetic Ga ...
- [HDOJ1231]最大连续子序列
混了好几个地方的博客,还是觉得博客园比较靠谱,于是决定在这里安家落户了.本人本科生一个,希望各位巨巨多多指教~ Hello World! 单独一个象征性的问候实在是太low了,还是决定来点实质性的.. ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 【转】移动端input输入placeholder垂直不居中
在移动端编写input输入框时候,为了输入文字与输入框垂直居中,一般情况下,会将input的line-height的高度等于height.但在移动端输入的时候会发现,虽然输入内容确实是垂直居中了,但是 ...
- SQL HAVING语句
HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. SQL HAVING 语法 SELECT column_name, aggregate_f ...
- Google账户_GooglePlay_关联
PS:这过程叫一个折腾...穷逼的无奈啊... 1. 1.1. 网上搜到说,安装 google play & google框架服务,手机需要root,没去证实这个事情... 用了之前的 And ...
- php按条件查询的数据分页显示,点击下一页时又列出全部数据的解决办法
其实很简单,只要把表单提交方式改为get方式就行了,然后调用分页函数: function getpage(&$m,$where,$pagesize=10){ $m1=clone $m;//浅复 ...
- (一)使用springAPI以及自定义类 实现AOP-aop编程
Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...