欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-15 11:57:17
题目链接:传送门感想:好久不打cf,一打直接被教育,本来想的是用两个整数变量来模拟,但是似乎删除数字时不好处理,然后就凉了。思路:用set来存储所有出现次数大于4的数,变量f2存储所有大于等于2小于4的数(即2和3),然后随着每次操作维护即可。这样似乎比较笨,但是比较好想。代码:#include using namespace std;const int maxn = 1e5 + 5;int a[maxn];map

题目链接:传送门

感想:好久不打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;
}

本文地址:https://blog.csdn.net/qq_39475280/article/details/107876220

相关标签: 模拟 codeforces