Codeforces Round #565 (Div. 3) B. Merge it!

CodeForces B题解析
本文解析了CodeForces竞赛中一道关于数组操作的问题,旨在通过任意次数的操作找到数组中最多能有多少个元素被3整除。核心思路在于分析数组元素除以3的余数,并优先配对余数为1和2的元素。

链接:

https://codeforces.com/contest/1176/problem/B

题意:

You are given an array a consisting of n integers a1,a2,…,an.

In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array [2,1,4] you can obtain the following arrays: [3,4], [1,6] and [2,5].

Your task is to find the maximum possible number of elements divisible by 3 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.

You have to answer t independent queries.

思路:

找除三的余数,优先配对1和2.
再3个1或3个2可以配成一个。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 1e2 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;

int a[3];

int main()
{
    scanf("%d", &t);
    while (t--)
    {
        memset(a, 0, sizeof(a));
        scanf("%d", &n);
        int v;
        for (int i = 1;i <= n;i++)
        {
            scanf("%d", &v);
            a[v%3]++;
        }
        if (a[1] > a[2])
            swap(a[1], a[2]);
        int res = a[0]+a[1]+(a[2]-a[1])/3;
        cout << res << endl;
    }

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/10998706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值