Traffic Lights set的使用

题目描述

There is a street of length x whose positions are numbered 0,1,...,x. Initially there are no traffic lights, but n sets of traffic lights are added to the street one after another.

Your task is to calculate the length of the longest passage without traffic lights after each addition.

输入

The first input line contains two integers x (1 ≤ x ≤ 109)and n(1 ≤ n ≤ 2*105): the length of the street and the number of sets of traffic lights.

Then, the next line contains n integers p1,p2,...,pn(0 < pi < x): the position of each set of traffic lights. Each position is distinct.

输出

Print the length of the longest passage without traffic lights after each addition.

样例输入 Copy
复制代码
8 3
3 6 2
样例输出 Copy
复制代码
5 3 3

使用set 快速找到每个节点插入后的左右节点

multiset 删除原有路段并加入新路段,快速查找当前最大值

cin cout超时

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int x,n,p;
set<int>road;
multiset<int>dis;
int main(){
	scanf("%d%d",&x,&n);
	set<int>road{0,x};
    multiset<int>dis{x};
	for(int i=0;i<n;++i){
		scanf("%d",&p);
		road.insert(p);
		auto it=road.find(p);
		auto l=*prev(it);
		auto r=*next(it);
		dis.erase(dis.find(r-l));
		dis.insert(p-l);
		dis.insert(r-p);
		printf("%d ",*dis.rbegin());
	}
	return 0;
}
相关推荐
north_eagle20 分钟前
向量搜索技术深度研究报告:架构原理、核心算法与企业级应用范式
算法·架构
椰萝Yerosius1 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
小曹要微笑1 小时前
STM32F7 时钟树简讲(快速入门)
c语言·stm32·单片机·嵌入式硬件·算法
南山安1 小时前
栈(Stack):从“弹夹”到算法面试题的进阶之路
javascript·算法·面试
2301_764441332 小时前
Python构建输入法应用
开发语言·python·算法
AI科技星2 小时前
为什么变化的电磁场才产生引力场?—— 统一场论揭示的时空动力学本质
数据结构·人工智能·经验分享·算法·计算机视觉
TheLegendMe3 小时前
贪心+线程安全单例
算法·哈希算法
豐儀麟阁贵4 小时前
8.5在方法中抛出异常
java·开发语言·前端·算法
胖咕噜的稞达鸭4 小时前
算法入门:滑动窗口--->找到字符串中所有的字母异位词,串联所有的子串,最小覆盖子串
数据库·redis·算法
小青龙emmm4 小时前
2025级C语言第二次周测(国教专用)题解
c语言·开发语言·算法