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 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 11 and nn and increase aiai by 11.

Let's calculate crcr (0≤r≤m−1)0≤r≤m−1) — the number of elements having remainder rr when divided by mm. In other words, for each remainder, let's find the number of corresponding elements in aa with that remainder.

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

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

Input

The first line of input contains two integers nn and mm (1≤n≤2⋅105,1≤m≤n1≤n≤2⋅105,1≤m≤n). It is guaranteed that mm is a divisor of nn.

The second line of input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤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 00 to m−1m−1, the number of elements of the array having this remainder equals nmnm.

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 10181018.

Examples
input

Copy
  1. 6 3
    3 2 0 6 10 12
output

Copy
  1. 3
    3 2 0 7 10 14
input

Copy
  1. 4 2
    0 1 2 3
output

Copy
  1. 0
    0 1 2 3

给你N个数 你可以对这些数 + 1 操作

所有数对n取模后1->m-1 每一个数都出现n/m次

求最少的操作次数

你第一次扫一遍 看看比 n/m大的 放入set里面  因为应该对超过了n/m的进行操作

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn = 2e5 + ;
  5. const int INF = 0x7fffffff;
  6. LL n, m, num, ans, a[maxn], b[maxn];
  7.  
  8. int main() {
  9. scanf("%lld%lld", &n, &m);
  10. for (int i = ; i < n ; i++) {
  11. scanf("%lld", &a[i]);
  12. b[a[i] % m]++;
  13. }
  14. set<LL>st;
  15. ans = , num = n / m;
  16. for (int i = ; i < m ; i++)
  17. if (b[i] < num) st.insert(i);
  18. for (int i = ; i < n ; i++) {
  19. if (b[a[i] % m] <= num) continue;
  20. b[a[i] % m]--;
  21. set<LL>::iterator it;
  22. it = st.lower_bound(a[i] % m);
  23. if (it != st.end()) {
  24. ans += *it - a[i] % m;
  25. a[i] += *it - a[i] % m;
  26. b[*it]++;
  27. if (b[*it] == num) st.erase(it);
  28. } else {
  29. LL temp = m - a[i] % m;
  30. ans += *st.begin() + temp;
  31. a[i] += *st.begin() + temp;
  32. b[*st.begin()]++;
  33. if (b[*st.begin()] == num) st.erase(st.begin());
  34. }
  35. }
  36. printf("%lld\n", ans);
  37. for (int i = ; i < n ; i++)
  38. printf("%lld ", a[i]);
  39. printf("\n");
  40. return ;
  41. }

D. Equalize the Remainders (set的基本操作)的更多相关文章

  1. Codeforces 999D Equalize the Remainders (set使用)

    题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...

  2. D. Equalize the Remainders set的使用+思维

    D. Equalize the Remainders set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...

  3. D. Equalize the Remainders 解析(思維)

    Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...

  4. CodeForces - 999D Equalize the Remainders (模拟+set)

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

  5. CodeForces-999D Equalize the Remainders

    题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...

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

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

  7. CoderForces999D-Equalize the Remainders

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

  8. Codeforces Round #490 (Div. 3)

    感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...

  9. [Codeforces]Codeforces Round #490 (Div. 3)

    Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...

随机推荐

  1. python七类之字符串

    字符串 一.关键字:str 字符串是不可变的可迭代的数据类型 二.方法: name = 'alex uwu sir' .title #标题 使首字母大写​​ 只要有特殊字符隔开,才能分别认为是多个单词 ...

  2. Go生成UUID

    Go生成UUID 在实际项目中,是经常会使用到一个唯一标识的,比如唯一标识一条记录等,使用C#得到唯一标识是很容易的.例 string guid = Guid.NewGuid().ToString() ...

  3. WPF中使用定时器的注意事项

    原文:WPF中使用定时器的注意事项 注意事项 要使用System.Windows.Threading.DispatcherTimer,而不能使用System.Timers.Timer. 原因是WPF是 ...

  4. 如何在Moodle中显示PPT课件

    Moodle中目前是不直接支持PPT的,所以需要曲线救国: 1.安装 iSpring Free 8版本,免费版,其实是一个PPT的插件,在PPT的工具栏中有显示. 2.打开PPT后,直接在该工具中进行 ...

  5. c/c++ 数组传参

    在c/c++中,在进行数组传参时,数组的元素个数默认是不作为实参传入调用函数,也就是说c/c++ 不允许向函数传递一个完整的数组作为参数 实例: 1.形式参数是一个指针,实参包括数组长度: 1 voi ...

  6. 获取单片机唯一id(stm32获取单片机唯一id)

    stm32唯一id: 不同型号的stm32单片机,id不在同一地址上!具体地址可以通过用户手册中的Device electronic signature>Unique device ID reg ...

  7. Java - 问题集 - 导出csv文件中文乱码

    微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头. FileOutputStream fos = new FileOutputStream(new File(&q ...

  8. json 处理日期格式

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date createT ...

  9. python自动化之BDD框架之lettuce初识问题集

    最近在学习虫师老师编写的python自动化的书.其中讲到了BDD结构lettuce入门一章. 因为是小白,按部就班地进行操作,先不谈执行操作如何,先来讲讲遇到的几个坑,和怎么解决的: 第一坑:pyth ...

  10. OpenStack配置虚拟机vcpu绑定步骤 转至元数据结尾

    . Changed in compute node: 给宿主机预留资源: 宿主机可用cpu:cpuid – cpuid 宿主机可用内存:25G #vim /etc/nova/nova.conf vcp ...