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;
}
相关推荐
一念&4 小时前
每日一个C语言知识:C 头文件
c语言·开发语言·算法
Miraitowa_cheems7 小时前
LeetCode算法日记 - Day 88: 环绕字符串中唯一的子字符串
java·数据结构·算法·leetcode·深度优先·动态规划
B站_计算机毕业设计之家8 小时前
python电商商品评论数据分析可视化系统 爬虫 数据采集 Flask框架 NLP情感分析 LDA主题分析 Bayes评论分类(源码) ✅
大数据·hadoop·爬虫·python·算法·数据分析·1024程序员节
小白菜又菜8 小时前
Leetcode 1518. Water Bottles
算法·leetcode·职场和发展
长存祈月心9 小时前
Rust Option 与 Result深度解析
算法
杭州杭州杭州9 小时前
机器学习(3)---线性算法,决策树,神经网络,支持向量机
算法·决策树·机器学习
不语n11 小时前
快速排序(Quick Sort)详解与图解
数据结构·算法·排序算法·快速排序·双指针排序
电鱼智能的电小鱼11 小时前
基于电鱼 ARM 工控机的AI视频智能分析方案:让传统监控变得更聪明
网络·arm开发·人工智能·嵌入式硬件·算法·音视频
初学者,亦行者11 小时前
Rust性能优化:内存对齐与缓存友好实战
算法·rust