【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

题干:

Constructing Roads In JGShining's Kingdom

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


 

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.



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.

Hint

Huge input, scanf is recommended.

解题报告:

下面介绍一下o(nlogn)的上升子序列做法:

    就是求最长上升子序列,一开始用的普通办法求的!直接TEL;就在网上找了一个时间复杂度为O(nlogn)的算法,其算法思想为:(网上找的)

假设要寻找最长上升子序列的序列是a[n],然后寻找到的递增子序列放入到数组b中。

(1)当遍历到数组a的第一个元素的时候,就将这个元素放入到b数组中,以后遍历到的元素都和已经放入到b数组中的元素进行比较;

(2)如果比b数组中的每个元素都大,则将该元素插入到b数组的最后一个元素,并且b数组的长度要加1;

(3)如果比b数组中最后一个元素小,就要运用二分法进行查找,查找出第一个比该元素大的最小的元素,然后将其替换。

在这个过程中,只重复执行这两步就可以了,最后b数组的长度就是最长的上升子序列长度。例如:如该数列为:

5 9 4 1 3 7 6 7

那么:

5 //加入
5 9 //加入
4 9 //用4代替了5
1 9 //用1代替4
1 3 //用3代替9
1 3 7 //加入
1 3 6 //用6代替7
1 3 6 7 //加入

最后b中元素的个数就是最长递增子序列的大小,即4。

要注意的是最后数组里的元素并不就一定是所求的序列,

例如如果输入 2 5 1

那么最后得到的数组应该是 1 5

