题意:给出一些对集合的操作,要求找出mex-k,即不存在于集合当中最小的k的倍数。

+ x 向集合中添加数x

?x 询问mex-x

- x 删去集合中的x(保证x一定不存在于集合中)(该操作只在hard版当中出现)

q<=2e5 x<=1e18

思路:在eazy版当中,我们很容易的就能写出一个暴力代码

for (int i = 1; i <= n; i++){char op; ll x;cin >> op >> x;if (op == '+') {now.insert(x);}else {for (ll i = x;; i += x){if (!now.count(i)){cout << i << endl;break;}}}}

对于这样的代码来说,时间复杂度处于O(q^2)的级别,主要原因在于,我可以一直查询一个较小的值,并且得出一个较大的值,具体的来说,就是没有记录下答案。

于是我们考虑,每次查询更新一个map<ll,ll> ans,以记录答案,但是需要注意的是,每次记录的答案并不等同下次查询时的答案,由于eazy版当中没有减数这一种操作。每次的答案只会比上一次大。因此准确的来说我们只是记录了ans的初始值

但是够了,这样的时间复杂度也只是与q相关处在q*sqrt(q)的级别上

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 500010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n, m, k;
map<ll, int> mapp;
int cnt;
ll ans[N];ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {ll res = 1;while (b) {if (b & 1) res = res * a % MOD;a = a * a % MOD;b >>= 1;}return res;
}inline void init() {}ll get(ll x)
{if (!mapp.count(x))return mapp[x] = ++cnt;else return mapp[x];
}void slove()
{cin >> n;set<ll> s;map<ll, set<ll>> del, factor;for (int i = 1; i <= n; i++){char op; ll x;cin >> op >> x;if (op == '+') {s.insert(x);}else if (op == '-') {s.erase(x);for (auto u : factor[x])del[u].insert(x);factor[x].clear();}else {ll ans;if (!del[x].empty())ans = *del[x].begin();else ans = x;while (s.count(ans)) {if (del[x].count(ans)) del[x].erase(ans);factor[ans].insert(x);if (!del[x].empty()) ans = *del[x].begin();else ans += x;}cout << ans << '\n';del[x].insert(ans);}}return;
}signed main()
{//IOS;int _ = 1;//si(_);init();while (_--){slove();}return 0;
}
/*
1
7 7
0 1 0 1 0 1 0
1 7
3 6
2 5
1 4
4 7
2 6
2 7*/

HARD版

对于hard版来说,上面的方法也就不适用了,因为删除会减小某些值的结果值。

维护两个数据结构,使用map映射,维护每个数会影响的较小的数,以及每个数的可能ans

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 500010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n, m, k;
map<ll, int> mapp;
int cnt;
ll ans[N];ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {ll res = 1;while (b) {if (b & 1) res = res * a % MOD;a = a * a % MOD;b >>= 1;}return res;
}inline void init() {}ll get(ll x)
{if (!mapp.count(x))return mapp[x] = ++cnt;else return mapp[x];
}void slove()
{cin >> n;set<ll> s;map<ll, set<ll>> del, factor;for (int i = 1; i <= n; i++){char op; ll x;cin >> op >> x;if (op == '+') {s.insert(x);}else if (op == '-') {s.erase(x);for (auto u : factor[x])del[u].insert(x);factor[x].clear();}else {ll ans;if (!del[x].empty())ans = *del[x].begin();else ans = x;while (s.count(ans)) {if (del[x].count(ans)) del[x].erase(ans);factor[ans].insert(x);if (!del[x].empty()) ans = *del[x].begin();else ans += x;}cout << ans << '\n';del[x].insert(ans);}}return;
}signed main()
{//IOS;int _ = 1;//si(_);init();while (_--){slove();}return 0;
}
/*
1
7 7
0 1 0 1 0 1 0
1 7
3 6
2 5
1 4
4 7
2 6
2 7*/

