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] ...
随机推荐
- 《MySQL必知必会》知识汇总二
六.用通配符进行过滤 本章介绍什么是通配符.如何使用通配符以及怎样使用LIKE操作符进行通配搜索 LIKE操作符 百分号(%)通配符 select prod_id,prod_name from pro ...
- 使用Google OR-Tools分析过去20年中国金融资产最佳配置组合
前两天,在朋友圈里看到一张截至2022年Q2的金融资产历年收益图如下,图中列举了国内从2005年到2022年近20年主要的金融资产历年收益率,随产生想法分析和验证下面几个问题: 过去20年,基于怎样的 ...
- Safari浏览器对SVG中的<foreignObject>标签支持不友好,渲染容易错位
在 svg 中需要写一个 markdown 编辑器,需要用到 <foreignObject> 绘制来html,编辑器选择了 simplemde.大致html部分结构如下,<markd ...
- 如何自定义调整bootstrap的模态框大小
背景 项目遇到一个需求,一个大表格放到模态框中,总是会出现撑开的效果,换了文档最大的modal-lg样式还不能解决,原因就是官方不支持更大号的模态框,需要自定义. 经过尝试理解,总结出调整模态框大小通 ...
- AVM 拖动组件 movable-view 介绍
应用开发中拖动功能是比较常见的 ,如滑动解锁,滑动验证码,实现一些小游戏,少儿编程中常见. avm.js 是多端开发框架,一套代码可同时编译为APP .小程序.h5. avm 框架中实现拖动功能非常简 ...
- python进阶之路9文件的处理方法
内容回顾 字典内置方法 1.类型转换 dict() 2.重要操作 get() d[key] = values 常用 pop() update() 键存在则修改键值对 键不存在则新增键值对 fromke ...
- [LeetCode]数组拆分 I
题目 代码 class Solution { public: int arrayPairSum(vector<int>& nums) { std::sort(nums.begin( ...
- 使用JS实现复制粘贴功能
使用JS实现复制粘贴功能 如果嵌套太多使用这个: // 複製單號1 // 第一步把這個放到頁面 // <div style="position:absolute; opacity: 0 ...
- CSS-@规则(At-rules)常用语法使用总结
At-rules规则是目前CSS中一种常见的语法规则,它使用一个"@"符号加一个关键词定义,后面跟上语法区块,如果没有则以分号结束即可. 这种规则一般用于标识文档.引入外部样式.条 ...
- 【学习笔记】C/C++ 设计模式 - 模板模式
介绍说明 模板设计模式是一种非常简单的设计模式,其主要是利用了虚函数的特性实现.非常适合应用在一些算法.流程.业务逻辑是固定的形式,其中某些步骤的实现方式又无法确定下来的场景. 举例说明 以下为模拟某 ...