Codeforces1176B(B题)Merge it!
B. Merge it!
You are given an array aanna1,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][3,4][3,4][1,6][1,6][2,5][2,5]
Your task is to find the maximum possible number of elements divisible by 33
You have to answer tt
The first line contains one integer tt1≤t≤10001≤t≤1000
The first line of each query contains one integer nn1≤n≤1001≤n≤100
The second line of each query contains nna1,a2,…,ana1,a2,…,an1≤ai≤1091≤ai≤109
For each query print one integer in a single line — the maximum possible number of elements divisible by 3
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int n,m;
cin>>n;
for(int i=; i<n; i++) {
int cnt=,cnt1=,a1=,b1=;
int a[];
int b[];
cin>>m;
for(int j=; j<m; j++) {
cin>>a[j];
if(a[j]%==) {
cnt++;
} else {
a[j]%=;
b[cnt1++]=a[j];
}
}
for(int i=; i<cnt1; i++) {
if(b[i]==) {
a1++;
}
if(b[i]==) {
b1++;
}
}
if(a1>b1) {
int h=a1-b1;
if(h>=) {
cnt+=h/;
}
cnt+=b1;
} else {
int h=b1-a1;
if(h>=) {
cnt+=h/;
}
cnt+=a1;
}
cout<<cnt<<endl;
}
}
思路分析:若本身可整除3就加1,将不可整除3的数模3,都转化成1和2,比较1和2的数量,1比2多那就配对2数量个3,然后看1比2多了几个,如果超过3,看有多少个3,再记数。
Codeforces1176B(B题)Merge it!的更多相关文章
- LeetCode算法题-Merge Sorted Array(Java实现)
这是悦乐书的第161次更新,第163篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第20题(顺位题号是88).给定两个排序的整数数组nums1和nums2,将nums2中 ...
- [LC]88题 Merge Sorted Array (合并两个有序数组 )
①英文题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...
- leetcode第22题--Merge k Sorted Lists
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...
- LeetCode算法题-Merge Two Binary Trees(Java实现)
这是悦乐书的第274次更新,第290篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第142题(顺位题号是617).提供两个二叉树,将其合并为新的二叉树,也可以在其中一个二 ...
- 【算法】LeetCode算法题-Merge Two Sorted List
这是悦乐书的第148次更新,第150篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第7题(顺位题号是21).合并两个已排序的链表并将其作为新链表返回. 新链表应该通过拼接 ...
- 第十二题 Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...
- [LC]21题 Merge Two Sorted Lists (合并两个有序链表)(链表)
①英文题目 Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...
- 链表经典题Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- 隐藏响应中的server和X-Powered-By
来源:https://www.yduba.com/biancheng-7831560074.html 有时候,我们用调试工具查看别人的网站时,经常看到 X-Powered-By:PHP/7.1.8 这 ...
- php函数封装
这是一些自定义封装的函数类,调用起来很方便,以后会有更多的封装函数更新! // 弹框跳转function alert($msg,$url=''){ echo "<script>& ...
- PHP 把MYSQL重复ID 二维数组重组为三维数组
应用场景 MYSQL在使用关联查询时,比如 产品表 与 产品图片表关联,一个产品多张产品图片,关联查询结果如下: $arr=[['id'=>1,'img'=>'img1'],['id'=& ...
- python字符串 提取括号中的内容
返回值是一个列表 re.findall(r'[(](.*?)[)]', str1)
- Hyperledger Fabric基础知识
文章目录 什么是Hyperledger Fabric? Hyperledger架构是怎么工作的? Hyperledger交易如何执行 总结 Hyperledger Fabric基础知识 本文我们会介绍 ...
- Python下redis包安装
找到Python的第三方包安装路径,在dos命令行中切换到该目录,输入: pip install redis 最后在Python解释器中即可.
- Echarts设置点击事件
简单明了. echarts初始化完成之后,给实例对象通过on绑定事件. 这里的事件包括: 'click','dblclick','mousedown','mouseup','mouseover','m ...
- 递归与N皇后问题
递归的基本概念 一个函数调用其自身,就是递归 递归的作用 1) 替代多重循环 2) 解决本来就是用递归形式定义的问题 3) 将问题分解为规模更小的子问题进行求解 一行只能有一个皇后,这个根据游戏规则中 ...
- Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...
- 数学--数论--HDU 2104 丢手绢(离散数学 mod N+ 剩余类 生成元)+(最大公约数)
The Children's Day has passed for some days .Has you remembered something happened at your childhood ...