Equalize the Remainders(思维)

杰理的蓝牙芯片的key是什么?以及该如何添加key?杰理key文件原理 很多的工程师,在开发杰理蓝牙芯片的第一步就会有一个很深的疑惑,关于key的 1、什么是key文件? 2、Key文件是如何产生的? 3、为什么需要他key文件? 4、如何添加key文件? 5、以及实际开发中的一些报错该如何处理? 阅读详情

Equalize the Remainders

You are given an array consisting of n integers a1,a2,…,an, and a positive integer m. It is guaranteed that m is a divisor of n

.

In a single move, you can choose any position i
between 1 and n and increase ai by 1

.

Let’s calculate cr
(0≤r≤m−1) — the number of elements having remainder r when divided by m. In other words, for each remainder, let’s find the number of corresponding elements in a

with that remainder.

Your task is to change the array in such a way that c0=c1=⋯=cm−1=nm

.

Find the minimum number of moves to satisfy the above requirement.

Input

The first line of input contains two integers n

and m (1≤n≤2⋅105,1≤m≤n). It is guaranteed that m is a divisor of n

.

The second line of input contains n
integers a1,a2,…,an (0≤ai≤109

), the elements of the array.

Output

In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from 0

to m−1, the number of elements of the array having this remainder equals nm

.

In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed 1018

.

Examples
Input

6 3
3 2 0 6 10 12

Output

3
3 2 0 7 10 14 

Input

4 2
0 1 2 3

Output

0
0 1 2 3 
题意:

n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次?
操作:把某个数字+1。输出最少操作次数,和操作后的序列(可以输出任意一种)。

分析:

对于余数x来说,如果余数为x的数没凑够n/m个,那么就让他不变,如果已经凑够了,我们就让他变为距离他最近的没凑够的余数(且通过+1得到的)。
所以,C[i]数组维护余数为i的数的个数,定义set< int>s,维护还没凑够的余数(初始化把0到m-1个余数全部放进去)。所以对于每一个数字求出他的余数d,然后找到s中第一个大于等于d的数字x(直接使用set中的lower_bound),++C[x],如果C[x]==n/m就把x从s中删除。但是注意一种情况,x比s中最大的数还大,那么我们就把x变为s中最小的数,举个例子m=5,余数为4和为0的数字凑够了,此时又来一个余数为4的数,该数应该变为余数为1。维护答案和序列,ans += (x-d+m)%m(需要+1的次数),a[i] += (x-d+m)%m(给原来这个数加上这些个1)。

code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
ll n,m,a[maxn],c[maxn],ans;
set<int> s;
int main(){
    scanf("%lld%lld",&n,&m);
    for(int i = 0; i < m; i++) s.insert(i);
    for(int i = 0; i < n; i++){
        scanf("%lld",&a[i]);
        int d = a[i] % m,x;
        //找到d能加到的最近的余数x
        if(d > *s.rbegin()) x = *s.begin();//d比最大的余数还大就变成第一个余数
        else x = *s.lower_bound(d);
        if(++c[x] == n / m) s.erase(x);
        ans += (x - d + m) % m;
        a[i] += (x - d + m) % m;
    }
    printf("%lld\n",ans);
    for(int i = 0; i < n; i++) printf("%lld%c",a[i],i == n-1 ? '\n' : ' ');
    return 0;
}
第3章 基于三电平空间矢量的中点电位平衡策略 本文档深入探讨了基于三电平空间矢量的中点电位平衡策略。重点在于NPC型三电平逆变器的中点电位平衡问题,由于三电平结构的特点,使得直流侧两个电解电容器的充放电不均匀导致中点电压不均衡。文章通过对不同矢量作用下电流、电压的变化进行了详细的定量和定性分析,并提出了利用冗余小矢量的合理配置以及引入调制因子来解决中点电位偏移的方法,确保中点电压在一个完整的开关周期内的稳定性,进而提高系统的可靠性。 阅读详情

相关推荐

基于STM32 FOC下桥三电阻采样方式的电机相电流重构方法

如何提高三电阻采样的调制率

wanrenqi的博客 9252

Equalize the Remainders

You are given an array consisting of n integers a1,a2,…,an, and a positive integer m. It is guaranteed that m is a divisor of n . In a single move, you can choose any position i between 1 and n and...

codertcm的博客 369

LabVIEW小白福音:手把手教你用NI采集卡搭建振动信号采集系统(附源码和避坑指南)

