题目链接
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:
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.
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≤1e5): the lengths of the planks.
The third line contains a single integer q (1≤q≤1e5): 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≤1e5).
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).
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
NO
YES
NO
NO
NO
YES
这题我考虑的好像有点复杂了,记录了四个变量,分别是:
每次加减都变化一下即可,设多的坏处就是输出时要考虑很多,因为这个WA了两发/(ㄒoㄒ)/~~,AC代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char c;
int n,q,x,cnt2=0,cnt4=0,cnt6=0,cnt8=0;
cin>>n;
map<int,int>m;
while(n--){
cin>>x,m[x]++;
}
cin>>q;
for(auto i:m){
if(i.second>=8) cnt8++;
else if(i.second>=6) cnt6++;
else if(i.second>=4) cnt4++;
else if(i.second>=2) cnt2++;
}
while(q--){
cin>>c>>x;
if(c=='-'){
if(m[x]==8) cnt8--,cnt6++;
else if(m[x]==6) cnt6--,cnt4++;
else if(m[x]==4) cnt4--,cnt2++;
else if(m[x]==2) cnt2--;
m[x]--;
}else{
if(m[x]==7) cnt8++,cnt6--;
else if(m[x]==5) cnt6++,cnt4--;
else if(m[x]==3) cnt4++,cnt2--;
else if(m[x]==1) cnt2++;
m[x]++;
}
if(((cnt2>1||cnt6||cnt8)&&cnt4>0)||(cnt6>0&&cnt2>0)||(cnt4>1)||(cnt8>0)||(cnt6>1)) printf("YES\n");
else printf("NO\n");
}
return 0;
}