hdu1025——Constructing Roads In JGShining's Kingdom

CLion与STM32CubeMX联调:JLink GDB Server高效调试指南 本文详细介绍了如何搭建并配置CLion、STM32CubeMX与JLink GDB Server联调的现代化STM32开发环境。通过一站式环境搭建、工程创建、核心调试配置及高级技巧(如RTT日志),该组合实现了高效的代码编辑、可视化配置与稳定调试,大幅提升嵌入式开发效率与体验。 阅读详情

Constructing Roads In JGShining's Kingdom

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


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  1074  1080  1059  1160 
 

转化很简单,对左边坐标从小到大排序,如果左边相等,再按右边从小到大排序,这样之后,左边一定有序,如果不能出现交叉的路,那么就是在右边寻找一个最长的不下降子序列,但是O(n^2)的做法会TLE

于是去网上学了O(logn)的做法,这里简单介绍下


我们假设所求序列放在数组d里,显然d是单调不下降的
对于当前尝试的a[i],如果a[i]比d[len]大或者相等,那么放入a[i],显然会得到一个更长的序列;如果a[i]小于d[len],就需要通过二分查找,查到大于a[i]的最小的数并且替换,为什么要这么做呢,我们可以在不减小序列长度的情况下,缩小序列元素的值,使得它更有可能纳入后面的元素从而使整个最长不下降序列的长度更长
对了,如果不是修改大于a[i]的最小的数,而是修改其他更大的数,那么就会导致d数组不是有序的,从而破坏了d的结构,那就无法求出最长不下降序列的长度了
还有一点要注意,最后求出来的d数组不是所求的最长不下降序列,例如  2 5 1,最后在d数组里的是 1 5,而真正的最长不下降序列是2 5

#include<map>
#include<set>
#include<list>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

struct node
{
    int l, r;
}road[500010];

int d[500010];
int arr[500010];

int cmp(node a, node b)
{
    if(a.l != b.l)
        return a.l < b.l;
    return a.r < b.r;
}

int main()
{
    int n;
    int icase = 1;
    while(~scanf("%d", &n))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &road[i].l, &road[i].r);
        }    
        sort(road, road + n, cmp);
        for(int i = 0; i < n; i++)
            arr[i] = road[i].r;
//      dp[i] = max(dp[j])+ 1
//       O(n^2)  TlE
/*        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < i; j++)
            {
                if(arr[i] > arr[j] && dp[i] < dp[j] + 1)
                    dp[i] = dp[j] + 1;
            }
            ans = max(ans, dp[i]);
        } 
*/
		memset( d, 0, sizeof(d) );
		int len = 0, l, mid, r;
		for(int i = 0; i < n; i++)
		{
			if(d[len] <= arr[i])
				d[++len] = arr[i];
			else
			{
				//二分查比arr[i]大的第一个数
			 	l = 1;
			 	r = len;
			 	int ans;
			 	while(l <= r)
			 	{
			 		mid = (l + r) >> 1;
	 				if(d[mid] > arr[i])
	 				{
				 		r = mid - 1;
				 		ans = mid;
				 	}
				 	else
				 	{
	 					l = mid + 1;
	 				}
	 			}
	 			d[ans] = arr[i];//替换 
			}
		}
        printf("Case %d:\n", icase++);
        if(len == 1)
        	printf("My king, at most %d road can be built.\n", len);
       	else
       		printf("My king, at most %d roads can be built.\n", len);
		printf("\n");
    }
    return 0;
}



Debezium系列之:使用指定值替代指定字段的值 阅读详情

相关推荐

Mac Studio M5 Ultra本地部署大模型实战:从内存原理到Ollama开发工作流

大模型推理的性能瓶颈往往不在算力,而在于内存能否装下整个模型、带宽能否持续供得上。Transformer架构每生成一个token都需要把权重完整扫一遍,决定了这本质上是内存密集型任务——统一内存架构因此成为本地部署的关键通路,超大容量与高带宽并行,让“装得下”和“跑得快”同时成立。模型量化技术(如Q4_K_M)则是另一项决定性软件策略,用轻微精度损失换取更低的硬件门槛,也是Ollama本地部署从7B到70B各类模型必做的前置功课。当本地推理服务跑起来后,借助VS Code等开发工具的OpenAI兼容API接

weixin_29043895的博客 453

hdu 1025 Constructing Roads In JGShining's Kingdom dp(LIS)

hdu 1025 Constructing Roads In JGShining's Kingdom http://acm.hdu.edu.cn/showproblem.php?pid=1025   二分求最长上升子序列,长度即可建路数   //hdu 1025 #include #include #include #include #include #include usi

倩 797

Adversarial Texture for Fooling Person Detectors in the Physical World

本文提出了对抗纹理的想法(AdvTexture),与基本补丁攻击不同对抗纹理可以生成任意大小,可以覆盖任意大小的不了,并且纹理的任何局部都有对抗性的效果。当衣服被对抗纹理覆盖是,摄像头捕捉到的每一个局部区域都可以攻击探测器,这成功解决了片段缺失的问题。提出了一种两阶段生成方法:基于环形收割的可扩展生成攻击(TC-EGA)来制作广告纹理。第一阶段,训练一个全卷积网络FCN作为生成器,通过采样随机潜变量作为输入来产生纹理(不同于GAN中传统的生成器架构,每一层都使用卷积运算)......

weixin_49171105的博客 1067

hdu 1025 Constructing Roads In JGShining's Kingdom

最长上升子序列 题意:有2条线,每条线上有n个点,现在给你一些连接方式,一条线的点去连接另外一条线上的点。问这些线不交叉,最多能连多少条线 #include #include #include #include #define maxn 500100 using namespace std; struct tt { int x,y; }A[maxn]; struct node {

