HDU1025 nlogn的LIS

一文搞懂 RC Correlation Scale Factor 的生成与应用 pre-route 用的是统计估算模型,post-route 用的是详细布线提取,signoff 用的是第三方 QRC 或 StarRC——三个工具,三套参数体系,结果自然天差地别。不同 RC corner(rc_worst/rc_best/rc_typical)对应不同的工艺角和温度环境,每个 corner 的 R/C 偏差比例不同,必须分别生成和应用各自的 scale factor。别被名字骗了,这个工具专门干一件事——统计拟合两组 SPEF 之间的偏差,自动推荐最优 scaling factor。 阅读详情

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.

这题就是升级版lis
按照之前的思路,是肯定超时的因为有50万的数据量
普通的n^2LIS的思路是开一个五十万大小的DP数组
dp[500001][2】这样的数组,第一个用来记录当前最小值,第二个用来记录当前的长度,当遍历的目前值两个都比过去的大的时候就进行更新…..
以前觉得这个LIS真的挺好的,现在看看真的蠢

看了别人的博客以后发现了新的方法
那就是维护一个dp【500001】一个就够了,地址用来存len的长度,里边的值是对应长度里最小的那个….
因为长度这个东西是一定连续的所以不存在浪费内存这一回事…遍历起来也方便
用了这个以后,就能发现这个DP数组里边存的数是单调递增的这也是可以证明的

来我们非形式化的证明一下子
这个循环不变式的初始化是由一个空的都是0的数组组成
然后迭代一次第一个数一定比0大也就是比dp【0】大,那么就一定会自动更新dp【1】;
后边的多次迭代只会把每一个值弄得比自己更小,但是后一个也一定比前一个的更新前的值更大,因此在循环不变式结束的时候这个数组一定也是单调递增的

因此这时候直接上二分法就是logn辣~~~~

这个二分写的我感觉也有些别扭,因为他不是从里边找一个数而是找到一个小区间…

然后我就写的非常奇怪…
不过以后可以记得找区间和找本数是一样的….
最后头=尾的地方一定就是要更新的值,头比整个序列还大的时候就说明没有比他大的了….
直接开新的位置就可以了。没必要拿出来单独写

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
using namespace std;
int zhi[500001];
int dp[500001];
int len;
int main()
{
    int n;
    int ji = 0;
    while (cin >> n)
    {
        memset(dp, 0, sizeof(dp));
        len = 0;
        int y,o;
        for (int a = 1;a <= n;a++)
        {
            scanf("%d%d", &y,&o);
            zhi[y] = o;
        }
        for (int a = 1;a <= n;a++)
        {
            if (zhi[a] > dp[len])
            {
                len++;
                dp[len] = zhi[a];
            }
            else
            {
                int tou=1, wei=len;
                int w = 0;
                while (tou<=wei&&w==0)
                {
                    int mid = (tou + wei) / 2;
                    if (zhi[a] > dp[mid])tou = mid + 1;
                    else
                    {
                        if (zhi[a] > dp[mid - 1])
                        {
                            dp[mid] = zhi[a];
                            w = 1;
                        }
                        else wei = mid - 1;
                    }
                }
            }
        }
        if(len==1)printf("Case %d:\nMy king, at most 1 road can be built.\n\n", ++ji);
        else printf("Case %d:\nMy king, at most %d roads can be built.\n\n", ++ji,len);
    }
    return 0;
}
机器学习(三) ----------线性回归算法(梯度下降+正则化) 线性回归算法在机器学习中具有核心地位,它以其简单性和直观性为基础,为理解数据间的关系提供了基本框架。作为许多复杂算法的基础,线性回归不仅用于预测目标变量,还用于解释自变量对目标的影响。同时,它也是机器学习入门的经典算法,对于推广机器学习知识和技术具有重要意义。 阅读详情

相关推荐

手把手教你搞定Pattern Recognition期刊的LaTeX投稿(附完整模板下载与避坑指南)

本文详细指导如何在Pattern Recognition期刊上使用LaTeX进行投稿,包括模板下载、环境配置、核心排版规范及投稿材料准备。特别强调期刊对格式的严格要求,如数学符号、图像引用等细节,并提供实用技巧和避坑指南,帮助作者高效完成投稿流程。

weixin_30876945的博客 398

最长单调子序列例题

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

基于PaddleX的垃圾分类识别

基于PaddleX的智能垃圾分类识别项目,使用百度开源的PaddlePaddle深度学习框架,结合PaddleX开发套件,实现对垃圾分类的识别检测

Mefishes的博客 8131

HDU 1025.Constructing Roads In JGShining's Kingdom【最长上升子序列n×logn算法】【1月6】

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 c

Mr丶Gao 1325

HMC5883L磁力计驱动开发:I²C通信、校准与FreeRTOS集成

磁力计是惯性导航与电子罗盘系统的核心传感器,其输出原始磁场数据需经精确采集、单位换算与误差补偿才能用于姿态解算。基于各向异性磁阻(AMR)原理的三轴磁力计,如HMC5883L,依赖稳定I²C通信获取12位有符号磁场采样值,并通过增益配置、硬软铁校准及温度补偿提升物理量精度。在嵌入式实时系统中,驱动层须保障数据的时间一致性与抗干扰鲁棒性,避免总线挂死、字节错位或量程误配等典型问题。本文围绕HMC5883L这一广泛使用的停产但仍在产线服役的磁传感器,详解寄存器配置、定点单位转换、八方位校准算法及FreeRTOS

weixin_42594427的博客 150

Zotero内存占用与存储优化:从根源解决卡顿与空间不足

文献管理软件在学术工作中扮演着重要角色,其核心原理是通过数据库管理元数据与附件文件,实现文献的快速检索与组织。然而,随着文献库规模增长,软件的资源管理机制可能面临挑战,尤其是内存占用与存储空间优化问题。从技术价值看,高效的附件管理策略不仅能提升软件响应速度,还能确保数据安全与可移植性。在实际应用场景中,研究者常遇到软件卡顿、存储目录膨胀等痛点,这通常与附件存储模式、缓存机制及数据库维护有关。本文聚焦于Zotero这一主流工具,针对其内存占用高、文件无法删除等常见问题,提供了外置存储、链接附件、数据库维护等系

weixin_34209060的博客 518

B - Constructing 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 othe...

Cworld2017的博客 413

高通MBIM拨号流程简述

高通平台MBIM流程简述

不秃头的程序猿Gabriel的博客 5073

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

RK3399平台开发系列讲解(外设篇)交换芯片RTL8306M

网络:交换芯片RTL8306M 电路: make menuconfig 配置: 目录:arch/arm64/configs/rk3399_b201_defconfig CONFIG_FIXED_PHY=y 设备树: arch/arm64/boot/dts/rockchip/rk3399-b201-rev1.dts 时钟: Open之后首先校验网卡mac地址是否正确(stmmac_chec...

内核笔记 2402

ATECC508A芯片开发笔记(四):自定义配置508功能,规划DataZone数据存储

ATECC508A芯片开发笔记(四):自定义配置508功能,规划DataZone数据存储 508A内部存储区介绍 Data Zone Config Zone 508A Config Zone配置(自定义) - ATECC508A芯片在使用之前,需要自定义配置ConfigZone与DataZone,并同Lock命令将两个区域锁住,否则无法正常使用功能。而一旦锁上就是永久性的,配置区不能够进行解锁

HowieXue 薛永浩的博客 2万+

HDU1025最长上升子序列问题

HDU25 最长上升子序列问题 下面向上面边线,只要保证从左到右目的连接目的地是按序上升即可保证不交叉,此问题则转化成了最长上升子序列问题。 /* * hdu 1025 * 请求序列构成按序增加即可达到连线不相交,序列长度即题目解,因此题目转化为求最长上升子序列问题。 * 注意:仅有一条路时,为road,否则为roads。 */ #include <iostream> #in...

qq_24489251的博客 222

23种设计模式-代理(Proxy)设计模式

软考中级-软件设计师 代理设计模式

片言可决讼,半纸能缚龙 1788

hdu 1025 LIS nlogn复杂度

#include #include #include #include #include #include #include #include #include #include using namespace std; #define INF 0x4fffffff #define LL long long #define MAX(a,b) ((a)>(b))?(a):(b)

点羽染墨 639

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)

题目链接 第一次写nlogn复杂度的LIS,纪念一下。 题目意思是说。有两条平行线,两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市。输入n,接下来有n行,p,r表示穷城市p和富有城市r 之间可以建一条路(p的顺序是1--n,一个贫穷城市只对应一个富有城市(弱爆的语文描述能力T_T)),公路不能交叉。 问最多可以建多少条公路。

SOUTH的专栏 969

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

HDU 1025 Constructing Roads In JGShining's Kingdom LIS -

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

BRCOCOLI的博客 304

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)

