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;
}
相关推荐
默默无名的大学生15 小时前
数据结构——链表的基本操作
数据结构·算法
Neverfadeaway15 小时前
C语言————冒泡排序(例题2)
c语言·数据结构·算法·冒泡排序·升序排列·降序排列
惊鸿.Jh15 小时前
1733. 需要教语言的最少人数
算法·leetcode
大千AI助手16 小时前
SPEA2多目标进化算法:理论与应用全解析
算法·多目标优化·种群·spea2·mop·moea·帕累托最优
AndrewHZ16 小时前
【图像处理基石】图像处理中的边缘检测算法及应用场景
图像处理·算法·计算机视觉·cv·算子·边缘检测
熊文豪17 小时前
【华为OD】区块链文件转储系统
算法·华为od·区块链
塔中妖17 小时前
【华为OD】Linux发行版的数量
linux·算法·华为od
熊文豪17 小时前
【华为OD】阿里巴巴找黄金宝箱
算法·华为od
bestadc17 小时前
LeetCode 几道 Promises 和 Time 的题目
javascript·算法·leetcode
墨染点香17 小时前
LeetCode 刷题【71. 简化路径】
算法·leetcode·职场和发展