Codeforces 1176B - Merge it!

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


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

思路: 对于所有元素进行 模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 }

猜你喜欢

转载自www.cnblogs.com/Carered/p/11020417.html