【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,一经查实,立即删除!

相关文章

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

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

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

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

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

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

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

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

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…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

算法讲解 -- 区间dp经典模型与优化(石子归并)

石子合并问题是最经典的DP问题。首先它有如下3种题型&#xff1a; PPT讲解&#xff1a;点击打开链接 (1)有N堆石子&#xff0c;现要将石子有序的合并成一堆&#xff0c;规定如下&#xff1a;每次只能移动任意的2堆石子合并&#xff0c;合并花费为新合成的一堆石子的数量。求将…

cpu影响matlab仿真速度吗,Proteus仿真速度很慢的分析

类型&#xff1a;行业软件大小&#xff1a;1.1M语言&#xff1a;中文 评分&#xff1a;6.5标签&#xff1a;立即下载这篇文章是我的个人实践经验&#xff1a;很多朋友在做Proteus硬件仿真的时候可能都碰上了仿真速度慢的问题&#xff0c;在点击了开始仿真之后&#xff0c;CPU过…

mysql 7天自动拒单功能,mysql查询最近7天的数据,没有数据自动补0

select DATE( createtime) date , createtime, count(1) as count from order表 where DATEDIFF( now(), createtime)<7group by date ;12 月九号没有;给他在关联一张日期的表;--生成从今天开始完整的7天日期DECLARE LastSevenDaytable(day date)DECLARE StartDaydate …

基于维纳滤波的语音增强算法 matlab,基于维纳滤波语音增强算法的改进实现

通过对维纳滤波的介绍,实现了基本维纳滤波效果;利用两级维纳滤波和两级滤波器组滤波方法实现了语音增强,达到了良好的效果。维普资讯 http://doc.docsou.com文章编号&#xff1a;0 2 8 8 (o 7 0 - 0 4 0 10—6 4 2 o )1 0 4 - 3基于维纳滤波语音增强算法的改进实现白文雅&#…

php7 获取文件类型,太简单了!PHP获取文件扩展名的7中方法

PHP中获取文件扩展名的方法第一种&#xff1a;$file x.y.z.png;echo substr(strrchr($file, .), 1);解析&#xff1a;strrchr($file, .)strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置&#xff0c;并返回从该位置到字符串结尾的所有字符第二种&#xff1a;$file…

【EOJ Monthly 2018.12 - A,B,C】套题训练,部分题解

A. 题干&#xff1a; A. 仰望星空 单测试点时限: 2.0 秒 内存限制: 512 MB 你就这样静坐在草地上&#xff0c;离我稍远的地方。 我用眼角瞅着你&#xff0c;你什么话也别说。 语言是误会的根源。 但是&#xff0c;每天&#xff0c;你可以坐得离我近一些…… 你和她一起仰头…

php百度搜索框代码,基于jquery的仿百度搜索框效果代码_jquery

先看看整个的效果图&#xff1a;图一&#xff1a;图二&#xff1a;图三&#xff1a;图四&#xff1a;大概的效果图就这样&#xff0c;接下来直接看源码页面&#xff1a;CSS&#xff1a;.autoSearchText{border:solid 1px #CFCFCF;height:20px;color:Gray;}.menu_v{margin:0;pad…

matlab大作业题题单,2011MATLAB大作业-题目-

(1)求解线性规划问题&#xff1a;minZ 4x1 x2 7x3s.t.x1 x2 x3 53x1 x2 x3 4x1 x2 4x3 7x1,x2 0问各xi分别取何值时&#xff0c;Z有何极小值。(2)编写一个函数&#xff0c;使其能够产生如下的分段函数&#xff1a;0.5x&#xff0c;x 2f(x) 1.5 0.25x&#xff0c;2 x 6&#xff…

php webshell编写,php webshell学习

一、环境kali 192.168.43.177开户apache /etc/init.d/apache2 start/var/www/html/目录下编辑php代码hackbarhttps://github.com/Mr-xn/hackbar2.1.3二、php基础输出函数:echo - 可以输出一个或多个字符串print - 只允许输出一个字符串&#xff0c;返回值总为 1提示&#xff1a…

【CodeForces - 27E】Number With The Given Amount Of Divisors (数论,数学,反素数)

题干&#xff1a; Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018. Input The first line of the input contains integer n (1 ≤ n ≤ 1000). Outp…

*【UVA - 10382】Watering Grass(贪心,区间覆盖问题,思维)

题干&#xff1a; 题目大意&#xff1a; 有一块草坪&#xff0c;长为l&#xff0c;宽为w&#xff0c;在它的水平中心线上有n个位置可以安装喷水装置&#xff0c;各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数&#xff0c;如果无论如何…

oracle如何把字符集改回默认,更改oracle字符集

在安装oracle时&#xff0c;选了默认字符集是utf8&#xff0c;后来发现与plsql developer工具联合使用时&#xff0c;会出现各种乱码问题。再加上我的项目也是gbk的&#xff0c;因此&#xff0c;将字符集改成gbk试试。步骤如下&#xff1a;1.查看当前的字符集和语言select * fr…