当前位置: 首页 > news >正文

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超时

代码 

#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;
}

http://www.lryc.cn/news/600704.html

相关文章:

  • AI Agent开发学习系列 - langchain之LCEL(5):如何创建一个Agent?
  • Ansible列出常见操作系统的发行版,Ansible中使用facts变量的两种方式
  • 定义域第一题
  • InfluxDB Flux 查询协议实战应用(二)
  • 修改site-packages位置与pip配置
  • 网络:应用层
  • docker安装问题汇总
  • 一文速通《多元函数微分学》
  • AI Agent开发学习系列 - langchain之LCEL(4):Memory
  • x86汇编语言入门基础(三)汇编指令篇5 串操作
  • 【架构】Docker简单认知构建
  • JAVA学习-练习试用Java实现“深度优先搜索(DFS):实现八数码问题的解法(最短路径搜索)”
  • LangChain4j低阶+高阶Api+日志配置+监听器+重试机制+超时机制
  • 【LeetCode 热题 100】131. 分割回文串——回溯
  • 算法竞赛阶段二-数据结构(35)数据结构单链表模拟实现
  • Android-广播详解
  • golang实现一个定时引擎,功能包括按照corntab的时间任务实时增加、修改、删除定时任务
  • 常见sql深入优化( 二)
  • 一文学会c++list
  • 激光雷达-相机标定工具:支持普通相机和鱼眼相机的交互式标定
  • 二叉搜索树(Binary Search Tree)详解与java实现
  • Linux 如何统计系统上各个用户登录(或者登出)记录出现的次数?
  • Android-三种持久化方式详解
  • 摘录-打造第二大脑
  • J2EE模式---表现层集成模式
  • C++ TAP(基于任务的异步编程模式)
  • Web后端进阶:springboot原理(面试多问)
  • React入门学习——指北指南(第五节)
  • JavaScript手录06-函数
  • 【RK3568 PWM 子系统(SG90)驱动开发详解】