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;
}
相关推荐
m0_629494731 小时前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户2 小时前
用队列实现栈
数据结构·算法
做人求其滴2 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣
daad7772 小时前
记一组无人机IMU传感器数据
算法
计算机安禾2 小时前
【c++面向对象编程】第42篇:模板特化与偏特化:为特定类型定制实现
开发语言·c++·算法
小O的算法实验室2 小时前
2026年KBS,流形感知强化学习差分进化算法+不规则3D无人机路径规划,深度解析+性能实测
算法·智能算法·智能算法改进
玖釉-2 小时前
C++ 中的循环语句详解:while、do...while、for、嵌套循环与循环控制
开发语言·c++·算法
不做无法实现的梦~3 小时前
运动控制系统复习一览-----常考题目总结版本
算法
小短腿的代码世界3 小时前
信号路由风暴:Qt算法交易系统的高频信号分发架构
qt·算法·架构
阿文的代码库3 小时前
一文读懂GROUP BY 1,2 VS GROUP BY column_1, column_2 的区别
算法