当前位置:网站首页>2022.8.9考试平衡的余数--1000题解
2022.8.9考试平衡的余数--1000题解
2022-08-10 01:55:00 【bj_hacker】
题目
1、平衡的余数–1000
时间限制: | 空间限制:
题目描述:
给出一个正整数 ( 能被3整除)和一个大小为 的整数数组 。
在一次操作中,你可以选择任意一个 中的数并让它增加1。你可以对同一个位置上的数进行多次该操
作。
请求出至少需要多少步操作(可以是0次),使 中模3余 的数的个数相等。共 组测试数据。
输入格式:
第一行仅有一个正整数 ( ),表示测试数据的组数。
接下来有 组测试数据,每组共两行:
第一行仅一个正整数 ( , 能被3整除,且所有测试数据中 之和不超过
),表示数组大小;
第二行有 个整数 ( )用空格隔开。
输出格式:
对于每组测试数据,输出一行一个整数,表示至少多少次操作使 符合条件。
思路
余数为0 1 2的传导构成了一个环,所以只需要判断一遍与平均值的关系,传导即可。
代码实现
#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;
}
边栏推荐
- Visual low-code system practice based on design draft identification
- The flask to add and delete
- SQLserver加个判断
- Unity vertex animation
- Button countdown reminder
- 【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
- 【引用计数器及学习MRC的理由 Objective-C语言】
- Algorithm and voice dialogue direction interview question bank
- FusionCompute产品介绍
- ImportError: Unable to import required dependencies: numpy
猜你喜欢
随机推荐
Premint工具,作为普通人我们需要了解哪些内容?
翻译工具-翻译工具下载批量自动一键翻译免费
《GB39707-2020》PDF download
The shell specifies the parameter name to pass the parameter
Visual low-code system practice based on design draft identification
宝塔服务器PHP+mysql网页URL跳转问题
【wpf】自定义事件总结(Action, EventHandler)
常用正则备查
RESOURCE_EXHAUSTED: etcdserver: mvcc: database space exceeded
数据治理(五):元数据管理
Golang nil的妙用
高压之下,必有懦夫
Unity image is blurry after using long image
首次在我们的centos登录我们的Mysql
2022年8月8日-2022年8月15日,ue4视频教程+插件源码()
【SSRF漏洞】实战演示 超详细讲解
Initial attempt at UI traversal
数据库治理利器:动态读写分离
【干货】集成学习原理总结
51单片机驱动HMI串口屏,串口屏的下载方式