当前位置:网站首页>2022.8.9 Remainder of Exam Balance--1000 Question Solutions
2022.8.9 Remainder of Exam Balance--1000 Question Solutions
2022-08-10 03:20:00 【bj_hacker】
题目
1、Balanced remainder–1000
时间限制: | 空间限制:
题目描述:
给出一个正整数 ( 能被3整除)和一个大小为 的整数数组 .
在一次操作中,You can choose either one and let it increase1.You can do this multiple times on the same number
作.
The minimum number of steps required to request(可以是0次),使 中模3余 are equal in number.共 组测试数据.
输入格式:
第一行仅有一个正整数 ( ),表示测试数据的组数.
接下来有 组测试数据,There are two lines in each group:
The first line contains only one positive integer ( , 能被3整除,且所有测试数据中 之和不超过
),表示数组大小;
第二行有 个整数 ( )用空格隔开.
输出格式:
对于每组测试数据,输出一行一个整数,Indicates how many operations to use at least 符合条件.
思路
余数为0 1 2The conduction forms a ring,So you only need to judge the relationship with the average once,Conduction can.
代码实现
#include<bits/stdc++.h>
using namespace std;
int t,n,ans;
int cnt[3];
int main(){
scanf("%d",&t);
while(t--){
ans=0;
memset(cnt,0,sizeof(cnt));
scanf("%d",&n);
for(int i=1;i<=n;i++){
int a;
scanf("%d",&a);
cnt[a%3]++;
}
int op=n/3;
if(cnt[0]>op){
int s=cnt[0]-op;
ans+=s;
cnt[0]=n/3;
cnt[1]+=s;
}
else if(cnt[0]<op){
int s=op-cnt[0];
ans+=s;
cnt[2]-=s;
cnt[0]=n/3;
}
if(cnt[2]>op){
int s=cnt[2]-op;
ans+=s;
cnt[0]+=s;
cnt[2]=n/3;
}
else if(cnt[2]<op){
int s=op-cnt[2];
ans+=s;
cnt[2]=n/3;
cnt[1]-=s;
}
if(cnt[1]>op){
int s=cnt[1]-op;
ans+=s;
cnt[1]=n/3;
cnt[2]+=s;
}
else if(cnt[1]<op){
int s=op-cnt[1];
ans+=s;
cnt[1]=n/3;
cnt[0]-=s;
}
printf("%d\n",ans);
}
return 0;
}
边栏推荐
- 月薪35K,靠八股文就能做到的事,你居然不知道
- Algorithm and voice dialogue direction interview question bank
- Linux(Centos7)服务器中配置Mysql主从数据库,以及数据库的安装,防火墙操作
- 通关剑指 Offer——剑指 Offer II 012. 左右两边子数组的和相等
- LeetCode每日两题01:移动零 (均1200道)方法:双指针
- grafana9配置邮箱告警
- 《GB39732-2020》PDF download
- 跨站请求伪造(CSRF)攻击是什么?如何防御?
- 【8.8】代码源 - 【不降子数组游戏】【最长上升子序列计数(Bonus)】【子串(数据加强版)】
- 【Kali安全渗透测试实践教程】第8章 Web渗透
猜你喜欢
随机推荐
Fusion Compute网络虚拟化
【Kali安全渗透测试实践教程】第7章 权限提升
【Kali安全渗透测试实践教程】第9章 无线网络渗透
免费文档翻译软件电脑版软件
2022.8.8考试从记忆中写入(memory)题解
微透镜阵列的高级模拟
常用正则备查
2022.8.9考试立方和--1100题解
算法与语音对话方向面试题库
2022.8.8考试摄像师老马(photographer)题解
Open3D 泊松盘网格采样
用于X射线光学器件的哈特曼波前传感器
在蓝图中给组件动态加子Actor组件
2022.8.8考试区域链接(district)题解
2020.11.22考试哥德巴赫猜想题解
In automated testing, test data is separated from scripts and parameterized methods
HRnet
3dmax如何制作模型走路动画
GDB之指令基础参数
基于C51的中断控制