Codeforces--1176B--Merge it!

该博客主要介绍了Codeforces的一道编程题目,内容涉及数组操作和整除性质。题目要求通过将数组中的两元素替换为它们的和,求解最多可以得到多少个能被3整除的元素。博主提出了一个简单的贪心算法作为解决方案。
摘要由CSDN通过智能技术生成

题目描述:
You are given an array a consisting of n integers a1,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] you can obtain the following arrays: [3,4], [1,6] and [2,5].
Your task is to find the maximum possible number of elements divisible by 3 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.
You have to answer t independent queries.
输入描述:
The first line contains one integer t (1≤t≤1000) — the number of queries.
The first line of each query contains one integer n (1≤n≤100).
The second line of each query contains n integers a1,a2,…,an (1≤ai≤109).
输出描述:
For each query print one integer in a single line — the maximum possible number of elements divisible by 3 that are in the array after performing described operation an arbitrary (possibly, zero) number of times.
输入:
2
5
3 1 2 3 1
7
1 1 1 1 1 2 2
输出:
3
3
题意:
给您一个由n个整数a1,a2,…,an组成的数组 。 在一个操作中,您可以选择数组中的两个元 素,然后用等于其总和的元素替换它们(插入新元素的位置无关紧要)。例如,从数组[2,1,4] 您 可以获得以下数组:[3,4],[1,6]和[2,5] 。 您的任务是找到执行此操作任意次(可能为零)次后, 数组中能被3整除的元素个数的大值。
题解
简单贪心,匹配一下就好了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值