首页 > 其他分享 >CF1732D2 Balance (Hard version) 题解

CF1732D2 Balance (Hard version) 题解

时间:2022-10-25 11:33:48浏览次数:58  
标签:Balance int 题解 CF1732D2 ch del ex res include

前天打了波CF结果怒掉100分,果然退化得太厉害了,遂于昨晚补题.

题面

维护一个集合 SS ,有三种操作:插入一个数、删除一个数、查询 kk 的倍数中没出现过的最小的数。

思路

考试的时候想了一个瞎jb倍增的做法,结果显然是错的(悲)
赛后想了个貌似复杂度不对的算法(能过就行,甚至还比大部分人快).
就是对每一个数x,在询问它的时候更新与x对应的一个集合exist(用map实现),这个集合维护已经在当前插入的数中(记为S)有的x的倍数(但是这个貌似根本只需要一个vector就行了,因为保证他有序)
对于一个x,查询的mex要不然在比之前exist中最大的元素大的最小的那一个里面,要不然就在被删除的exist元素中,逐一对比即可.

D.cpp
#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#define int long long

#define PRINT_SET(x) \
cout<<"SET_VALUE:"<<endl;\
for(set<int>::iterator i=x.begin();i!=x.end();++i) \
cout<<*i<<' ';\
cout<<endl<<"END"<<endl;

using namespace std;
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
inline void write(int a)
{
    if (a < 0)
    {
        putchar('-');
        putchar('1');
    }
    else
    {
        if (a >= 10)
            write(a / 10);
        putchar(a % 10 + '0');
    }
}
const int maxn = 100005, mod = 998244353;
int q;
set<int> S, del;
map<int, set<int>> exist;
signed main()
{
    /* freopen("D.out","w",stdout); */
    S.insert(0);
    q = read();
    while (q--)
    {
        /* cout<<"S"<<endl;
        PRINT_SET(S);
        cout<<"del"<<endl;
        PRINT_SET(del); */
        char ch;
        cin >> ch;
        int x = read();
        if (ch == '+')
        {
            S.insert(x);
            if (del.find(x) != del.end())
            {
                del.erase(x);
            }
        }
        else if (ch == '-')
        {
            del.insert(x);
            S.erase(x);
        }
        else
        {
            int res = 0, flag = 0;
            set<int> &ex = exist[x];
            set<int>::iterator ex_i = ex.begin(), exend = ex.end();
            set<int>::iterator del_i = del.begin(), delend = del.end();
            while (ex_i != exend && del_i != delend)
            {
                ex_i = ex.lower_bound(*del_i);
                if (ex_i != exend && *ex_i == *del_i)
                {
                    res = *ex_i;
                    /* ex.erase(res); */
                    flag = 1;
                    break;
                }
                del_i = del.lower_bound(*ex_i);
            }
            if (flag)
            {
                write(res), puts("");
                continue;
            }
            int maxx=0;
            if(!ex.empty())
                maxx=*ex.rbegin()+x;
            
            set<int>::iterator Send = S.end();
            for(int i=maxx;;i+=x)
            {
                if(S.find(i)==Send)
                {
                    res=i;
                    break;
                }
                else
                {
                    ex.insert(i);
                }
            }

            write(res), puts("");
        }
    }
    return 0;
}
/*
3
+ 1
- 1
? 2
*/
/*
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
? 2
- 1
- 3
- 6 
+ 6
? 2
- 6
? 2
*/

标签:Balance,int,题解,CF1732D2,ch,del,ex,res,include
From: https://www.cnblogs.com/520Enterprise/p/16824316.html

相关文章

  • LeetCode2447 最大公因数等于 K 的子数组数目 题解
    看到这题,发现可以直接枚举字串进行check,复杂度是\(\mathcalO(n^2(n+\logw))\),但是可以固定左端点,向右推右端点统计答案优化到\(\mathcalO(n(n+\logw))\)。虽然这里......
  • P6533 RELATIVNOST 题解
    P6533RELATIVNOST题解目录P6533RELATIVNOST题解题目分析思路代码题目洛谷P6533RELATIVNOST分析题目中要求至少有\(c\)人买走至少一张彩色画,那暴力的思路就是......
  • 【题解】ABC259Ex Yet Another Path Counting
    Sol考虑两种暴力。直接枚举同类点,组合数计算两点之间的路径数。单次操作时间复杂度\(O(B^2)\)。其中\(B\)表示这类点的个数。暴力dp,记\(dp_{i,j}\)表示到\((i,j......
  • Codeforces Round #829-1754A+B与1753A+B+C 题解
    1754A-TechnicalSupport题意给定一个只包含大写字母\(\texttt{Q}\)和\(\texttt{A}\)的字符串,如果字符串里的每一个\(\texttt{Q}\)都能与在其之后的\(\texttt{A......
  • 【题解】ABC259F Select Edges
    Sol考虑dp。记\(dp_{u,0/1}\)表示\(u\)点是否向上连边的最大值。转移的话相当于是找若干个\(dp_{v,1}+w(u,v)\)进行转移。其中\(w(u,v)\)表示\((u,v)\)这条......
  • 2022级HAUT新生周赛题解汇总
    2022级HAUT新生周赛(零)题解@:2022级HAUT新生周赛(一)题解@卞子骏:2022级HAUT新生周赛(二)题解@武其轩:2022级HAUT新生周赛(三)题解@焦小航:2022级HAUT新生周赛(四)题解@张子豪:202......
  • leetcode刷题MySQL题解十八
    leetcode刷题MySQL题解十八题目叙述Views表:±--------------±--------+|ColumnName|Type|±--------------±--------+|article_id|int||author_id|int|......
  • leetcode刷题MySQL题解十五
    leetcode刷题MySQL题解十五题目叙述Employee表:±------------±-----+|ColumnName|Type|±------------±-----+|id|int||salary|int|±------------±......
  • leetcode刷题MySQL题解十三
    leetcode刷题MySQL题解十三题目叙述表:Products±------------±--------+|ColumnName|Type|±------------±--------+|product_id|int||store1|int||st......
  • leetcode刷题MySQL题解十四
    leetcode刷题MySQL题解十四题目叙述给定一个表tree,id是树节点的编号,p_id是它父节点的id。±—±-----+|id|p_id|±—±-----+|1|null||2|1||3|1......