Codeforces Round #662 (Div. 2) B. Applejack and Storages

题目链接:传送门

感想:好久不打cf,一打直接被教育,本来想的是用两个整数变量来模拟,但是似乎删除数字时不好处理,然后就凉了。
思路:用set来存储所有出现次数大于4的数,变量f2存储所有大于等于2小于4的数(即2和3),然后随着每次操作维护即可。这样似乎比较笨,但是比较好想。

代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;

int a[maxn];
map <int , int> mp;
vector <int> v;
int l1; 
set <int> st;


int main() {
	int n;
	ios::sync_with_stdio(0);
	cin >> n;
	l1 = 0;
	for(int i = 0 ; i < n ; i++) {
		cin >> a[i];
		if(!mp.count(a[i])) {
			v.push_back(a[i]);
		}
		mp[a[i]]++;
	}	
	int f1 = 0 , f2 = 0;
	for(int i = 0 ; i < v.size() ; i++) {
		int t = mp[v[i]];
		if(t >= 4) {
			st.insert(v[i]);
		}
		else if(t >= 2) {
			f2 += 1;
		}
	}
	int q;
	cin >> q;
	while(q--) {
		char c;
		int x;
		cin >> c >> x;
		int t;
		if(c == '+') {
			mp[x]++;
			t = mp[x];
			if(t == 2) {
				f2++;
			}
			else if(t == 4) {
				f2--;
				st.insert(x);
			}
		}
		else if(c == '-') {
			mp[x]--;
			t = mp[x];
			if(t == 1) {
				f2--;
			}
			else if(t == 3){
				f2++;
				st.erase(x);
			}
		}
		auto it = st.begin();
		if(st.size() >= 2 || (mp[*(it)] >= 8) || (mp[*(it)] >= 6 && f2 >= 1) || (st.size() >= 1 && f2 >= 2) ) {
			cout << "YES\n";
		}
		else {
			cout << "NO\n";
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值