Codeforces Round #662 (Div. 2)

Codeforces Round #662 (Div. 2) 传送门

B - Applejack and Storages

Description

This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend’s company. Initially, the company storehouse has n planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack’s friend can provide her with information about each operation. For convenience, he will give her information according to the following format:

  • +x: the storehouse received a plank with length x
  • −x: one plank with length x was removed from the storehouse (it is guaranteed that the storehouse had some planks with length x).

Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.

Input

The first line contains a single integer n (1≤n≤105): the initial amount of planks at the company’s storehouse, the second line contains n integers a1,a2,…,an (1≤ai≤105): the lengths of the planks.
The third line contains a single integer q (1≤q≤105): the number of events in the company. Each of the next q lines contains a description of the events in a given format: the type of the event (a symbol + or −) is given first, then goes the integer x (1≤x≤105).

Output

After every event in the company, print “YES” if two storages of the required shape can be built from the planks of that company’s set, and print “NO” otherwise. You can print each letter in any case (upper or lower).

input

6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2

output

NO
YES
NO
NO
NO
YES


题目大意

给你n块木板的长度,还有两种操作方式:1.添加一块长度为x的木板  2.删除一块长度为x的木板;
问在每次操作后,是否能够满足组成一个正方形和一个矩形(正方形也是特殊的矩形)

解题思路

先记录木板每个长度的数量,然后再根据木板长度的数量记录木板相同长度的组数,最后判断操作后木板的组数是否能满足组成正方形和矩形即可


Code

#include
#include
#include
#include
using namespace std;
const int maxn = 1e6;
int len[maxn];
int plank[maxn];
int main()
{
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	int n;
	cin>>n;
	for(int i = 0  ; i < n ; i++)
	{
		int key;
		cin>>key;
		len[key]++;
		plank[len[key]]++;
	}
	int q;
	cin>>q;
	string op;int x;
	while(q--)
	{
		cin>>op;
		if( op[0] == '+' )
		{
			cin>>x;
			len[x]++;
			plank[len[x]]++;
			
		}
		if(op[0] == '-')
		{
			cin>>x;
			plank[len[x]]--;
			len[x]--;
		}
		//cout<<"plank[2] = "<
		//cout<<"plank[4] = "<
		//cout<<"plank[6] = "<
		//cout<<"plank[8] = "<

		int flag = 0;
		if(plank[2] >= 3 && plank[4] >= 1){	flag = 1;}
		if(plank[2] >= 2 && plank[6] >= 1){	flag = 1;}
		if(plank[4] >= 2){	flag = 1;}
		if(plank[8] >= 1){	flag = 1;}
		if(!flag)
		{
			cout<<"NO"<<endl;
		}else
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}
//7
//1 1 2 2 3 3 3
//6
//+ 3
//plank[2] = 3
//plank[4] = 1
//plank[6] = 0
//plank[8] = 0
//YES
//+ 3
//plank[2] = 3
//plank[4] = 1
//plank[6] = 0
//plank[8] = 0
//YES
//+ 3
//plank[2] = 3
//plank[4] = 1
//plank[6] = 1
//plank[8] = 0
//YES
//+ 3
//plank[2] = 3
//plank[4] = 1
//plank[6] = 1
//plank[8] = 0
//YES
//+ 3
//plank[2] = 3
//plank[4] = 1
//plank[6] = 1
//plank[8] = 1
//YES

你可能感兴趣的:(Codeforces,Round,算法)