B. Merge it!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers a1,a2,…,ana1,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][2,1,4] you can obtain the following arrays: [3,4][3,4], [1,6][1,6] and [2,5][2,5].
Your task is to find the maximum possible number of elements divisible by 33 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.
You have to answer tt independent queries.
Input
The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of queries.
The first line of each query contains one integer nn (1≤n≤1001≤n≤100).
The second line of each query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Output
For each query print one integer in a single line — the maximum possible number of elements divisible by 33 that are in the array after performing described operation an arbitrary (possibly, zero) number of times.
Example
input
Copy
2 5 3 1 2 3 1 7 1 1 1 1 1 2 2
output
Copy
3 3
Note
In the first query of the example you can apply the following sequence of operations to obtain 33 elements divisible by 33: [3,1,2,3,1]→[3,3,3,1][3,1,2,3,1]→[3,3,3,1].
In the second query you can obtain 33 elements divisible by 33 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][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 0 统计0的个数。
然后优先让1 和2结合,取1 2数量的最小值x加上,1 2数量减去x,分别除以3即可,因为至少三张牌才能组成3的倍数。
这个%3好像和某场区域赛的思想很接近。
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 2000+6;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
ll a[maxn];
int main()
{
int q;
scanf("%d",&q);
while(q--)
{
int x;
scanf("%d",&x);
ll cnt=0;
ll a1=0;
ll a2=0;
rep(i,1,x)
{
scanf("%lld",&a[i]);
a[i]%=3;
if(!a[i])cnt++;
if(a[i]==1)
{
a1++;
}else if(a[i]==2)
{
a2++;
}
}
ll le=min(a1,a2);
a1-=le;
a2-=le;
printf("%lld\n",a1/3+a2/3+le+cnt);
}
}