HLSHYR的博客 540

Pyside6+qml+Qtcreator项目实战

PYTHON and QT QUICK - Custom Buttons With QML And JavaScript - [MODERN GUI]-#01

u014627020的博客 919

高通量测序数据分析:RNA-seq

深度测序相关数据库与数据格式 SRA toolkit 一、NCBI 和EBI、DDBJ组成INSDC,数据内容相同所以找NCBI就行。 (一)NCBI常用数据库 GenBank:遗传序列数据库,收集了所有公开的DNA序列及其注释 GEO (Gene Expression Omnibus) :收集整理各种表达芯片数据,后来加入了甲基化、lncRNA、miRNA、CNV等其他芯片,还有高通量测序数据...

qq_41134363的博客 3万+

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 these cities are rich in resource (we call them rich

fanesemyk的博客 417

10-142 6-4 查询厂商“D“生产的PC和便携式电脑的平均价格

分数 10全屏浏览题目切换布局作者 lhm单位 河北农业大学本题目要求编写SQL语句,查询厂商D生产的PC和便携式电脑的平均价格。

dqd66的博客 2748

HDU1025 Constructing Roads In JGShining's Kingdom

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 题意大概就是给你一大堆的城市,左边的数表示这个城市缺什么物资,右边的数表示那个物资从哪个城市往里运,两个城市之间需要链接道路,问不交叉的情况下,最多能连多少条。这道题原本应该是一个最长公共子序列,但是无奈他给的数据范围太大,LCS的做法明显不满足,所以首先我们可以把右边的数映射到左边来,这样就

浅雨歌 839

Zynq裸机调试RTL8211FS网卡踩坑记:从ping不通到完美兼容的完整代码修改

本文详细记录了在Zynq平台上使用Xilinx SDK(XSDK)调试RTL8211FS网卡的完整过程,从最初的ping不通问题到最终实现完美兼容的解决方案。通过分析PHY芯片的寄存器配置差异,提供了关键代码修改和调试技巧,帮助开发者快速解决类似问题。

weixin_42550597的博客 421

计算机组成原理(第三版)唐朔飞-第六章计算机的运算方法-课后习题(17-32)

计算机组成原理(第三版)唐朔飞-第六章计算机的运算方法-课后习题(17-32)

weixin_45735391的博客 12万+

【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

VTK API 详解(134):vtkOBJWriter - 写入 OBJ 模型

是 VTK 中用于将数据写入 Wavefront OBJ 文件格式的类。OBJ 格式是广泛支持的 3D 网格模型格式,支持顶点、面和基本几何信息的保存,便于模型在多种三维软件间交换。

yumeiguo的博客 140

HDU_1025_Constructing Roads In JGShining's Kingdom

HDU 1025 二分 Constructing Roads In JGShining's Kingdom

Sothan 708

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

VB摄像头监控

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

HDU 1025 Constructing Roads In JGShining's Kingdom

太强了 !!!!!!!!膜拜啊!! 摘: 链接:http://acm.hdu.edu.cn/forum/read.php?tid=15580&fpage=0&page=1 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5。 下面一步一步试着找出它。 我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列。 此外,我

诺诺罗亚 612

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

HDU Problem 1025 Constructing Roads In JGShining's Kingdom 【LIS】

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

博客已搬迁到http://www.cnblogs.com/cniwoq/ 344

HDU 1025 Constructing Roads In JGShining's Kingdom LIS -

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025 要用nlogn的算法,不然会超时 参见博客讲解:http://blog.youkuaiyun.com/kaitangshouljz/article/details/41074187?locationNum=7 #include #include #include #include using

BRCOCOLI的博客 304

HDU 1025 Constructing Roads In JGShining's Kingdom (LIS 最长递增子序列)

【题目链接】:click here~~ 【题意】:河岸两旁有n个村庄,他们之间要互相修路,并且同一边的不互相修,在保证不交叉的情况下,最大限度的路的是多少。 【思路】转化题意后,发现是求LIS,入门题训练 LIS详细分析: 代码: /* * Problem: HDU No.1025 * Running time: 374MS * Complier: G++ * Author:

herongwei 的 BLOG 1301

hdu 1025 Constructing Roads In JGShining's Kingdom dp

听说这是个LIS的题,画了画图,发现是这样的.. View Code #include <stdio.h>int ar[500005],vis[500000];int main(){ int n,i,j,k,m,p,r,l=1; while (scanf("%d",&n)!=EOF) { for (i=0;i<n;i++...

diaopi1575的博客 130

Constructing Roads In JGShining's Kingdom HDU - 1025(二分优化)

运用二分将时间复杂度n*n 优化到n*logn

_ispecter_的博客 243

DP 60题 -2 HDU1025 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 cities are rich in resource (we call them rich ci...

日拱一卒,功不唐捐 3172

HDU1025——LIS——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 cities are rich in resource (we call them ric...

dgjm4087的博客 141

HDU_1025Constructing Roads In JGShining's Kingdom

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 ot

小小梅花 665

HDU 1025 Constructing Roads In JGShining's Kingdom(动态规划)

题目链接:Click here题意:这道题目看了好几遍,英语是硬伤啊~~借助了谷歌翻译~~自己修改了一下,毕竟电脑翻译的真不忍直视啊JGShining的王国包括2n个小城市,分别位于两条平行线。 这些城市中有一半都含有丰富的资源(我们称他们丰富的城市),而另一些缺少资源(我们称他们为穷人市)。每个贫困城市缺少一种资源,每一个富有城市富有一种资源。你可以假设没有任何两个城市同时缺少一个同类型的资源,并

工厂搬砖的代码小白 464
上一篇: hdu1242——Rescue
下一篇: hdu5023——A Corrupt Mayor's Performance Art
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值