D2. Balance相关推荐

  1. HDU 5616 Jam's balance(01背包)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...

  2. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

  3. D2 日报 2019年6月11日

    ? 开源项目 ➡️ sfyc23/EverydayWechat watch 34 star 1690 fork 317 每日自动给女朋友发微信暖心话. github.com ➡️ YMFE/yapi ...

  4. SAP QM 采样方案的c1 d1 c2 d2 --多重采样

    SAP QM 采样方案的c1 d1 c2 d2 --多重采样 使用QDP1创建采样方案的时候为什么只要填写c1 d1不用填写c2 d2等等呢? 首先,C1/C2-C7代表接受数 D1/D2-D7代表拒 ...

  5. SAP 金属行业实施项目的GAP之Metall Balance

    去年7月初笔者加入D项目之初,就听闻该项目有一个复杂的需求或者说GAP,即是金属平衡. 客户所在的行业是金属加工制造行业,为汽车生产某个关键零部件.客户提出,整个生产过程,没有化学反应产生,理论上生产 ...

  6. D2 日报 2019年1月2日

    官网阅读获得更好的体验,传送门<日报 2019年1月2日> 你有一个苹果,我有一个苹果,交换之后我们还是各自有一个苹果. 你有一份知识,我有一份知识,我们互相分享一下就都有了两份知识. 开 ...

  7. 从d2来看前端的未来

    前两天跟组内的小伙伴们参加了d2前端分享会议,主题大概就是工程化.前后端分离以及nodejs的应用环境,工程化的前端时代已经来临,各家公司的手段都差不多,都是为了提高开发效率,使多人协同开发更好维护以 ...

  8. Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1

    A. Water Buying 链接:http://codeforces.com/contest/1118/problem/A 实现代码: #include<bits/stdc++.h> ...

  9. D2 第11届前端技术论坛记录

    从上海跑到杭州去参加d2技术论坛收获其实还是很大的:我选择的是一号厅,很遗憾晚上没能参加夜场. 腾讯的黄荣奎的<微信小程序解决方案>结论: 1.微信小程序是很封闭的(在微信小程序中没有wi ...

最新文章

  1. 5如何将表格的一行数据清空_微信公众号推文中如何自定义添加表格?
  2. CSS 特效 (教程还是英文的好)
  3. 利用K8S技术栈打造个人私有云(连载之:K8S资源控制)
  4. 【儿童成长心理学】第一章 引言
  5. STM32 - 定时器的设定 - 基础- 0D - Timer synchronization chaining - 主从模式下 - 定时器同步和级联控制 - 级联启动定时器
  6. 广度优先搜索(BFS)——抓住那头牛(POJ 4001)
  7. 年度调薪一般涨多少_宁愿辞职也不敢提加薪,心理专家告诉你加薪提多少才合适...
  8. 为什么JavaScript仅在IE中打开开发人员工具一次后才能工作?
  9. DB2sql关键字——ALTER TABLE ....ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE
  10. java mp3合并_java合并MP3文件
  11. Camera 驱动 读取摄像头ID失败问题、低温下Camera打开花屏或者读不到id、概率性读取不到id
  12. Mac系统中移动硬盘热拔(强制退出)后,在插上不显示移动硬盘的问题和解决办法
  13. 简单认识一下苹果笔记本以及使用MBP开发一年的心得体会
  14. 计算机动态图显示原理,30张传感器工作原理动态图
  15. 图像识别-纯数字识别
  16. 静态网页如何上传附件
  17. JavaSE06-集合
  18. 配置git send-mail发邮件 (126邮箱适用)
  19. ceph存储 smartctl用法小结
  20. python生成图文并茂的pdf--财务报表(五)如何生成并列的图表

热门文章

  1. ArcMap常用1:地理配准(一张地图图片和gis地图的匹配)
  2. underscore.js 报_is not defined解决方法
  3. 【无标题】UBUNTU22.04 WIFI图标显示
  4. vs2013 编译 该文件已在源编辑器之外被修改
  5. MYSQL数据库- 修改数据表名称、列名称
  6. 机器学习之感知机算法
  7. 『NLP学习笔记』Cross Entropy Loss 的硬截断、软化到 Focal Loss
  8. 就一个翻译功能,百度你都跟不上谷歌,无力吐槽
  9. Vue3定义全局变量/方法
  10. rpc--sekiro框架