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

Codeforces 1176B - Merge it!


题意:给定序列,任意俩个元素可以相加成一个元素,求序列元素能被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 }

 

相关文章:

  • 2021-04-15
  • 2021-07-20
  • 2022-12-23
  • 2022-03-01
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
猜你喜欢
  • 2021-05-07
  • 2022-02-12
  • 2022-12-23
  • 2022-01-19
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案