而实际上要求的序列是 2 5

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN=500010;
int a[MAXN],b[MAXN];//用二分查找的方法找到一个位置,使得num>b[i-1] 并且num<b[i],并用num代替b[i]
//手写upper_bound 
//int Search(int num,int low,int high) {
//	int mid;
//	while(low<=high) {
//		mid=(low+high)/2;
//		if(num>=b[mid])  low=mid+1;
//		else   high=mid-1;
//	}
//	return low;
//}
int DP(int n) {int len,pos;b[1]=a[1];len=1;for(int i=2; i<=n; i++) {if(a[i]>=b[len]) { //如果a[i]比b[]数组中最大还大直接插入到后面即可b[++len]=a[i];} else { //用二分的方法在b[]数组中找出第一个比a[i]大的位置并且让a[i]替代这个位置//pos=Search(a[i],1,len);pos = upper_bound(b+1,b+len+1,a[i]) - b;b[pos]=a[i];}}return len;
}
int main() 
{int n;int iCase=0,x,y;while(scanf("%d",&n)!=EOF) {for(int i=1; i<=n; i++) {scanf("%d%d",&x,&y);a[x]=y;}int res=DP(n);printf("Case %d:\n",++iCase);if(res==1) {printf("My king, at most 1 road can be built.\n\n");} elseprintf("My king, at most %d roads can be built.\n\n",res);}return 0;
}

ps:其实DP函数中应该是a[i]>b[len],但是因为这个题的题干和数据特殊性,确保了不会出现两次重复的数字,所以加上等号也可以ac。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441099.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java list详解_Java 中 list 用法案例详解

LIST是个容器接口可以理解为动态数组&#xff0c;传统数组必须定义好数组的个数才可以使用&#xff0c;而容器对象无须定义好数组下标总数&#xff0c;用add()方法即可添加新的成员对象&#xff0c;他可以添加的仅仅只能为对象&#xff0c;不能添加基本数据类型&#xff0c;容器…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐的组合数+(取模,数学,思维)

题干&#xff1a; 小乐乐得知一周有7天之后就对7产生了兴趣。 小乐乐得到了两堆数字数字时连续的。 第一堆包含[1,n]n个数字&#xff0c;第二堆包含[1,m]m个数字。 小乐乐想要从两堆中各挑选出一个整数x,y&#xff0c;使得x,y的和为7的倍数。 请问小乐乐有多少种组合的方式…

java 写文件 属性吗_使用JAVA读写Properties属性文件

自己定义一个属性文件&#xff1a;例如prop.propertiesbaseFilePathD\:/kuanter/resourcetesxabcd我们要做的第一步就是要将文件读取到Properties类对象中&#xff0c;由于load有一个参数是InputStream&#xff0c;所以我们可以用 InputStream的子类FileInputStream将属性文件读…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】 小乐乐算数字(水题,快速幂,lowbit)

题干&#xff1a; 小乐乐最喜欢玩数字了。 小乐乐最近迷上了2这个整数&#xff0c;他觉得2的幂是一种非常可爱的数字。 小乐乐想知道整数x的最大的 2的幂 &#xff08;2^y&#xff09;的因子。 y为整数。 输入描述: 输入整数x。(1<x<1e18) 输出描述: 输出整数x的最…

java date 相差_java 比较时间相差多少分钟

/** * 返回二个时间相差的分分钟数,如果一个为空&#xff0c;返回为0&#xff1b; * param startDate&#xff0c;比如08&#xff1a;09 * param endDate&#xff0c;如18&#xff1a;09 * return */ public static int getMinutesDiff(String startDate,String endDate){ int …

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐吃糖豆 (fIb博弈)

题干&#xff1a; 小乐乐是一个比较喜欢吃糖豆的小孩子&#xff0c;小乐乐的哥哥大乐乐也同样爱吃糖豆。 作为一个小孩子&#xff0c;他们永远觉得谁吃掉了最后一个糖豆&#xff0c;谁吃的糖豆最多。 为了公平起见小乐乐与大乐乐商量吃糖豆的规则如下&#xff1a; 1. …

html5 sse java_html5----sse实现服务端推送数据给前端

案例基于thinkPHP框架&#xff1a;服务端方法&#xff1a;public function ssefun(){ob_implicit_flush();header(Content-Type: text/event-stream);header(Cache-Control: no-cache);$itime();echo retry:1000.PHP_EOL;//每秒执行一次echo "data: The server time is: {…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐切割方块(思维,水题)

题干&#xff1a; 小乐乐的作业本是2n*2n的方格本。 某天小乐乐的童鞋&#xff0c;想要考验一下小乐乐。 他将小乐乐的一张方格纸中的某个格子(x,y)涂成黑色&#xff0c; 小乐乐能否在将4*4的方格本沿着方格边缘且切割线与黑色方格不存在公共交点的情况下将方格本切割成两…

*【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐打游戏(bfs,双元bfs,思维)

题干&#xff1a; 小乐乐觉得学习太简单了&#xff0c;剩下那么多的时间好无聊&#xff0c;于是便想打游戏。 最近新出了一个特别火的游戏&#xff0c;叫吃猪&#xff0c;小乐乐准备玩一玩。 吃猪游戏很简单&#xff0c;给定一个地图&#xff0c;大小为n*m&…

java map 结构体_java之mapstruct的应用

一、MapStruct是一个代码生成器&#xff0c;简化了不同的Java Bean之间映射的处理&#xff0c;所以映射指的就是从一个实体变化成一个实体。例如我们在实际开发中&#xff0c;DAO层的实体和一些数据传输对象(DTO)&#xff0c;大部分属性都是相同的&#xff0c;只有少部分的不同…

【CodeForces - 340D】Bubble Sort Graph (思维,nlogn最长上升子序列类问题)

题干&#xff1a; Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (lets call it G) i…

java nio有哪些功能_如何真正理解java中的NIO?

从历史进程来看可能会比较好早期计算机性能较差的情况下反正你同时也没法同时处理很多io&#xff0c;不如开一个就阻塞在那边&#xff0c;程序员编程也省事&#xff0c;别的花里胡哨的骚操作还未必有这种方式性能高&#xff0c;这个叫做bio后面cpu内存性能上去了&#xff0c;磁…

【LightOJ - 1031】Easy Game (区间dp,博弈)

题干&#xff1a; You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot tak…

java web svn_如何搭建svnadmin,一个简单的svnWEB页面

Svn Admin是一个Java开发的管理Svn服务器的项目用户的web应用。安装好Svn服务器端好&#xff0c;把Svn Admin部署好&#xff0c;就可以通过web浏览器管理Svn的项目&#xff0c;管理项目的用户&#xff0c;管理项目的权限。使得管理配置Svn简便&#xff0c;再也不需要每次都到服…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)

题干&#xff1a; 小乐乐一天天就知道玩&#xff0c;这一天又想玩象棋。 我们都知道马走日。 现在给定一个棋盘&#xff0c;大小是n*m,把棋盘放在第一象限&#xff0c;棋盘的左下角是(0,0),右上角是(n - 1, m - 1); 小乐乐想知道&#xff0c;一个马从左下角(0, 0)开始&#…

java 递归 时间复杂度_递归到底是怎么实现的?它的时间复杂度怎么算?

递归到底是个啥&#xff1f;常听见的一句话就是&#xff1a;自己调用自己。按照这个说法&#xff0c;写个简单的递归自己推导一下的确可以&#xff0c;但是总是有点绕&#xff0c;推着推着自己把自己陷进去了。递归函数运行时&#xff0c;实际上会进行一个压栈(思考栈的特点&am…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐搭积木(状压dp)

题干&#xff1a; 小乐乐想要给自己搭建一个积木城堡。 积木城堡我们假设为n*m的平面矩形。 小乐乐现在手里有1*2&#xff0c;2*1两种地砖。 小乐乐想知道自己有多少种组合方案。 输入描述: 第一行输入整数n,m。(1<n,m<10) 输出描述: 输出组合方案数。 示例1 输…

Java 重定向 无法写入_java IO 文件读入,写入,重定向

Java代码 packagestar20110526;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io…

【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

题干&#xff1a; FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 < p < n and 0 < q < n. At each grid location Fatmouse has hid between 0 and 10…

获取excel名称java_使用Apache POI获取大型Excel文件的Excel工作表名称

小编典典为了显示Gagravarr的评论可能意味着什么&#xff1a;该XSSFReader包含方法XSSFReader.getSheetsData其中“返回一个迭代器&#xff0c;这将让你在把所有的不同的表&#xff0c;每个表的InputStream中只有打开时开始迭代器牵强。这是给你的时候&#xff0c;每个做关闭In…