题目链接 第一次写nlogn复杂度的LIS,纪念一下。 题目意思是说。有两条平行线,两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市。输入n,接下来有n行,p,r表示穷城市p和富有城市r 之间能够建一条路(p的顺序是1--n,一个贫穷城市仅仅相应一个富有城市(弱爆的语文描写叙述能力T_T))。公路不能交叉。 问最多能够...

weixin_30586257的博客 92

HDU 1025 Constructing Roads In JGShining's Kingdom【LIS_nlogn】

题意: 给你两条街,上面的街编号从 1-n 下面的也是,然后告诉你他们的一些边,     让你从中尽可能选出多的边,使他们两两之间没有交点。 分析: LIS 的变形,把一条街看成有序的,求令一条街的最长递增子序列,     因为当 上面 i < j 时 有 f[i]<f[j]。 View Code #include<stdio.h> #include&lt...

weixin_34256074的博客 124

HDU 1025 Constructing Roads In JGShining's Kingdom (DP----LIS 的nlogn算法)

链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1025 开始用N^2的方法总超时 , 后来找到了nlongn的算法,就可以过去了 参考---  http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 就是利用一个数组,下标代表当前LIS的长度  其值代表 当前长度的

Napoleon 1013

HDU - 1025(LIS-O(nlogn)) - 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 ri

