当前位置: 首页 > 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.dtcms.com/a/299443.html

相关文章:

  • CSS变量与Houdini自定义属性:解锁样式编程新维度
  • Go 语言函数设计原则:避免修改传入参数
  • MCU中的GPIO(通用输入/输出)是什么?
  • [Qt]QString隐式拷贝
  • 利用DeepSeek解决kdb+x进行tpch测试的几个问题及使用感受
  • 系统架构设计师-【2025年上半年案例题】-真题分享
  • unittest 案例执行顺序详解
  • [SAP ABAP] ALV报表练习4
  • FreeRTOS-事件组
  • Cortex-M3内核SysTick定时器介绍
  • `munmap`系统调用及示例
  • 柔性智造:华控智能的垂直整合定制方案
  • 微服务springcloud http客户端feign
  • 伟淼科技李志伟:破解二代接班传承困局,系统性方案破除三代魔咒
  • Redis缓存策略以及bigkey的学习(九)
  • C语言——学习笔记
  • 数据结构(4)单链表算法题(上)
  • Linux DNS 服务器正反向解析
  • 深入分析计算机网络传输层和应用层面试题
  • 从压缩到加水印,如何实现一站式图片处理
  • 编程语言Java——核心技术篇(四)集合类详解
  • 从0开始学linux韦东山教程Linux驱动入门实验班(5)
  • C语言中:形参与实参的那些事
  • 分类预测 | MATLAB实现CPO-SVM冠豪猪算法优化支持向量机分类预测
  • 分类预测 | MATLAB实现DBO-SVM蜣螂算法优化支持向量机分类预测
  • pyskl-Windows系统使用自己的数据集训练(一)
  • 《C++ list 完全指南:从基础到高效使用》
  • 【洛谷】单向链表、队列安排、约瑟夫问题(list相关算法题)
  • 扣子(Coze)宣布开源两大核心项目——Coze Studio(扣子开发平台)和Coze Loop(扣子罗盘),附安装步骤
  • ubuntu下docker安装thingsboard物联网平台详细记录(附每张图)