codeforces1051C—— Vasya and Multisets【思維,模擬】
C. Vasya and Multisets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya has a multiset ss consisting of nn integer numbers. Vasya calls some number xx nice if it appears in the multiset exactly once. For example, multiset {1,1,2,3,3,3,4}{1,1,2,3,3,3,4} contains nice numbers 22 and 44.
Vasya wants to split multiset ss into two multisets aa and bb (one of which may be empty) in such a way that the quantity of nice numbers in multiset aa would be the same as the quantity of nice numbers in multiset bb (the quantity of numbers to appear exactly once in multiset aaand the quantity of numbers to appear exactly once in multiset bb).
Input
The first line contains a single integer n (2≤n≤100)n (2≤n≤100).
The second line contains nn integers s1,s2,…sn (1≤si≤100)s1,s2,…sn (1≤si≤100) — the multiset ss.
Output
If there exists no split of ss to satisfy the given requirements, then print "NO" in the first line.
Otherwise print "YES" in the first line.
The second line should contain a string, consisting of nn characters. ii-th character should be equal to 'A' if the ii-th element of multiset ssgoes to multiset aa and 'B' if if the ii-th element of multiset ss goes to multiset bb. Elements are numbered from 11 to nn in the order they are given in the input.
If there exist multiple solutions, then print any of them.
Examples
input
Copy
4 3 5 7 1
output
Copy
YES BABA
input
Copy
3 3 5 1
output
Copy
NO
我們定義一個數在一個幾何裡面只出現一次我們把他叫做“好數”,給定一組數,我們把他們分為A,B兩組,保證兩組的好數的數量相同。
題目大致思路:我們先把出現兩次的數字都放到統一個集合中,相當於他們沒有出現過,然後在考慮出現一次的數字,如果出現一次的數字是偶數個則能分,如果是奇數則不能分。然後在考慮出現次數>=3的數字,我們他們一次交替放在一個集合中。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int a[MAXN];
char ans[MAXN];
map<int, int> cnt;
int main(){
int n;
while(~scanf("%d",&n)){
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
int sum1 = 0,sum3 = 0;
for(int i = 1; i <= 100; i++){
if(cnt[i] == 1) sum1++;
else if(cnt[i] >= 3) sum3++;
}
for(int i = 1; i <= n; i++){
if(cnt[a[i]] == 2) ans[i] = 'A';
}
if(sum1 % 2 == 0){
puts("YES");
int flag = 0;
for(int i = 1; i <= n; i++){
if(cnt[a[i]] == 1){
ans[i] = 'A' + flag;
flag ^= 1;
}else ans[i] = 'A';
}
for(int i = 1; i <= n; i++) printf("%c",ans[i]);
printf("\n");
continue;
}
if(sum3 == 0) { puts("NO"); continue; }
puts("YES");
int flag = 0;
for(int i = 1; i <= n; i++){
if(cnt[a[i]] >= 3){
ans[i] = 'B' - flag;
flag = 1;
}
}
flag = 0;
for(int i = 1; i <= n; i++){
if(cnt[a[i]] == 1){
ans[i] = 'A' + flag;
flag ^= 1;
}
}
for(int i = 1; i <= n; i++) printf("%c",ans[i]);
printf("\n");
}
return 0;
}