4-adjacent(AtCoder-2686)

Problem Description

We have a sequence of length N, a=(a1,a2,…,aN). Each ai 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 ai and ai+1 is a multiple of 4.
Determine whether Snuke can achieve his objective.

Constraints

  • 2≤N≤105
  • ai is an integer.
  • 1≤ai≤109

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.

Example

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 的倍数,乘以右边也是 4 的倍数,若能成功输出 Yes,若不能输出 No

思路:

由于每个数乘以相邻的数是 4 的倍数,那么说明其中一个数是 4 的倍数,或者两个数都是 2 的倍数

因此,直接统计这 n 个数中为 2、4 的倍数的数的个数,考虑每个位置插 2 插 4 的关系即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 200000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int main() {
    int n;
    scanf("%d",&n);

    int num4=0,num2=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]%4==0)
            num4++;
        else if(a[i]%2==0)
            num2++;
    }
    if(n>2*num4+num2+1)
       printf("No\n");
    else if(2*num4+num2+1==n&&num2!=0)
        printf("No\n");
    else
        printf("Yes\n");

    return 0;
}

 

展开阅读全文