Codeforces Round #565 (Div. 3) B

B. Merge it!

题目链接:http://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.
Input
The first line contains one integer t (1≤t≤1000) — the number of queries.

The first line of each query contains one integer n
(1≤n≤100).

The second line of each query contains n
integers a1,a2,…,an (1≤ai≤109).
Output

For each query print one integer in a single line — the maximum possible number of elements divisible by 3

that are in the array after performing described operation an arbitrary (possibly, zero) number of times.
Example

Input
2
5
3 1 2 3 1
7
1 1 1 1 1 2 2

Output
3
3

Note

In the first query of the example you can apply the following sequence of operations to obtain 3
elements divisible by 3: [3,1,2,3,1]→[3,3,3,1]

In the second query you can obtain 3
elements divisible by 3 with the following sequence of operations: [1,1,1,1,1,2,2]→[1,1,1,1,2,3]→[1,1,1,3,3]→[2,1,3,3]→[3,3,3].

题意

给你一个数组,可以相加任意两个数,让你输出相加任意次数后该数组里的数是3的倍数的个数

思路

3=1+2,所以记录该数组里%3为1的个数了,和%3为2的个数了,之后就可以算结果了,别忘了还存在三个%3为1或2的相加也是3的倍数。

//
// Created by hjy on 19-6-5.
//
#include<bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 10;
int main()
{

    int T;
    cin>>T;
    while(T--)
    {
        int t;
        cin>>t;
        int x;
        vector<int>sh,ch;
        int sum=0;
        for(int i=0;i<t;i++)
        {
            cin>>x;
            if(x%3==0)
                sum++;
            else
                sh.push_back(x%3);
        }
        int _1=0,_2=0;

        for(int i=0;i<sh.size();i++)
        {
        if(sh[i]==1)_1++;
        else _2++;
        }
           cout<<sum+min(_1,_2)+(max(_1,_2)-min(_1,_2))/3<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Vampire6/p/11001324.html

时间: 2024-10-11 19:30:03

Codeforces Round #565 (Div. 3) B的相关文章

Codeforces Round #565 (Div. 3) C. Lose it!

链接: https://codeforces.com/contest/1176/problem/C 题意: You are given an array a consisting of n integers. Each ai is one of the six following numbers: 4,8,15,16,23,42. Your task is to remove the minimum number of elements to make this array good. An a

Codeforces Round #565 (Div. 3) A

A. Divide it! 题目链接:http://codeforces.com/contest/1176/problem/A 题目 You are given an integer n You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace n with n2 if n is divisible by 2;Rep

Codeforces Round #565 (Div. 3) E. Cover it!

题目:https://codeforces.com/contest/1176/problem/E 思路:奇偶染色 #include<bits/stdc++.h> using namespace std; #define DEBUG cout<<"DEBUG"<<endl typedef long long ll; typedef unsigned long long ull; typedef pair<int,int>pii; typed

Codeforces Round #565 (Div. 3)

传送门 A. Divide it! •题意 给定一个数n, 每次可以进行下列一种操作 1.如果n可以被2整除,用n/2代替n 2.如果n可以被3整除,用2n/3代替n 3.如果n可以被5整除,用4n/5代替n 如果可以经过上述操作使得 n 变为 1,输出最小操作次数,反之,输出-1: •思路 n/2 < 2n/3 < 4n/5 要想操作次数最少,优先操作 1 > 2 > 3: •代码 #include<bits/stdc++.h> using namespace std

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除