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;
}
相关推荐
涉密IT资质笔记几秒前
涉密资质投入产出分析模型:时间成本、四块成本构成与 ROI 决策框架
java·开发语言·算法
木井巳3 分钟前
【BFS 解决拓扑排序】火星词典
java·算法·leetcode·广度优先·宽度优先·图搜索算法
北风toto14 分钟前
KMP 算法详解:从暴力匹配到 O(n+m) 的字符串搜索
java·开发语言·算法
林澈在路上21 分钟前
哪个写歌软件可以用自己声音?MELO音乐详解
人工智能·python·算法
爱读书的小胖21 分钟前
Codewars Sum of a Sequence Hard Core Version C 解题分析
c语言·数据结构·算法
不会就选b22 分钟前
算法日常・每日刷题--<动态规划>24
算法·动态规划
Shaoshing33 分钟前
二分模板(左右查询)
java·算法·二分
蒲公英eric42 分钟前
线性回归与正则化:从最小二乘到岭回归、LASSO
人工智能·算法·数学建模·线性回归·岭回归·最小二乘·lasso
碧海银沙音频科技研究院5 小时前
炬芯ATS3089X手表的HFP通话不自动路由到设备BT的问题解决
网络·人工智能·嵌入式硬件·算法