Codeforces 1176B - Merge it!

题目链接:http://codeforces.com/problemset/problem/1176/B


题意:给定序列,任意俩个元素可以相加成一个元素,求序列元素能被3整除的最大数量。

思路: 对于所有元素进行 模3 的预处理,然后 贪心 余数1 和 余数2 的配对,剩下的 3个 一组配对。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int t;
 6     int a[105];
 7     cin >> t;
 8     while(t--)
 9     {
10         int n;
11         cin >> n;
12         int ans = 0;
13         int a1 = 0,a2 = 0;
14         for(int i = 0;i < n;i++)
15         {
16             cin >> a[i];
17             a[i] %= 3;
18             if(a[i] == 0) ans++;
19             else if(a[i] == 1) a1++;
20             else if(a[i] == 2) a2++;
21         }
22         if(a1 == a2) cout << ans + a1 << endl;
23         else
24         {
25             int m = min(a1,a2);
26             ans += m +(a1+a2-2*m)/3;
27             cout << ans << endl;
28         }
29     }
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/Carered/p/11020417.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值