D1. Balance (Easy version)
D1. Balance (Easy version)
This is the easy version of the problem. The only difference is that in this version there are no "remove" queries.
Initially you have a set containing one element — . You need to handle queries of the following types:
+ — add the integer to the set. It is guaranteed that this integer is not contained in the set;
? — find the of the set.
In our problem, we define the of a set of integers as the smallest non-negative integer that is divisible by and which is not contained in the set.
Input
The first line contains an integer — the number of queries.
The following lines describe the queries.
An addition query of integer is given in the format + . It is guaranteed that was not contained in the set.
A search query of is given in the format ? .
It is guaranteed that there will be at least one query of type ? .
Output
For each query of type ? output a single integer — the of the set.
Examples
input
15 + 1 + 2 ? 1 + 4 ? 2 + 6 ? 3 + 7 + 8 ? 1 ? 2 + 5 ? 1 + 1000000000000000000 ? 1000000000000000000
output
3 6 3 3 10 3 2000000000000000000
input
6 + 100 ? 100 + 200 ? 100 + 50 ? 50
output
200 300 150
Note
In the first example:
After the first and second queries, the set will contain elements . The smallest non-negative number that is divisible by and is not contained in the set is .
After the fourth query, the set will contain the elements . The smallest non-negative number that is divisible by and is not contained in the set is .
In the second example:
- Initially, the set contains only the element .
- After adding an integer the set contains elements .
- of the set is .
- After adding an integer the set contains elements .
- of the set is .
- After adding an integer the set contains elements .
- of the set is .
解题思路
先想一个最暴力的思路,就是我们用一个哈希表维护集合中存在的数,对于每个询问,我们依次枚举的倍数看看是否出现在哈希表中,直到枚举到某个的倍数在哈希表中不存在。
但这种做法必定会超时,如果依次插入的倍数,然后每次询问都是查询,那么计算量就是级别的。因此可以开个哈希表来记录对于某个上一次枚举到哪个数,如果后面再次询问,那么直接从上次枚举的到的数开始往后枚举的倍数就可以了。这是因为对于而言,当插入的数越多,值只会变大而不会变小。
这种做法的时间复杂度是多少呢?考虑最糟糕的情况,就是依次插入,然后依次询问,那么计算量大概就是。
这题卡std::unordered_map和std::unordered_set,因此要么人为手动初始化大小,要么改用std::map和std::set。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 const int N = 1e6 + 10; 7 8 void solve() { 9 int n; 10 scanf("%d", &n); 11 unordered_set<LL> st(N); 12 unordered_map<LL, LL> mp(N); 13 while (n--) { 14 char op[2]; 15 LL x; 16 scanf("%s %lld", op, &x); 17 if (op[0] == '+') { 18 st.insert(x); 19 } 20 else { 21 if (!mp.count(x)) mp[x] = x; 22 while (st.count(mp[x])) { 23 mp[x] += x; 24 } 25 printf("%lld\n", mp[x]); 26 } 27 } 28 } 29 30 int main() { 31 int t = 1; 32 // scanf("%d", &t); 33 while (t--) { 34 solve(); 35 } 36 37 return 0; 38 }
参考资料
Codeforces Round #830 (Div. 2) D1/D2(mex问题):https://zhuanlan.zhihu.com/p/576620849
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16821998.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效