题解:CF1732D1 Balance (Easy version)
lailai0916 · · 题解
解题思路
考虑通过直接模拟来解决。维护一个 set,在每次插入操作 + 时,将元素插入集合中;在查询操作 * 时,从小到大暴力枚举
然而,这种方法在最坏情况下会导致时间复杂度为
考虑优化,注意到集合中的元素是单调递增的。因此,对于每个数 map 来记录每个数
优化后的最坏情况是:前半部分的操作插入
参考代码
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=200005;
set<ll> s;
map<ll,ll> m;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin>>q;
s.insert(0);
while(q--)
{
char op;
ll x;
cin>>op>>x;
if(op=='+')s.insert(x);
else if(op=='?')
{
ll ans=m[x];
while(s.find(ans)!=s.end())ans+=x;
m[x]=ans;
cout<<ans<<'\n';
}
}
return 0;
}