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

链接:

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