CF1418D Trash Problem
思路
这题其实非常的简单,完全到不了 \(\mathcal *2100\)。
发现这个题目描述有点诈骗,但是翻译的挺不错,实质上问题就是给你 \(n\) 个点,让你动态维护相邻两个点的差值,最后答案即为 \(\max-\min-\) 最大差值。
于是我们可以二分套动态开点权值线段树或者直接 \(\mathcal multiset\) 瞎搞。
为了简单,可以使用 \(\mathcal multiset\),但是注意插入和删除都需要分类讨论,思路非常简单,可能码量有一点点长。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int const N=1e5+10;
int p[N];
multiset<int>s,S;
inline void cr(int x){
if (!s.size()){s.insert(x);return;}
if (s.lower_bound(x)==s.end()){
int pl=*(--s.lower_bound(x));
S.insert(x-pl);
}else if (s.lower_bound(x)==s.begin()){
int pl=*s.begin();
S.insert(pl-x);
}else{
int pl1=*(--s.lower_bound(x));
int pl2=*(s.lower_bound(x));
S.erase(S.lower_bound(pl2-pl1));
S.insert(x-pl1);S.insert(pl2-x);
}
s.insert(x);
}
inline void del(int x){
s.erase(s.lower_bound(x));
if (!s.size()) return;
if (s.lower_bound(x)==s.end()){
int pl=*(--s.lower_bound(x));
S.erase(S.lower_bound(x-pl));
}else if (s.lower_bound(x)==s.begin()){
int pl=*s.begin();
S.erase(S.lower_bound(pl-x));
}else{
int pl1=*(--s.lower_bound(x));
int pl2=*(s.lower_bound(x));
S.insert(pl2-pl1);
S.erase(S.lower_bound(x-pl1));
S.erase(S.lower_bound(pl2-x));
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,q;cin>>n>>q;
for (int i=1;i<=n;++i) cin>>p[i],s.insert(p[i]);
sort(p+1,p+n+1);
for (int i=2;i<=n;++i) S.insert(p[i]-p[i-1]);
if (s.size()<=1) cout<<"0\n";
else cout<<(*(--s.end()))-(*s.begin())-(*(--S.end()))<<'\n';
while (q--){
int opt,x;cin>>opt>>x;
if (opt==1) cr(x);
else del(x);
if (s.size()<=1) cout<<"0\n";
else cout<<(*(--s.end()))-(*s.begin())-(*(--S.end()))<<'\n';
}
return 0;
}
应该非常好理解,插入和删除的分讨是一样的,还有输出答案需要判 \(\mathcal set\) 中是否还有值。
CF1418D Trash Problem的更多相关文章
- CodeForces 1418D Trash Problem
题意 数轴上有 \(n\) 个点,每一次你可以将所有位置在 \(x\) 的点移动到 \(x-1\) 或者是移动到 \(x+1\),花费为 \(1\). 有 \(q\) 次操作,每一次会在数轴上添加一个 ...
- CodeForces 题目乱做
是个补题记录. 1419 除了 F 场上都过了. CF1419A Digit Game 这题好多人 FST 啊-- 考虑如果串长为奇数那么最后操作的肯定是第一个人,串长为偶数的最后操作的肯定是第二个, ...
- URAL 1076 Trash Trash(最大权匹配)
Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
随机推荐
- HTTP2 协议长文详解
一.HTTP2 简介 HTTP2 是一个超文本传输协议,它是 HTTP 协议的第二个版本.HTTP2 主要是基于 google 的 SPDY 协议,SPDY 的关键技术被 HTTP2 采纳了,因此 S ...
- MISC相关刷题记录迁移
- 【Day01】Spring Cloud入门-架构演进、注册中心Nacos、负载均衡Ribbon、服务调用RestTemplate与OpenFeign
〇.课程内容 课程规划 Day1 介绍及应用场景 Day2 组件介绍及 广度 Day3 设计思想.原理和源码 Day4 与容器化的容器(服务迁移.容器编排) 一.业务架构的演进 1.单体架构时代 缺陷 ...
- python random模块几个常用方法
python random模块几个常用方法 random.random()方法 random.uniform(a, b)方法 random.randint(a, b)方法 random.randran ...
- Relational Learning with Gated and Attentive Neighbor Aggregator for Few-Shot Knowledge Graph Completion 小样本关系学习论文解读
小样本知识图补全--关系学习.利用三元组的邻域信息,提升模型的关系表示学习,来实现小样本的链接预测.主要应用的思想和模型包括:GAT.TransH.SLTM.Model-Agnostic Meta-L ...
- Vue3.0 生命周期
所有生命周期钩子的this上下文都是绑定至实例的. beforeCreate:在实例初始化之后.进行数据帧听和事件/侦听器的配置之前同步调用. created:实例创建完成,主要包括数据帧听.计算属性 ...
- 【转载】【WinAPI】LockWindowUpdate的函数的用法
DelPhi LockWindowUpdate的函数的用法 Application.ProcessMessages; LockWindowUpdate(Self.Handle); //锁住当前窗口 L ...
- 对Asp.net WebApi中异步(async+await)接口实际使用及相关思考(示例给出了get,post,提交文件,异步接口等实践).
[很多初学者的疑问] 为何作为web api这样的天然的并发应用,还需要在controller的action上声明使用async这些呢? <参考解答> 在 web 服务器上,.NET Fr ...
- [python]《Python编程快速上手:让繁琐工作自动化》学习笔记4
1. 处理Excel 电子表格笔记(第12章)(代码下载) 本文主要介绍openpyxl 的2.5.12版处理excel电子表格,原书是2.1.4 版,OpenPyXL 团队会经常发布新版本.不过不用 ...
- 宝塔SSL
1:开启面板SSL访问教程 开启完之后,要以 https 方式访问面板 一定要先 下载证书 和 复制证书密码 再点"开启ssl",不然得重置再走一遍流程. [教程贴]如何开启面板S ...