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] ...
随机推荐
- MISC中的图片修改宽高问题
在做CTF中MISC分类题目时,很常见的一个问题就是修改图片正确的宽与高 (此篇笔记中的内容以ctfshow中MISC入门分类为切入点,感兴趣的同学可以一边做一边有不会的看看,仅供参考,我是菜鸡) 曾 ...
- view-design table的renderHeader中hover添加checkboxGroup遇到的问题
示例demo https://codepen.io/sphjy/pen/mdKaQJZ 问题见图 勾选多个复选框 on-change事件返回的数据只有当前点击的这一项,而且设置在checkboxGro ...
- express 为所有路由添加 405 method not allowd 响应
背景知识 HTTP Status Code 405 405 Method not allowed The resource was requested using a method that is n ...
- Nmap扫描参数
执行Nmap/nmap --help查看帮助文档,将显示Namp的用法及其功能Nmap的相关参数的含义与用法:扫描目标时用到的参数:-iL:从文件中导入目标主机或目标网段-iR:随意选择目标主机--e ...
- js修改数组中的属性名
将数组 [{id:"1",name:"AAA"}] 修改为 ===> [{id:"1",text:"AAA",va ...
- Ajax---EventLoop事件循环
前言 JavaScript 是一门单线程执行的脚本语言.也就是说,同一时间只能做一件事情. JavaScript要运行在宿主环境中(浏览器,nodejs)下.浏览器内部有执行js代码的引擎 ...
- 【转载】EXCEL VBA 20个有用的ExcelVBA代码
1.显示多个隐藏的工作表 如果你的工作簿里面有多个隐藏的工作表,你需要花很多时间一个一个的显示隐藏的工作表. 下面的代码,可以让你一次显示所有的工作表 Sub UnhideAllWoksheets() ...
- Flink mini-batch "引发" 的乱序问题
问题描述 近期业务反馈, 开启了 mini-batch 之后, 出现了数据不准的情况, 关掉了 mini-batch 之后, 就正常了, 因此业务方怀疑,是不是 Flink 的 mini-batch ...
- MySQL 更新数据 不同条件(批量)更新不同值
一般在更新时会遇到以下场景:1.全部更新:2.根据条件更新字段中的某部分内容:3.根据不同的条件更新不同的值,以下是几种场景中常用的update方法. 一.方法分类 二.具体用法 (1)根据条件更新值 ...
- CSS文字超出宽度---换行总结
2021-5-5重新总结---CSS文字超出宽度---附代码 <!DOCTYPE html> <html lang="en"> <head> ...