当前位置: 首页 > 工具软件 > AppleJack > 使用案例 >

Codeforces 1393 B. Applejack and Storages

江奕
2023-12-01

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 푛 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:

  • 푥: the storehouse received a plank with length 푥
    − 푥: one plank with length 푥 was removed from the storehouse (it is guaranteed that the storehouse had some planks with length 푥).
    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 푛 (1≤푛≤105): the initial amount of planks at the company’s storehouse, the second line contains 푛 integers 푎1,푎2,…,푎푛 (1≤푎푖≤105): the lengths of the planks.

The third line contains a single integer 푞 (1≤푞≤105): the number of events in the company. Each of the next 푞 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 푥 (1≤푥≤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).

Example
inputCopy
6
1 1 1 2 1 1
6

  • 2
  • 1
  • 1
  • 2
  • 1
  • 2
    outputCopy
    NO
    YES
    NO
    NO
    NO
    YES
    Note
    After the second event Applejack can build a rectangular storage using planks with lengths 1, 2, 1, 2 and a square storage using planks with lengths 1, 1, 1, 1.

After the sixth event Applejack can build a rectangular storage using planks with lengths 2, 2, 2, 2 and a square storage using planks with lengths 1, 1, 1, 1.

题意:
给出一些数,操作为加减一些数的个数,求能否用剩下数当做边(每个边对应一个数),构成一个正方形和一个矩形。

思路:
维护2,4,6,8的个数即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
 
int num[maxn],a[maxn];
int Num[10];
 
int main() {
    int n;scanf("%d",&n);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
        num[a[i]]++;
    }
    for(int i = 1;i <= 100000;i++) {
        if(num[i] >= 8) Num[8]++;
        else if(num[i] >= 6) Num[6]++;
        else if(num[i] >= 4) Num[4]++;
        else if(num[i] >= 2) Num[2]++;
    }
    
    int q;scanf("%d",&q);
    while(q--) {
        char op[10];scanf("%s",op);
        int x;scanf("%d",&x);
        if(op[0] == '+') {
            if(num[x] == 1) {
                Num[2]++;
            } else if(num[x] == 3) {
                Num[2]--;
                Num[4]++;
            } else if(num[x] == 5) {
                Num[4]--;
                Num[6]++;
            } else if(num[x] == 7) {
                Num[6]--;
                Num[8]++;
            }
            num[x]++;
        } else {
            if(num[x] == 2) {
                Num[2]--;
            } else if(num[x] == 4) {
                Num[2]++;
                Num[4]--;
            } else if(num[x] == 6) {
                Num[4]++;
                Num[6]--;
            } else if(num[x] == 8) {
                Num[6]++;
                Num[8]--;
            }
            num[x]--;
        }
 
        if(Num[8] >= 1) {
            printf("YES\n");
        } else if(Num[6] >= 2) {
            printf("YES\n");
        } else if(Num[6] >= 1 && Num[4] >= 1) {
            printf("YES\n");
        } else if(Num[6] >= 1 && Num[2] >= 1) {
            printf("YES\n");
        } else if(Num[4] >= 2) {
            printf("YES\n");
        } else if(Num[4] >= 1 && Num[2] >= 2) {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答