标签
- 数学
题目地址
C - 4-adjacent
- https://atcoder.jp/contests/abc069/tasks/arc080_a?lang=en
问题描述
Problem Statement
We have a sequence of length N, a = (a_1, a_2, …, a_N). Each a i _i i is a positive integer.
Snuke’s objective is to permute the element in a so that the following condition is satisfied:
- For each 1 ≤ i ≤ N - 1 the product of a i _i i and a i + 1 _{i + 1} i+1 is a multiple of 4.
Determine whether Snuke can achieve his objective.
Constraints
- 2 ≤ N ≤ 10 5 ^5 5
- a i _i i is an integer.
- 1 ≤ a i _i i ≤ 10 9 ^9 9
Input
Input is given from Standard Input in the following format:
N
a1 a2 ... aN
Output
If Snuke can achieve his objective, print Yes
; otherwise, print No
.
Sample Input 1
3
1 10 100
Sample Output 1
Yes
One solution is (1, 100, 10).
Sample Input 2
4
1 2 3 4
Sample Output 2
No
It is impossible to permute a so that the condition is satisfied.
Sample Input 3
3
1 4 1
Sample Output 3
Yes
The condition is already satisfied initially.
Sample Input 4
2
1 1
Sample Output 4
No
Sample Input 5
6
2 7 1 8 2 8
Sample Output 5
Yes
题意
给定一个长度为n的整数列,如果有可以在重新进行排列后保证相邻两数的积均为4的倍数的排列方法,输出Yes,反之输出No
思路
任意一个数乘上4的倍数依旧是4的倍数(废话…),而4的倍数就是a类数
奇数就是b类数
只要保证每个奇数都可以与4的倍数配对,则一定可以达成条件
反之则不能
题解
小码匠
void coder_solution() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> vi(n);
int a = 0, b = 0;
for(int i = 0; i < n; i++) {
cin >> vi[i];
if (vi[i] % 4 == 0) {
a++;
} else if ( vi[i] % 2 == 1) {
b++;
}
}
if(a + 1 > b) {
cout << "Yes";
} else {
if(a + 1 == b) {
if (a + b == n) {
cout << "Yes";
} else {
cout << "No";
}
} else {
cout << "No";
}
}
}
官方题解
#define yes "Yes"
#define no "No"
int N, A[201010];
string solve() {
int c2 = 0;
int c4 = 0;
int c = 0;
for (int i = 0; i < N; i==) {
if (A[i] % 4 == 0) c4++;
else if (A[i] % 2 == 0) c2++;
else c++;
}
if (c4 + 1 == c && N == (c4 + c)) return yes;
if (c4 < c) return no;
return yes;
}
void _main() {
cin >> N;
for (int i = 0; i < N; i++ ) cin >> A[i];
cout << solve() << endl;
}