本文为LabVIEW初学者提供详细的NI采集卡振动信号采集系统搭建指南,涵盖硬件连接、驱动安装、信号处理及性能优化等关键步骤。特别针对振动信号采集系统中的常见问题提供解决方案,并附源码和避坑指南,帮助用户快速上手并避免常见错误。

weixin_30485379的博客 115

D. Equalize the Remainders[模拟+set中lower_bound效率问题]

D. Equalize the Remainders题意:要求改变一个数组,使得模m后,结果为0,1,2,3,...,m-1都是n/m个,每次操作可以选择一个数+1,问至少执行多少次,并输出最终的数组思路:模拟当前元素应该往哪个元素去改变注意:std::set::lower_bound的复杂度为logN,而std::lower_bound的复杂度在set里是logn+n,原因大致是set是一颗平衡...

Haipai1998的博客 465

国产麒麟系统下Kettle安装避坑指南:解决aarch64架构不支持问题

本文详细解析了在国产麒麟系统上部署Kettle时遇到的aarch64架构不支持问题,提供了从获取适配的SWT库、修改启动脚本到生产环境调优的完整解决方案。文章重点解决了ARM架构与Java GUI工具的兼容性挑战,并分享了内存配置、字体显示及Carte服务部署等实战经验,助力用户在国产化环境中稳定运行ETL流程。

weixin_29197835的博客 578

Jetbrains IDE 配置上Gemini Code 插件,让你的的编程高效起飞~

Gemini Code Assist是 Google 家产品,基座模型为最新的Gemini 2.5 Pro。本篇实践了在 WebStrom 中Gemini Code Assist插件的高效编程。

2301_76993408的博客 4604

Equalize the Remainders 【CodeForces - 999D】【思维】

题目连接 题目大意 有n个数的序列,有一个m,保证m被n整除,每次操作可以任选一个数使其加一。问操作多少次可以使得这些数取模m得到的1到m-1的个数都为n/m 解题思路 我们开一个set记录取余后小于n/m的数,然后枚举每个数字。取余小于n/m的就不用管,大于的让他加1编程大于等于他最小的那个set里的数 #include<bits/stdc++.h> using namespace ...

qq_41925919的博客 180

易智联LM401模块学习(一)——初识Lora

Lora

weixin_46046597的博客 1063

Codility经典算法题之十五:Triangle

Task description:An array A consisting of N integers is given. A triplet (P, Q, R) istriangularif 0 ≤ P &lt; Q &lt; R &lt; N and:A[P] + A[Q] &gt; A[R],A[Q] + A[R] &gt; A[P],A[R] + A[P] &gt; A[Q].For...

数据库 1531

9.23华为OD机试真题 新系统 - 小菊的寻宝之旅 (Java/Py/C/C++/Js/Go)

9.23华为OD机试真题 新系统 - 小菊的寻宝之旅 (Java/Py/C/C++/Js/Go)

算法大师! 1197

FPGA 学习笔记:Vivado 工程更改FPGA 型号

FPGA 学习笔记:Vivado 工程更改FPGA 型号

坚韧与专注 1万+

Equalize the Remainders(CF-999D)

Problem Description You are given an array consisting of n integers a1,a2,…,an, and a positive integer m. It is guaranteed that m is a divisor of n. In a single move, you can choose any position i ...

Alex_McAvoy的博客 618

路径规划 | 图解粒子群(PSO)算法(附ROS C++仿真)

粒子群算法(PSO)的基本思想是模拟鸟群觅食机制,将鸟类的觅食方向分为自我认知部分和社会部分,个体自主探索群体周围空间的同时,也随着群体的主流方向前进,不断逼近最优目标。本文详解PSO算法基本概念,并给出ROS C++实现加深理解

FRIGIDWINTER的博客 2624

Equalize the Remainders(set二分+思维)

You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an, and a positive integer mm. It is guaranteed that mm is a divisor of nn. In a single move, you can choose any position ii between ...

starlet_kiss的博客 430

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

【CodeForces - 999D】Equalize the Remainders(思维,贪心)

题干: You are given an array consisting ofnnintegersa1,a2,…,ana1,a2,…,an, and a positive integermm. It is guaranteed thatmmis a divisor ofnn. In a single move, you can choose any positioniibe...

xuanweiace的博客 532

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

CodeForces - 999D Equalize the Remainders (set+思维)

题目链接:点击这里 题目大意: 给出一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1​,a2​,...,an​ ,每次操作可使其中一个数 +1+1+1 ,现给出一个数 mmm(保证m∣nm|nm∣n) ,设 cic_ici​ 为 aimod  m=ia_i \mod m=iai​modm=i 的数量,求使 c0=c1=...=cm−1=nmc_0=c_1=...=c_{m-1}=\frac nmc0​=c1​=...=cm−1​=mn​ 的最小操作数 题目分析: 一开始

