首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >AtCode ABC069 C-4-adjacent

AtCode ABC069 C-4-adjacent

作者头像
小码匠
发布2022-06-16 17:53:59
发布2022-06-16 17:53:59
3940
举报

AtCode ABC069 C-4-adjacent

标签

  • 数学

题目地址

C - 4-adjacent

  • https://atcoder.jp/contests/abc069/tasks/arc080_a?lang=en

问题描述

Input

Input is given from Standard Input in the following format:

代码语言:javascript
复制
N
a1 a2 ... aN

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

代码语言:javascript
复制
3
1 10 100

Sample Output 1

代码语言:javascript
复制
Yes

One solution is (1, 100, 10).

Sample Input 2

代码语言:javascript
复制
4
1 2 3 4

Sample Output 2

代码语言:javascript
复制
No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

代码语言:javascript
复制
3
1 4 1

Sample Output 3

代码语言:javascript
复制
Yes

The condition is already satisfied initially.

Sample Input 4

代码语言:javascript
复制
2
1 1

Sample Output 4

代码语言:javascript
复制
No

Sample Input 5

代码语言:javascript
复制
6
2 7 1 8 2 8

Sample Output 5

代码语言:javascript
复制
Yes

题意

给定一个长度为n的整数列,如果有可以在重新进行排列后保证相邻两数的积均为4的倍数的排列方法,输出Yes,反之输出No

思路

任意一个数乘上4的倍数依旧是4的倍数(废话......),而4的倍数就是a类数

奇数就是b类数

只要保证每个奇数都可以与4的倍数配对,则一定可以达成条件

反之则不能

题解

小码匠

代码语言:javascript
复制
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";
        }
    }
}

官方题解

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • AtCode ABC069 C-4-adjacent
  • 标签
  • 题目地址
  • 问题描述
    • Input
    • Output
    • Sample Input 1
    • Sample Output 1
    • Sample Input 2
    • Sample Output 2
    • Sample Input 3
    • Sample Output 3
    • Sample Input 4
    • Sample Output 4
    • Sample Input 5
    • Sample Output 5
  • 题意
  • 思路
  • 题解
    • 小码匠
    • 官方题解
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档