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] ...
随机推荐
- Oracle 插入时间戳id函数func_getnewid()
在Oracle数据库中出入以时间戳为id的最简单方法运用func_getnewid()函数: select func_getnewid(30) from dual; id长度可调
- 【微服务架构设计实施】第一部分:架构篇-1:微服务架构与Spring Cloud介绍
〇.概述 一.微服务架构与Spring Cloud (一)概念 不同说法:细粒度的.清凉组件化的小型SOA(面向服务架构) 统一说法:小型应用程序(服务组件),使用轻量级设计方法和HTTP协议通信 理 ...
- SVNAdmin2 - 基于web的SVN管理系统
1. 介绍 SVNAdmin2 是一款通过图形界面管理服务端SVN的web程序. 正常情况下配置SVN仓库的人员权限需要登录到服务器手动修改 authz 和 passwd 两个文件,当仓库结构和人员权 ...
- Linux开发板连接WPA加密的AP路由器
Linux目前有两种方法配置网络: wireless-tools wpa_supplicant iw支持的驱动较多,但只支持WEP加密:wpa_supplicant有部分驱动支持不完善,但支持WEP. ...
- CGI、WSGI、uWSGI、ASGI……
在学习 Python Web 开发时候,可能会遇到诸如 uwsgi.wsgi 等名词,下面通过梳理总结,探究它们之间的关系. CGI CGI(Common Gateway Interface)通用网关 ...
- uniapp(vue)实现点击左侧菜单,右侧显示对应的内容
<template> <view class="container"> <view class="fication-search" ...
- P5380 [THUPC2019]鸭棋
题面 查看题面 题目背景 鸭棋是一种风靡鸭子界的棋类游戏.事实上,它与中国象棋有一些相似之处,但规则不尽相同.在这里,我们将为你介绍鸭棋的规则. 鸭棋在一个 \(10\times 9\)(\(10\) ...
- 如何通过Terraform Associate考试并获得证书
1 什么是Terraform? Terraform是一个IaC工具,IaC全称为Infrastructure as Code,基础设施即代码.它的理念是通过代码来管理基础设施,如服务器.数据库等,更多 ...
- VUEX 使用学习三 : mutations
转载请注明出处: 在 Vuex 中 store 数据改变的唯一方法就是提交 mutations.mutations里面装着一些改变数据方法的集合,这是Vuex 设计很重要的一点,就是把处理数据逻辑方法 ...
- 通过Sqoop来传输mysql/oracle/vertica数据至Hive
1.环境: CDH SQOOP2 2.操作步骤 1.准备表与主键 表名:test.test_log 主键:sys_log_id 建表语句 CREATE TABLE hbase_test_test_lo ...