B. Merge it!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers a1,a2,…,ana1,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][2,1,4] you can obtain the following arrays: [3,4][3,4], [1,6][1,6] and [2,5][2,5].
Your task is to find the maximum possible number of elements divisible by 33 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.
You have to answer tt independent queries.
Input
The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of queries.
The first line of each query contains one integer nn (1≤n≤1001≤n≤100).
The second line of each query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Output
For each query print one integer in a single line — the maximum possible number of elements divisible by 33 that are in the array after performing described operation an arbitrary (possibly, zero) number of times.
Example
input
Copy
2 5 3 1 2 3 1 7 1 1 1 1 1 2 2
output
Copy
3 3
Note
In the first query of the example you can apply the following sequence of operations to obtain 33 elements divisible by 33: [3,1,2,3,1]→[3,3,3,1][3,1,2,3,1]→[3,3,3,1].
In the second query you can obtain 33 elements divisible by 33 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]
解题说明:此题是一道贪心题,将数列按被3除的余数分为三组,然后将余1组和余2组组合为3的倍数,然后将剩下的余1或余2组中的数字3个相加再次得到3的倍数,这样确保3的倍数的数字最多。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;int main()
{int t, y, q, m;scanf("%d", &t);while (t--) {scanf("%d", &y);int nt = 0, wq[5] = { 0 };while (y--) {scanf("%d", &q);wq[q % 3]++;}nt = wq[0];m = min(wq[1], wq[2]);nt += m;wq[1] -= m;wq[2] -= m;nt += (wq[1] / 3 + wq[2] / 3);printf("%d\n", nt);}return 0;
}
B. Merge it!相关推荐
- GitHub 中 Merge pull request 的 3 中选项说明
Merge pull request 提供了 3 种 merge 方法: Create a merge commit:GitHub 的底层操作是 git merge --no-ff.feature 分 ...
- Android布局优化之include、merge、ViewStub
include:引入重复使用的相同布局 merge:减少include布局的层级,将子元素直接添加到merge标签的parent中 ViewStub:其实就是一个宽高都为0的一个View,它默认是不可 ...
- SQL 利用merge 同步数据库之间表的数据
同步两个数据库之间两表的数据也许的数据库管理员偶尔需要做的一件事情,下面来记录一下常用的两种方法: 方法一:使用delete.truncate 方法二:使用 merge into ,Merge是在S ...
- update 改写 merge into
update语句改写成merge into有时会提高运行速度 看两个案例 1.根据业务将两个嵌套子查询改写成max,速度有3min提升到3s UPDATE OPER_792.LL_SCB_YDKB_2 ...
- 分离颜色通道(split)和多通道融合(merge)
//通道的拆分 Mat srcImg=imread("1.jpg"); Mat ImgBChannel,ImgGChannel,ImgRChannel,mergeImg; vect ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- Oracle中的MERGE语句
转自http://blog.chinaunix.net/space.php?uid=16981447&do=blog&cuid=430716 做了简单的格式整理,加入了一点点原创的东西 ...
- MYSQL的MERGE存储引擎
MYSQL的引擎不是一般的多,这次说到的是MERGE,这个引擎有很多特殊的地方: MERGE引擎类型允许你把许多结构相同的表合并为一个表.然后,你可以执行查询,从多个表返回的结果就像从一个表返回的结果 ...
- OpenCV代码提取:merge/split函数的实现
对OpenCV中的merge/split函数进行了实现,经测试,与OpenCV3.1结果完全一致. merge实现代码merge.hpp: // fbc_cv is free software and ...
- Oracle中merge into的使用
http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5 ...
最新文章
- 业务高峰期不小心拔掉了服务器电源!!
- Conficker蠕虫新动作 P2P更新5月份关闭
- 怎么给vue定义全局方法
- 走近分形与混沌(part13)--自然现象就其本质来说,是复杂而非线性的
- Github客户端使用教程
- modulenotfounderror python_python程序在命令行执行提示ModuleNotFoundError: No module named 'XXX' 解决方法...
- Python压缩图片到指定大小
- python ipaddr库_python访问纯真IP数据库的代码
- google浏览器添加抓包插件
- 有了这十五款编程游戏,谁都可以学编程!
- 计算机怎么看事件管理,用事件查看器查看电脑使用记录
- 国庆长假来啦!这些消遣目的地最适合程序员
- EIGRP优化—末节路由简介
- mysql sql where or_SQL的WHERE子句中包含多个AND和OR
- 信捷原创程序,8个伺服轴。 PLC:信捷XDM-60T10 HMI:信捷TG765 总体I/O在200个点之内
- 基础C语言代码(10题)
- c语言源代码万年历,万年历-C语言源代码.pdf
- 【编译原理】符号表的实现C语言
- 饭店 火锅店无线网络覆盖 案例
- VMware安装win10遇到的一个错误及解决方式