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 文中代码为了不动官网提供的初始 ...
随机推荐
- JVM 真的很难学么?不、只是你“不敢学”而已
JVM 真的很难学么?不.只是你"不敢学"而已 许多招聘的信息上面都说,要了解jvm.多线程什么的对于 java 程序员来说,这是工作好多年的程序员都不一定能掌握的东 ...
- 2019-2020-1 20199328《Linux内核原理与分析》第十一周作业
预备实验部分 2019/11/27 10:17:34 下载安装后的界面,如图1 出现vulnerable字样,发现了shellshock漏洞,如图2 实验部分 2019/11/27 10:26:48 ...
- 都2020年了,这5个java IDE神器你还不知道?
TIOBE的4月份编程语言排行榜出来了,java还是稳坐第一位,java最新的版本也到了13,一直以来java凭借其企业级应用的优势和大量的框架级应用俘获了大量的粉丝和企业客户. 谈到开发者,java ...
- 理解分布式一致性:Paxos协议之Multi-Paxos
理解分布式一致性:Paxos协议之Multi-Paxos Multi-Paxos without failures Multi-Paxos when phase 1 can be skipped Mu ...
- 1309:【例1.6】回文数(Noip1999)
传送门:http://ybt.ssoier.cn:8088/problem_show.php?pid=1309 [题目描述] 若一个数(首位不为零)从左向右读与从右向左读都是一样,我们就将其称之为回文 ...
- Java演示设计模式中的写代码的代码
下边代码内容是关于Java演示设计模式中的单件模式的代码,应该是对小伙伴们有所用处. public class SimpleSingleton { private static SimpleSingl ...
- 1月份中国综合PMI指数为53.2% 企业生产经营活动总体增速加快
中新社北京1月31日电 (记者 王恩博)中国国家统计局31日发布数据显示,2019年1月份,中国综合PMI产出指数为53.2%,比上月上升0.6个百分点,表明中国企业生产经营活动总体增速加快. 其中, ...
- python(while 循环语句)
一.循环语句 1.while 循环 当我们在 python 中需要重复执行一些动作的时候,这时我们就要用到循环 while 循环的结构,当条件成立的时候,就会执行里面的代码 while 循环不断的运行 ...
- visibility: hidden 和 display: none的区别
相同点: 两者都可以将dom元素隐藏 不同点: 1.display: none 隐藏之后不占用文档流,而visibility: hidden却会占用文档流,如果要在隐藏元素的同时获取其尺寸信息,那就可 ...
- CentOS-6.5操作系统安装
1.创建Linux虚拟机(如图所示) 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 ...