题目描述:
C - 4-adjacent
Problem Statement
We have a sequence of length NN, a=(a1,a2,...,aN)a=(a1,a2,...,aN). Each aiai is a positive integer.
Snuke's objective is to permute the element in aa so that the following condition is satisfied:
- For each 1≤i≤N−1, the product of aiai and ai+1ai+1 is a multiple of 4.
Determine whether Snuke can achieve his objective.
Constraints
- 2≤N≤105
- aiai is an integer.
- 1≤ai≤109
Input
Input is given from Standard Input in the following format:
NN a1a1 a2a2 ...... aNaNOutput
If Snuke can achieve his objective, print
Yes
; otherwise, printNo
.Sample Input 1 Copy
Copy
3 1 10 100Sample Output 1 Copy
Copy
YesOne solution is (1,100,10)(1,100,10).
Sample Input 2 Copy
Copy
4 1 2 3 4Sample Output 2 Copy
Copy
NoIt is impossible to permute aa so that the condition is satisfied.
Sample Input 3 Copy
Copy
3 1 4 1Sample Output 3 Copy
Copy
YesThe condition is already satisfied initially.
Sample Input 4 Copy
Copy
2 1 1Sample Output 4 Copy
Copy
NoSample Input 5 Copy
Copy
6 2 7 1 8 2 8
题意:
给出N个数,问能不能产生一种排列,使得相邻两数的乘积均为4的倍数。
思路:
显然,可以考虑每个数因子2的个数,然后想了好一会,发现一般情况下只要因子2个数为0的数数目<=因子个数>=2的数目即可,同时发现三个数例如:1 4 1的时候要特殊考虑,第一次WA的bug:没有将该种情况推广出去(即不存在因子2个数==1的数时),片面的认为只有这一种特殊情况,所以WA。
代码实现:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#include<algorithm>
#define LL long long
#define INF 1ll<<60
using namespace std;
const int N=2e5+100;
const int M=4e5+100;
const int mod=1e9+7;
LL cal(LL x){
LL num=0;
while(x%2==0){
num++;
x/=2;
}
return num;
}
int main(){
LL n;
while(cin >> n){
LL x,num1=0,num2=0,num3=0;
for(int i=1;i<=n;i++){
cin>>x;
int num=cal(x);
if(num==0){
num1++;
}else if(num==1)num2++;
else num3++;
}
int ans=0;
if(num2==0&&n!=2){
if(num3>=num1-1)ans=1;
else ans=0;
}else{
if(num3>=num1)ans=1;
else ans=0;
}
if(ans)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
THE END;