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 12Output3
3 2 0 7 10 14 Input4 2
0 1 2 3Output0
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能加到的最近的余数xif(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;
}

Equalize the Remainders(思维)相关推荐

  1. Equalize the Remainders(set二分+思维)

    You are given an array consisting of nn integers a1,a2,-,ana1,a2,-,an, and a positive integer mm. It ...

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

    题干: You are given an array consisting of nn integers a1,a2,-,ana1,a2,-,an, and a positive integer mm ...

  3. Equalize the Array(思维)

    题目链接: Equalize the Array 大致题意 给定一个长度为n的序列, 要求删除序列中尽可能少的元素, 使得剩余序列中出现的每一个元素数量相同. 解题思路 首先最终的序列, 一定是所有的 ...

  4. CodeForces-999D Equalize the Remainders (贪心+神奇的STL)

    题意:给你一个n,m;其中n一定能被m整除,然后给你n个数 有一种操作   选择n个数中的任意一个,使其+1: 条件: Ci 属于[0,m-1]  Ci代表ai模m的余数为i的个数 且都等于n/m; ...

  5. Equalize Them All(思维)

    题目链接:Codeforces Round #550 (Div. 3) – D 题意:给你一个数组,对于每一个数都可以有两种操作类型,问你通过最少几次操作之后数组里的元素都相同,题目保证这个数存在. ...

  6. F. Equalize the Array(思维+前缀和)

    https://codeforces.com/contest/1490/problem/F 题意:问最小删除多少个数字使得剩下的数字出现次数都一样. 思路: 开始就是暴力枚举剩下一个次数,这样肯定要用 ...

  7. B. Equalize by Divide - 思维+构造+排序

    题意: 给定一个数组,可以进行任意多次以下操作: 1.选择第i和第j个数. 2.使a[i]=a[i]/a[j](向上取整). 不可以插入或者删减数组元素,求多少次使数组元素都相同,输出次数以及每次操作 ...

  8. 游戏ui切图,颜色通道_什么是ui通道设计,为什么如此重要

    游戏ui切图,颜色通道 Our approach to interface design has changed dramatically since the rise of mobile devic ...

  9. CoderForces999D-Equalize the Remainders

    D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...

最新文章

  1. Python爬虫之诗歌接龙
  2. java打包后的怎么转成源码_PDF转成Word或PPT后还是图片是怎么回事?
  3. c++Data Member的绑定
  4. information_schema系列八(事物,锁)
  5. Activiti Modeler发布以及教程
  6. JavaScript中的String substring()方法和示例
  7. linux设置date-hwclock-clock
  8. 让你更好的使用jQuery插件
  9. 编码的奥秘:存储器组织
  10. 刚刚,陶哲轩惨遭3个物理学家狠狠打脸,一条数学公式或将引起教科书改革
  11. 如何使用PTPX预估芯片功耗
  12. Linux---带你区分根目录 和 家目录
  13. openssl rand
  14. Linux---查看内存型号
  15. Arduino ESP32 TFTLCD ST7735 代码和原理
  16. OBS录屏低音过重/背景嗡嗡声的解决方法
  17. 【PS】图片背景透明化
  18. Android开发 - PsyDuck说明书
  19. OSChina 周六乱弹 ——通常他们这么修复的bug,我都接受不了
  20. buildroot 修改和保存xxx_defconfig make savedconfig错误解决

热门文章

  1. CabloyJS一站式助力微信、企业微信、钉钉开发 - 钉钉篇
  2. 磁吸数据线的特点与优势
  3. 【微信小程序】模板消息推送(测试成功)。
  4. 在国内愚人节可以开的10个玩笑
  5. 基于nodejs+vue的社区问答网站与设计
  6. 终于给自己买了台电脑
  7. Linux常用操作命令(一)
  8. 投屏电脑怎么操作?投屏电脑最常用的4种方式
  9. 基于SSM+SpringBoot+MySQL+LayUI的高校学生评教系统
  10. leetcode 最常见的150道前端面试题(简单题下)