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),去搜了题解,学习了一下,感觉不错,拿出来 ...
随机推荐
- 【转】Ajax中send方法参数的使用(get/post)
Ajax中send方法参数的使用 一般情况下,使用Ajax提交的参数多是些简单的字符串,可以直接使用GET方法将要提交的参数写到open方法的url参数中,此时send方法的参数为null. 例如 : ...
- jquery之insertBefore(),insertAfter(),prependTo(),appendTo()用法详解
导航: 1,insertBefore(),insertAfter(),prependTo(),appendTo()这四个函数用法几乎一样 2, 与之相对的有四个函数:Before(),After(), ...
- LINUX DIFF命令详解
刚才在和公司做离线IP对比,最后手工了,感觉还是比较麻烦的,遇到数据很大的时候不能手工进行了 本想用linux下的DIFF来进行对比,发现结果很乱.时间很紧最后还是手工了. 现在忙完要认认真真学习一下 ...
- sql注入在线检测(sqlmapapi)
版权:http://blog.csdn.net/yueguanghaidao/article/details/38026431 每次看都不方便 摘抄下来 之前一搞渗透的同事问我,sqlmapapi ...
- 美国插画家Mike Bear作品欣赏
Mike Bear,美国插画师兼概念艺术家,在漫画和游戏界从业6年有余,分别为包括Popcap.Rockstar Games.Hasbro.EA等在内的业界巨头创建作品.他的画风较为抽象,大胆想象出一 ...
- Mybatis+struts2+spring整合
把student项目改造成ssm struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...
- Codeforces Round #261 (Div. 2) B
链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...
- python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...
- url 转码 urlencode和 urldecode
参考网址http://www.t086.com/code/php/function.php-urlencode.php urlencode 将字符串以 URL 编码. 语法: string urlen ...
- read 读取文件内容
文件名 test28.sh #!/bin bash # reading data from a file count= cat test | while read line do echo " ...