hdu 1025LIS思路同1257 二分求LIS

时间:2022-04-22 04:30:15

题目:

Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24446    Accepted Submission(s):
6968

Problem Description
JGShining's kingdom consists of 2n(n is no more than
500,000) small cities which are located in two parallel lines.

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.

hdu 1025LIS思路同1257    二分求LIS

In order to build as many roads
as possible, the young and handsome king of the kingdom - JGShining needs your
help, please help him. ^_^

 
Input
Each 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.
 
Output
For each test case, output the result in the form of
sample.
You should tell JGShining what's the maximal number of road(s) can
be built.
 
Sample Input
2
1 2
2 1
3
1 2
2 3
3 1
 
Sample Output
Case 1:
My king, at most 1 road can be built.
Case 2:
My king, at most 2 roads can be built.
 
大意:输入n 表示n对关系
接着n行,每行a  b   表示a村与b村建立了关系
一个贫穷村庄与一个富裕村建立关系(保证不重复且一个富村只能与一个贫建立)
保证路线不重叠交叉下求最多建立几条道路;
仔细想想还是lis,与1257类似的思想,
输入 a,b 存入数组中a[a]=b,因为村庄从1到n遍历一遍所以输入完之后就是一个长度为n的数组,求最多线路不就是求lis么(即保证数组升序即可)
此时因为maxn过大,采用O(NlogN),用到二分搜索算法
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const
int maxn=;
const
int inf=;
int
p[maxn],dp[maxn];
int
main()
{

int
n,i,m=;
while
(scanf("%d",&n)!=EOF){int q,r;m++;
for
(i=;i<n;i++)
scanf("%d%d",&q,&r),p[q-]=r;
fill(dp,dp+n,inf); for(i=;i<n;i++)
*
lower_bound(dp,dp+n,p[i])=p[i]; //每次更新的过程,由于fill了inf,所以如果当前ta为最大则直接加到数组最后一个!INF的后面
int
len=lower_bound(dp,dp+n,inf)-dp;
printf("Case %d:\n",m);
if
(len==)
printf("My king, at most 1 road can be built.\n");
else

printf("My king, at most %d roads can be built.\n",len);
printf("\n"); }
return
;
}

hdu 1025LIS思路同1257 二分求LIS的更多相关文章

  1. hdu5256 二分求LIS&plus;思维

    解题的思路很巧,为了让每个数之间都留出对应的上升空间,使a[i]=a[i]-i,然后再求LIS 另外二分求LIS是比较快的 #include<bits/stdc++.h> #define ...

  2. 二分求LIS并打印结果

    1275: God's ladder [DP] 时间限制: 1 Sec 内存限制: 128 MB  Special Judge 题目描述 天明来到神之宫殿,在他眼前出现了若干个石柱,每个石柱上有1枚金 ...

  3. HDU 2035 人见人爱A&Hat;B(二分求幂,快速求幂)

    题意:求A的B次方的后三位数字 思路1:常规求幂,直接取余求解 代码: #include<iostream> #include<cstdio> using namespace ...

  4. HDU 5773 The All-purpose Zero(O(nlgn)求LIS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍L ...

  5. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

  6. hdu 1950 最长上升子序列(lis) nlogn算法【dp】

    这个博客说的已经很好了.http://blog.csdn.net/shuangde800/article/details/7474903 简单记录一下自己学的: 问题就是求一个数列最长上升子序列的长度 ...

  7. UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)

    题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...

  8. UVa 10534 波浪子序列(快速求LIS)

    https://vjudge.net/problem/UVA-10534 题意:给定一个长度为n的整数序列,求一个最长子序列(不一定连续),使得该序列的长度为2k+1,前k+1个数严格递增,后k+1个 ...

  9. HDU 3856 Palindrome &lpar; Manacher &plus; RMQ &plus; 二分 &rpar; WA!!!

    不知道错在哪了,求大神指教!!! 思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行. #include &lt ...

随机推荐

  1. 在Visual Studio上开发Node&period;js程序&lpar;2&rpar;——远程调试及发布到Azure

    [题外话] 上次介绍了VS上开发Node.js的插件Node.js Tools for Visual Studio(NTVS),其提供了非常方便的开发和调试功能,当然很多情况下由于平台限制等原因需要在 ...

  2. js时间戳转成日期不同格式 【函数】

    //第一种 function getLocalTime(nS) { ).toLocaleString().replace(/:\d{,}$/,' '); } alert(getLocalTime()) ...

  3. Dapper ORM 用法—Net下无敌的ORM

    假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...

  4. android 纯c&sol;c&plus;&plus;开发(转)

    转载自: http://jingyan.baidu.com/article/a501d80cf394dfec630f5e85.html android 自ndk r8出来以后,就开始支持纯c/c++开 ...

  5. Eclipse调试Java的十个技巧

      先提三点 不要使用System.out.println()作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志   1.条件断点 想象一下我们平时如何添加断点,通常的做法是 ...

  6. python基础教程(六)

    学到这里已经很不耐烦了,前面的数据结构什么的看起来都挺好,但还是没法用它们做什么实际的事.从这一章节开始有点难度,需要好好理解. 基本语句的更多用法 使用逗号输出 >>> print ...

  7. C&num;控制台程序点击后暂停工作

    C#控制台应用程序,点击后就会暂停运行,但是我想让它运行不受点击的干扰.下面是程序演示: public void Test() { ThreadOut(); } private void Thread ...

  8. Solr 从文件创建索引

    http://blog.csdn.net/clj198606061111/article/details/21492457 http://wiki.apache.org/solr/Extracting ...

  9. Error&colon; Another program is already listening on a port that one of our HTTP servers is configured to use&period; Shut this program down first before starting

    解决方法: find / -name supervisor.sock unlink /name/supervisor.sock 2. www-data 用户是干什么用的 3.如何通过superviso ...

  10. git clone的

    git clone git@e.coding.net:wudi360/*******.git