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
6 3
3 2 0 6 10 12
output

Copy
3
3 2 0 7 10 14
input

Copy
4 2
0 1 2 3
output

Copy
0
0 1 2 3

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

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

求最少的操作次数

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

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + ;
const int INF = 0x7fffffff;
LL n, m, num, ans, a[maxn], b[maxn]; int main() {
scanf("%lld%lld", &n, &m);
for (int i = ; i < n ; i++) {
scanf("%lld", &a[i]);
b[a[i] % m]++;
}
set<LL>st;
ans = , num = n / m;
for (int i = ; i < m ; i++)
if (b[i] < num) st.insert(i);
for (int i = ; i < n ; i++) {
if (b[a[i] % m] <= num) continue;
b[a[i] % m]--;
set<LL>::iterator it;
it = st.lower_bound(a[i] % m);
if (it != st.end()) {
ans += *it - a[i] % m;
a[i] += *it - a[i] % m;
b[*it]++;
if (b[*it] == num) st.erase(it);
} else {
LL temp = m - a[i] % m;
ans += *st.begin() + temp;
a[i] += *st.begin() + temp;
b[*st.begin()]++;
if (b[*st.begin()] == num) st.erase(st.begin());
}
}
printf("%lld\n", ans);
for (int i = ; i < n ; i++)
printf("%lld ", a[i]);
printf("\n");
return ;
}

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. 43-Identity MVC:UI

    1-打开之前写的MvcCookieAuthSample项目, 在AccountController新加Register,Login方法 public class AccountController : ...

  2. NO-ZERO(空格补全)

    The NO-ZERO command follows the DATA statement REPORT Z_Test123_01. DATA: W_NUR(10) TYPE N. MOVE 50 ...

  3. Python正则表达式中的re.S,re.M,re.I的作用

    正则表达式可以包含一些可选标志修饰符来控制匹配的模式.修饰符被指定为一个可选的标志.多个标志可以通过按位 OR(|) 它们来指定.如 re.I | re.M 被设置成 I 和 M 标志: 修饰符 描述 ...

  4. 什么是 Cookie

    什么是 Cookie? Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递.Cookie 包含每次用户访问站点时 Web 应用程序都可以读取的信息. 例如,如果在用 ...

  5. 3 web服务器:静态文件

    1.处理客户端请求数据 >>> s = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nConnection: keep-alive& ...

  6. linux-clone-ip处理办法

    vim /etc/udev/rules.d/70-persistent-net.rules 步骤1:#将eth0相关的文件给删除 步骤2:#vi /etc/sysconfig/network-scri ...

  7. Java的HashMap和HashTable

    Java的HashMap和HashTable 1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashm ...

  8. Python 3基础教程18-获取用户键盘输入

    有时候,我们需要获取用户的键盘输入的信息,然后得到信息,拿去做一些事情. 请看下面的demo.py # 练习如何通过键盘获取用户输入 x = input('What is your name?') p ...

  9. 深挖 NGUI 基础 之UICamera (二)

    一.UI Camera作用 UICamera需要挂载在摄像机上才能发挥作用 UICamera仅负责 发送NGUI 事件 到 脚本所附加的摄像机中看得到的对象,比如我自定义了NGUI层(在Inspect ...

  10. 寻找完全数(C++)

    [问题描述] 输入一个大于 1 的正整数 n,请你将大于 1 和小于或等于 n 的所有完全数输出.所谓完全数就是因子(不算其本身)之和等于它本身的数.例如 1+2+4+7+14=28,所以 28 是完 ...