Codeforces1176B(B题)Merge it!

B. Merge it!

You are given an array aanna1,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][3,4][3,4][1,6][1,6][2,5][2,5]

Your task is to find the maximum possible number of elements divisible by 33

You have to answer tt

Input

The first line contains one integer tt1t10001≤t≤1000

The first line of each query contains one integer nn1n1001≤n≤100

The second line of each query contains nna1,a2,,ana1,a2,…,an1ai1091≤ai≤109

Output

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

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int main() {
 5     int n,m;
 6     cin>>n;
 7     for(int i=0; i<n; i++) {
 8         int cnt=0,cnt1=0,a1=0,b1=0;
 9         int a[105];
10         int b[105];
11         cin>>m;
12         for(int j=0; j<m; j++) {
13             cin>>a[j];
14             if(a[j]%3==0) {
15                 cnt++;
16             } else {
17                 a[j]%=3;
18                 b[cnt1++]=a[j];
19             }
20         }
21         for(int i=0; i<cnt1; i++) {
22             if(b[i]==1) {
23                 a1++;
24             }
25             if(b[i]==2) {
26                 b1++;
27             }
28         }
29         if(a1>b1) {
30             int h=a1-b1;
31             if(h>=3) {
32                 cnt+=h/3;
33             }
34             cnt+=b1;
35         } else {
36             int h=b1-a1;
37             if(h>=3) {
38                 cnt+=h/3;
39             }
40             cnt+=a1;
41         }
42         cout<<cnt<<endl;
43     }
44 }

思路分析:若本身可整除3就加1,将不可整除3的数模3,都转化成1和2,比较1和2的数量,1比2多那就配对2数量个3,然后看1比2多了几个,如果超过3,看有多少个3,再记数。

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

转载于:https://www.cnblogs.com/yuanhang110/p/11267914.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值