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;
}
相关推荐
nike0good3 分钟前
The 4th Universal Cup GP of Kyoto, April 4-5, 2026 题解
算法·深度优先·图论
澈20736 分钟前
高效查找算法详解:从顺序到哈希
数据结构·算法·哈希算法
kobesdu1 小时前
开源3D激光SLAM算法的异同点、优劣势与适配场景总结
算法·3d·机器人·ros
ZC跨境爬虫1 小时前
3D 地球卫星轨道可视化平台开发 Day13(卫星可视化交互优化+丝滑悬停聚焦)
前端·算法·3d·json·交互
水木流年追梦1 小时前
CodeTop Top 100 热门题目(按题型分类)
算法·leetcode
Tisfy1 小时前
LeetCode 1722.执行交换操作后的最小汉明距离:连通图
算法·leetcode·dfs·题解·深度优先搜索·连通图
不知名的老吴1 小时前
案例教学:最长递增子序列问题
数据结构·算法·动态规划
样例过了就是过了1 小时前
LeetCode热题100 杨辉三角
c++·算法·leetcode·动态规划
念越1 小时前
算法每日一题 Day05|双指针解决盛最多水的容器问题
算法·力扣
eggrall1 小时前
Leetcode 最大连续 1 的个数 III(medium)
算法·leetcode·职场和发展