0
点赞
收藏
分享

微信扫一扫

B. Merge it!(思维+贪心) Codeforces Round #565 (Div. 3)

Villagers 2022-06-24 阅读 81

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

B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组
测试样例

input
2
5
3 1 2 3 1
7
1 1 1 1 1 2 2
output
3
3

Note

Note
In the first query of the example you can apply the following sequence of operations to obtain 3 elements divisible by 3: [3,1,2,3,1]→[3,3,3,1].

In the second query you can obtain 3 elements divisible by 3 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].

题意: 给你一个数组,你可以选择数组中任意两个数进行相加变成新元素加入到数组中,合成的两个数删除,经过最优操作后能够得到多少被B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_ios_02整除的数。

解题思路: 这道题就是贪心裸题。如果一个数已经是B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03的倍数了,那么我们自然无需合成,我们只需要用一个元素。 如果不是呢?我们是不是想进行操作增加是B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_ios_02的倍数的元素, B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03取余余1的数和对B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03取余为B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_07的数配对是不是B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03的倍数,这种情况我们要用B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_07个元素。那么还有就是B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03个对B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03取余为B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_12的数配对和B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03个对B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03取余为B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_07的配对同样可以实现,这里我们需要用B. Merge it!(思维+贪心)   Codeforces Round #565 (Div. 3)_数组_03个元素 。根据贪心,故这里自然是有一个先后顺序的。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int a[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
int res[3]={0};
rep(i,0,n-1){
cin>>a[i];
res[a[i]%3]++;
}
int cnt=0;
cnt+=res[0];
int temp=min(res[1],res[2]);
res[1]-=temp;
res[2]-=temp;
cnt+=res[1]/3+temp+res[2]/3;
cout<<cnt<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论