首页 > 其他分享 >CF1418D Trash Problem

CF1418D Trash Problem

时间:2022-11-09 08:11:08浏览次数:62  
标签:insert lower pl2 int CF1418D bound Problem Trash pl

题目传送门

思路

这题其实非常的简单,完全到不了 \(\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\) 中是否还有值。

标签:insert,lower,pl2,int,CF1418D,bound,Problem,Trash,pl
From: https://www.cnblogs.com/tx-lcy/p/16871964.html

相关文章

  • CF464E The Classic Problem
    题意给定一张图,边权为\(2^x,x\le10^5\),求\(s\)到\(t\)的最短路以及方案。Solution直接上最短路!现在的问题是如何高效存储路径上权值的加和。这题有个特殊之处就是......
  • git submodule add 报错SSL certificate problem unable to get local issuer certifi
    在使用hugo并安装主题时遇到的错误SSLcertificateproblem:unabletogetlocalissuercertificate(base)PSE:\vscodeProject\chz8bit.github.io\quickstart>gitsu......
  • Simple Math Problem
    ​​传送门​​思路:题目中给出的矩阵均为16进制表示,根据规律输出对应10进制数即可。#include<bits/stdc++.h>usingnamespacestd;#definelllonglongconstintinf=0x3......
  • CF1685E The Ultimate LIS Problem 题解
    LinkCF1685ETheUltimateLISProblem题意概述给定长为\(2n+1\)的排列,对于\(m\)次交换操作,求出每次操作后一个能使排列\(\text{LIS}\leqn\)的循环移位\(k\),......
  • CF713C Sonya and Problem Wihtout a Legend
    题意给定一个有\(n\)个正整数的数组,一次操作中,可以把任意一个元素加一或减一。(元素可被减至负数或\(0\)),求使得原序列严格递增的求最小操作次数。题解首先有一个常规......
  • [leetcode] The Skyline Problem
      classSolution{publicList<int[]>getSkyline(int[][]buildings){List<int[]>res=newArrayList<>();List<int[]>height=newAr......
  • [COMP2119] Searching - Building and Egg Problem
    DescriptionThereisabuildingwith$n$floorsandyouhave$m$eggs.Determinethelowestfloorthrownfromwhichaneggwillbreak.Ifaneggisbroken,it......
  • 【CF802O】April Fools‘ Problem (hard)(wqs二分,模拟费用流,老鼠进洞)
    如果没有恰好为\(k\)的限制的话是个老鼠进洞的经典模型。加上恰好为\(k\)的限制后考虑使用wqs二分,因为费用流每次增广出来的费用是单调不降的。即如果设\(g(k)\)......
  • G. Periodic RMQ Problem
    G.PeriodicRMQProblem题目大意给你一个序列\(a\)让你支持\(1\)\(l\)\(r\)\(x\)区间赋值\(2\)\(l\)\(r\)询问区间最小值我们觉得这个问题太水了,所以我们......
  • [COMP2121] Bertrand's Ballot Problem
    DescriptionSupposethereare$x$votesfor$A$and$y$notesfor$B$,and$x>y$.Howmanysequencesarethereofrevealingthevotessuchthatthenumbervote......