这个题目有一点表述不清,出现过的指的是现有的牌河内存在的。
对于加入牌河的num 其会使得 num+3 和 num-3安全,只要这两个值合法就可以加入set(用来去重),
对于移出牌河的num如果这次操作使得它的出现次数归零显然会影响num+3 和 num-3 这就需要我们去检查
num-6 num-3 num 和 num num+3 num+6 即num-6和num+6是否为0,如果为0那么num-3和num+3就不在安全,从seet移出
每次操作后输出set的大小就是答案了
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int m,q;
cin >> m >> q;
map<int,int> mp;
set<int> res;
int ans = 0;
for(int i = 0; i < q; i++)
{
int op,num;
cin >> op >> num;
if(op==1)
{
mp[num]++;
if(num-3>=1) res.insert(num-3);
if(num+3<=m) res.insert(num+3);
}
if(op==2)
{
mp[num]--;
if(mp[num]==0)
{
if(res.find(num+3)!=res.end())
{
if(mp.find(num+6)==mp.end() || mp[num+6]==0)
{
res.erase(num+3);
}
}
if(res.find(num-3)!=res.end())
{
if(mp.find(num-6)==mp.end() || mp[num-6]==0)
{
res.erase(num-3);
}
}
}
}
cout << res.size() << endl;;
}
}

京公网安备 11010502036488号