【杭电oj1025】Constructing Roads In JGShining's Kingdom

杭电ACM OJ 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列 O(nlogn)算法详解+回溯暴力解决+动态规划dp优雅解决 Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26933    Accepted Submission(s): 7655 Proble 阅读详情

 打开题目             Constructing Roads In JGShining's Kingdom

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


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.
 

Author
JGShining(极光炫影)
 

Recommend
We have carefully selected several similar problems for you:   1024  1081  1074  1078  1080 

最长不减子序列,n*logn复杂度:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 5*1e5+10;
int dp[N],a[N];
struct node {
	int a,b;
} s[N];
bool cmp(node A,node B) {
	if(A.a==B.a)
		return A.b<B.b;
	return A.a<B.a;
}
int find(int l,int r,int x) {		//二分的时候不要用递归,容易超时, 
	while(l<=r) {					//任何时候递归都是耗时间的,能不用就不用 
		int mid=l+r>>1;
		if(dp[mid]<x)
			l=mid+1;
		else if(dp[mid]>x)
			r=mid-1;
		else {
			l=mid;
			break;
		}
	}
//	printf("--\n");
	return l;
}
int main() {
	int n,p=0;
	while(scanf("%d",&n)!=EOF) {
		for(int i=0; i<n; i++) {
			scanf("%d%d",&s[i].a,&s[i].b);
		}
		sort(s,s+n,cmp);
//		for(int i=0; i<n; i++)    //这段不要的,能省时间就省时间,记住
//			a[i]=s[i].b;
		int len=0,j;
		dp[0]=-1;
		for(int i=0; i<n; i++) {
			if(s[i].b>=dp[len])
				dp[++len]=s[i].b;
			//	printf("--\n");
			else {
				j=find(1,len,s[i].b);
				dp[j]=s[i].b;
			}
		}
		printf("Case %d:\n",++p);
		if(len==1)
			printf("My king, at most %d road can be built.\n\n",len);
		else
			printf("My king, at most %d roads can be built.\n\n",len);
	}
	return 0;
}



杭电ACM1002-1025VC版个人解答 该文件包含了杭电ACM第1002至1025题得个人解题答案,有些答案包含了两种至三种解答。 立即下载

相关推荐

SWD下载调试接口原理深度剖析

作为ARM嵌入式工程师,下载调试器都应该知道,但你真正了解SWD如何下载调试的么?ARM 芯片通过什么物理接口和什么方式连接完全取决于芯片Debug子系统的架构如何?

zhuwade的博客 1万+

最长单调子序列例题

1. HDU 1025 Constructing Roads In JGShining's Kingdom 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...

zamaochick的博客 493

AMG8833红外测温实战:在STM32F103上做个简易人体感应与温度报警器

本文详细介绍了如何在STM32F103上使用AMG8833红外测温传感器构建人体感应与温度报警系统。通过硬件连接方案、驱动代码开发、人体检测算法实现及报警系统设计,展示了如何利用8x8红外温度矩阵实现智能监测功能。文章包含实用的初始化流程、温度数据读取优化和低功耗策略,为创客项目提供完整解决方案。

weixin_33725239的博客 402

杭电OJ 1025(C++)

本题基于最长递增子序列,使用二分查找使时间复杂度降为O(nlogn)。 #include <iostream> using namespace std; const int N = 500010; int sub[N]; //存放rich的最长递增子序列 int poor[N]; //存放poor对应的rich int binarySearch(int ans, int len) //二分查找rich在目前子序列中的位置 { int left = 1, right = len; int mid;

Intelligence1028的博客 674

Windows/Mac/Linux三系统保姆级教程:用llama.cpp将safetensors模型转gguf格式(附常见错误解决)

本文提供Windows、Mac和Linux系统下使用llama.cpp将safetensors模型转换为gguf格式的详细教程,涵盖环境配置、转换原理及常见错误解决方案。通过跨平台兼容性处理,帮助开发者高效完成模型格式转换,优化边缘计算性能。

weixin_29227425的博客 280

RTT Studio和Cubemx联合开发

1. RTT studio创建工程 创建工程 ## 创建完成以后的目录结构 2. 配置CubuMx 双击cubumx的图标打开CubeMx 配置时钟 生成代码 构建后的代码结构 编译代码满屏错误:不要慌 3. 新增脚本 新建scons脚本文件 SConscript 脚本内容如下 import os from building import * cwd = GetCurrentDir() src = Glob('*.c') # add cubemx drivers src = Split('''

Car12 1580

杭电OJ——1025 Constructing Roads In JGShining's Kingdom(比较有趣的一道题目,思路详解)...

Constructing Roads In JGShining's Kingdom 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...

编程语言小筑 321

电子商务网站设计系统详细设计与具体代码实现

电子商务网站设计系统详细设计与具体代码实现 作者:禅与计算机程序设计艺术 1. 背景介绍 1.1 电子商务的兴起与发展 近年来,随着互联网技术的飞速发展和普及,电子商务已经成为人们生活中不可或缺的一部分。从最初

AI天才研究院 2056

杭电1025

杭电1025

imbamizhou的专栏 617

CloudCompare 二次开发(36)——非均匀体素降采样

使用CloudCompare与PCL编程实现的非均匀体素降采样。

点云侠的博客 756

【短距离通信】【WiFi】精讲802.11n/ac/ax mpdu和ampdu最大长度