j6635788的博客 227

HDU 1025 Constructing Roads In JGShining's Kingdom(LIS nlogn)

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) whi

reb0rn 250

HDU 1025 Constructing Roads In JGShining's Kingdom(LIS的O(nlogn)算法)

题目地址:点击打开链接 题意:一条河的2边住着穷人和富人,一个富人只打算和一个特定穷人之间建一座桥,问最多能建多少个桥 思路:O(n^n)的会超时,主要思想看后面大神解析 错误代码: #include #include #include #include #include #include #include #include #include #include #inc

Java识堂,一个有干货的微信公众号 325

HDU1025-最长上升子序列(LIS)模板题+O(nlogn)二分查找优化

题目链接 题目大意:在两条平行线间连点,要在线不交叉的前提下尽可能多的连线,问最多能连多少条线。 这题是最长上升子序列(LIS)模板题,我们先给出最基础的O(n2)O(n^2)O(n2)动态规划算法: 设a为一个序列,a[i]为序列a从左往右第i个元素;设dp[i]为----序列a中以元素a[i]结尾的所有子序列中的最大上升子序列 状态转移方程: dp[i] = max { dp[j] | j<i 且 a[j]<a[i] } + 1 ,( i从1到n,j从1到i-1 ) 模板如下: in

Prince的博客 336

HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)

#include&lt;iostream&gt; using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; //dp[i]保存的是长度为i的最长不降子序列的最小尾元素 int BS(int n,int x) //二分查找下标,当x比全部元素小时下标为1,比全部元素大时下标为n+1. {...

weixin_33852020的博客 159

hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列(LIS)O(nlogn)算法)

这一题很容易抽象成最长上升子序列问题: 最长上升子序列问题: 给出一个由n个数组成的序列x[1..n],找出它的最长单调上升子序列。即求最大的m和a1,a2……,am,使得a1<a2<……<am且x[a1]<x[a2]<……<x[am]。 动态规划求解思路分析:(O(n^2)) 经典的O(n^2)的动态规划算法,设A[i]表示序列中的第i个数...

weixin_30247781的博客 90
上一篇: POJ1384完全背包
下一篇: HDU1950LIS裸题
NineFailure
博客等级 码龄11年 22粉丝 317原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值