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

In a single move, you can choose any position 1.

Let's calculate 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.

The second line of input contains 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 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
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的进行操作

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 2e5 + 10;
 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 = 0 ; i < n ; i++) {
11         scanf("%lld", &a[i]);
12         b[a[i] % m]++;
13     }
14     set<LL>st;
15     ans = 0, num = n / m;
16     for (int i = 0 ; i < m ; i++)
17         if (b[i] < num)   st.insert(i);
18     for (int i = 0 ; 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 = 0 ; i < n ; i++)
38         printf("%lld ", a[i]);
39     printf("\n");
40     return 0;
41 }

 

 

 

相关文章:

  • 2021-09-04
  • 2021-09-05
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2021-10-25
猜你喜欢
  • 2022-02-03
  • 2021-08-15
  • 2022-12-23
  • 2022-02-25
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
相关资源
相似解决方案