AtCode ABC069 C-4-adjacent

标签

  • 数学

题目地址

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码匠和老码农

喜欢作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值