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;
}
相关推荐
与己斗其乐无穷1 天前
刷题记录(11)map和set的简单使用
算法
夜月yeyue1 天前
个人写HTOS移植shell
c++·mcu·算法·性能优化·架构·mfc
Nix Lockhart1 天前
《算法与数据结构》第七章[算法3]:图的最小生成树
c语言·数据结构·算法
十重幻想1 天前
PTA6-5 使用函数求1到10的阶乘和(C)
java·c语言·算法
名誉寒冰1 天前
【LeetCode】454. 四数相加 II 【分组+哈希表】详解
算法·leetcode·散列表
十重幻想1 天前
PTA6-4 使用函数统计指定数字的个数(C)
c语言·c++·算法
格林威1 天前
机器视觉的工业镜头有哪些?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
夏鹏今天学习了吗1 天前
【LeetCode热题100(35/100)】LRU 缓存
算法·leetcode·缓存
拾光Ծ1 天前
【C++】STL有序关联容器的双生花:set/multiset 和 map/multimap 使用指南
数据结构·c++·算法
西望云天1 天前
The 2023 ICPC Asia Shenyang Regional Contest(2023沈阳区域赛CEJK)
数据结构·算法·icpc