在 Wi-Fi 标准(802.11n、802.11ac 和 802.11ax)中,和是数据传输的核心概念。理解它们各自的作用以及不同标准下的最大长度限制,有助于深入了解 Wi-Fi 的性能优化机制。以下是对802.11n802.11ac和802.11ax中MPDU和A-MPDU最大长度的详细技术分析和对比。1. MPDU 和 A-MPDU 的基本概念MPDU 是经过 MAC 层处理后的数据单元,主要由MAC层数据帧和物理层的传输头部(PHY headers)组成。

风云说通信 1116

杭电ACM OJ 1025 Constructing Roads In JGShining‘s Kingdom 最长上升子序列 O(nlogn)算法详解+回溯暴力解决+动态规划dp优雅解决

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26933Accepted Submission(s): 7655 Problem Description JGShining's kingdom consists of 2n(n is no ...

u011250186的博客 402

【声纳技术】声呐时间反转镜技术:从被动聚焦到主动空间复用

时间反转镜技术以其独特的物理机制,为水声通信与声呐探测提供了一条绕过传统自适应均衡器的新路径。被动时间反转利用接收端的空间分集实现脉冲压缩,结构简洁、计算高效;主动时间反转则通过发射端预编码,将多径能量引导至目标位置,为多用户下行链路的空间复用奠定了物理基础。从Kuperman等人在海洋中首次验证声学时间反转镜,到TR-MIMO声呐的湖试验证,再到VTRM-CRK多用户通信系统的海试成功,该技术已从理论概念逐步走向工程实用。

u012133341的博客 77

【HDU 杭电OJ 1025 Constructing Roads In JGShining's Kingdom】

Constructing Roads In JGShining’s KingdomDescription 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 i

楚江枫 523

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

杭电OJ1025:Constructing Roads In JGShining's Kingdom

本题求得最长递增子序列,但是由于数据量很大所以要用复杂度为nlongn的方法求而不能用常规的dp方法求。 C++代码: #include #include using namespace std; const int N=500010; struct road{ int x,y; }arr[N]; bool cmp(road a,road b){ return a.x<b.x; } int

556

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

杭电OJ——1025 Constructing Roads In JGShining's Kingdom(比较有趣的一道题目,思路详解)

Constructing Roads In JGShining's Kingdom 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 the

李树花开,风中摇曳 4012

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Constructing Roads In JGShining's Kingdom(LIS)

http://acm.hdu.edu.cn/showproblem.php?pid=1025 题意:富人路与穷人路都分别有从1到n的n个点,现在要在富人点与穷人点之间修路,但是要求路不能交叉,问最多能修多少条。 思路:穷人路是按顺序给的,故求富人路的最长上升子序列即可。由于数据范围太大,应该用O(nlogn)的算法求LIS。 1 #include <stdio.h&gt...

weixin_30485291的博客 115

杭电oj1025-最长递增子序列

【最长递增子序列】     给定数列A1,A2,...An,求最长递增子序列 输入:     第一行一个整数n,表示有n个数(n     第二行n个整数,用空格隔开。 输出:     最长递增子序列长度。 【分析】      在求以Ai为末元素的最长递增子序列时,找到所有序号在Ai前面且小于Ai的元素Aj,即j     阶段i:以第i个数为末元素     状态S[i]:以第

我的代码小说 796

hdu 题目分类

<br />1001       这个就不用说了吧<br />1002       简单的大数<br />1003       DP经典问题,最大连续子段和<br />1004       简单题<br />1005       找规律(循环点)<br />1006       感觉有点BT的题,我到现在还没过<br />1007       经典问题,最近点对问题,用分治<br />1008       简单题<br />1009       贪心<br />1010       搜索题,剪枝很关键<br

悔惜晟 2127

hdu 1025 Constructing Roads In JGShining's Kingdom 深夜又一波DP,最长上升子序列(O(nlogn)算法)!尼玛坑爹的输出啊!!

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). Eac

Lionel 909

杭电oj1025题解析

/** *@author StormMaybin */写在前面 生活中没有退步。只要我们肯学,无论什么经验都是我们需要领悟的章节。 题目描述Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他

StormMaybin的博客 1492

HDU1025:Constructing Roads In JGShining's Kingdom(LIS)

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

ACM!荣耀之路! 2424

【HDU 1025】Constructing Roads In JGShining's Kingdom(最长上升子序列LIS)

DescriptionJGShining’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

Ice_Confuse 560

hdu 1025 Constructing Roads In JGShining's Kingdom(LIS)题意较难转换成LIS

1、http://acm.hdu.edu.cn/showproblem.php?pid=1025 题目好难懂,懂了之后也没想到这是一个LIS的题目,注意输出的细节,wrong了n遍 输出,注意一条多条的不同输出,样例之间打印空行 2、题目大意 有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有的城市之间富有的资源都不相同,贫穷的城市只有一种资源贫穷,

sdjzping的专栏 3272

Constructing Roads In JGShining's Kingdom(思维+NlogN求最长上升子序列)(好题)

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 other...

永夜莫明的博客 286

杭电 HDU ACM 1025 Constructing Roads In JGShining's Kingdom

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17732    Accepted Submission(s): 5023 Problem

舒哥的blog 1058
上一篇: 【POJ2533】Longest Ordered Subsequence
下一篇: 【杭电oj1257】最少拦截系统
better_space
博客等级 码龄11年 73粉丝 253原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值