题目链接

https://vjudge.net/problem/CodeForces-999D

题面

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 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=\(\frac{n}m\).

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 \(\frac{n}m\)

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 \(10^{18}\).

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

题解

当初比赛中做麻烦,直接写正解吧

用set维护现在个数还不够\(\frac{n}{m}\)的余数,然后从1到n循环,计算a[i]的余数,找到set中第一个大于等于这个余数的余数,把这个数的余数补成它,如果没有大于大于等于的,就找到set中的第一个数,这样每个余数不足的都由离他最近的数补成,从而使操作次数最小,用一个change数组记录每个数变化了多少,最后输出a[i]+change[i]即可

#include<bits/stdc++.h>
#define N 200050
using namespace std;
typedef long long ll;
int a[N];
int cnt[N];
int change[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
ll ans = 0;
set<int> s;
for (int i = 0; i < m; i++) {
s.insert(i);
}
for (int i = 1; i <= n; i++) {
int tmp = a[i] % m;
int x = tmp > *s.rbegin() ? *s.begin() : *s.lower_bound(tmp);
cnt[x]++;
if (cnt[x] == n / m) s.erase(x);
ans += (x - tmp + m) % m;
change[i] += (x - tmp + m) % m;
}
printf("%lld\n", ans);
for (int i = 1; i <= n; i++) {
printf("%d ", a[i] + change[i]);
}
return 0;
}

CodeForces-999D Equalize the Remainders的更多相关文章

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

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

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

  3. D. Equalize the Remainders (set的基本操作)

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

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

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

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

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

  6. codeforces 616E Sum of Remainders (数论,找规律)

    E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces 616E - Sum of Remainders

    616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As ...

  8. codeforces 616E. Sum of Remainders 数学

    题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...

  9. Codeforces 1037C Equalize

    原题 题目大意: 给你两个长度都为\(n\)的的\(01\)串\(a,b\),现在你可以对\(a\)串进行如下两种操作: 1.交换位置\(i\)和位置\(j\),代价为\(|i-j|\) 2.反转位置 ...

随机推荐

  1. c#按钮如何避免重复点击后报错

    前言:感谢51·halcon的绝地武士大佬啊,虽然你不认识我,但是我从你那学到了很多知识,真的感谢您对知识的无私传播哈哈(两天一个博客有在坚持的,都是草稿,等这个实习阶段过去了再回来整理博客~) bt ...

  2. 【洛谷P1525】[NOIP2010]关押罪犯

    关押罪犯 题目链接 思路: 二分图或并查集 这里讲并查集算法: 1.将每对罪犯的冲突关系按影响从大到小排序 2.将集合与(i+n)合并表示编号为i的罪犯不能在该集合内 3.依次从大到小处理冲突关系: ...

  3. 一篇SSM框架整合友好的文章(三)

    ###一.SpringMVC理论 它始终是围绕 handler. 数据模型 model. 页面view进行开发的. 运行流程图: 通过mvc配置文件,配置"中央处理器"dispat ...

  4. javascript入门笔记3-dom

    1.通过ID获取元素 document.getElementById("id") <!DOCTYPE HTML> <html> <head> & ...

  5. 使用poi将excel转换为html,适用本身有导出excel的而现在需要添加网页打印的功能

    PoiExcelToHtmlUtil.java import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.hssf.usermod ...

  6. RabbitMQ (1) 环境安装

    1.下载erlang, 设置系统的环境变量 下载地址:http://www.erlang.org/downloads ERLANG_HOME=D:\Program\erl9.3 Path = %ERL ...

  7. jQuery-laye插件实现 弹框编辑,异步验证,form验证提交

    代码中用到了 jQuery的ajax异步处理,each()循环,submit()页面验证提交form表单,prepend()追加标签,laye插件的弹框效果(如有其它弹框效果可参考官网:http:// ...

  8. python3 练习题100例 (十六)鸡尾酒疗法

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' n = input('请输入一个大于1,小于等于20的整 ...

  9. python pycharm2018开启debug模式

    为什么需要开启DEBUG模式1.如果开启了DEBUG模式,那么在代码中如果抛出了异常,在浏览器的页面中可以看到具体的错误信息,以及具体的错误代码位置,方便开发者调试.2.如果开启DEBUG模式,那么以 ...

  10. [Codeforces947D]Riverside Curio(思维)

    Description 题目链接 Solution 设S[i]表示到第i天总共S[i]几个标记, 那么满足S[i]=m[i]+d[i]+1 m[i]表示水位上的标记数,d[i]表示水位下的标记数 那么 ...