羊驼的博客 201

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

CodeForces ~ 999D ~ Equalize the Remainders (思维 + set二分)

题意 n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 操作:把某个数字+1。输出最少操作次数,和操作后的序列(可以输出任意一种)。 思路 对于余数x来说,如果余数为x的数没凑够n/m个,那么就让他不变,如果已经凑够了,我们就让他变为距离他最近的没凑够的余数(且通过+1得到的)。 所以,C[i]数组维护余数为iii的数的个数,定...

张松超的博客 1181

CodeForces - 999D D. Equalize the Remainders SET+思维

题目链接 D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given an array consisting of nn integers...

pxlsdz的博客 598

2019nyist ccpc final资格选拔赛2 D. Equalize the Remainders 思维贪心模拟

题目链接:http://codeforces.com/problemset/problem/999/D 题意求使数组中所有元素除m的余数相同且等于n/m,的最少操作次数,每次操作可以为一个元素加1. 贪心模拟每个余数超过数量的先变成最近的不满足数量的余数; ac代码: #include<set> #include <cstdio> #include <cst...

ClonH的博客 239

Codeforces Round #490 (Div. 3) A、B、C、D(思维)、E(tarjan)、F(贪心dp)

题目链接:https://codeforc.es/contest/999 嗯,补题还是div3好AK,开了套虚拟,四个题 D题难度1900,独立做出还是挺激动开心的 E题看错了,导致没做出来,是从首都城市到能达到其他城市,我看成从其他城市到达首都了。 前三题水题至极,15分钟A没啥好说的。 A. Mishka and Contest 给n,k,n是数组长度 题意都告诉做法了,只能从两...

ccsu_deer 709

Codeforces Round #550 (Div. 3) D. Equalize Them All(思维)

题目链接:https://codeforces.com/contest/1144/problem/D 题意是给了n个数,对于ai有两种操作,一种是ai = ai + |ai - aj|,另一种是ai = ai - |ai - aj|(i和j必须相邻),问至少多少次操作后能使这n个数相同,输出最少的操作数,以及具体的操作。 首先很容易想到对于一次操作来说可以使两...

Charles_Zaqdt的博客 456

codeforces 999D 贪心+二分查找

D. Equalize the Remainderstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an, a...

include的背后是微不足道的开始 854

Codeforces Round #490 (Div. 3) D. Equalize the Remainders

1,题目描述: D,使余数相等。 给你一个含有n个整数的数组a1,a2,…,an,和一个正整数m。保证m是n的因数。 在单次移动中,你可以选择在1到n之间的任一位置的数ai加1. 计算cr(0~m-1)——每个元素除以m之后的余数r。换句话说,对于每个余数, 找到与它相对应的元素。 你的任务是改变数组的元素使得c0=c1=…=cm-1=n/m; 找到满足上述要求的最小的需要改变的次数...

Light2Chasers的博客 621

codeforces 999D Equalize the Remainders

http://www.elijahqi.win/archives/3822 看做一个有向环 考虑我们其实从每个点出发如果采用最优策略那么一定都是一样的 那么不妨来看 我一遍扫描一边带着前缀和 如果遇到是小于平均值的我优先分配给他我最前面的 注意扫描一遍是不够的 但最多两遍即可 因为最优策略不可能绕一圈以上 #include&lt;bits/stdc++.h&gt; #define l...

Elijahqi 1777

Equalize the Remainders (set的基本操作)

题意 输入两个数m和n,数组a里有m个数,并且m/n=0 数组a0~am-1分别求余n,对应着m个余数,大小为从0到n-1 然后让每个余数的数量相等,且都等与m/n ai中每个数字都只能增加1 问为了增加的最少次数是多少并且输出增加后的数组 n and m (1≤n≤2*10^5,1≤m≤n). (0≤ai≤10^9) 研究题意就研究了好久。。。。 Input 6 3 3...

莉莉莉的博客 320

codeforces 999E Reachability from the Capital

http://www.elijahqi.win/archives/3824 tarjan缩点之后找出所有入度为0的点即可 #include&lt;bits/stdc++.h&gt; using namespace std; inline char gc(){ static char now[1&lt;&lt;16],*S,*T; if (T==S){T=(S=now)+f...

Elijahqi 766
上一篇: oval-and-rectangle HDU - 6362(求定积分+化简)
下一篇: Round Numbers POJ - 3252(数位dp)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址