Skip to content

Commit ac4ab61

Browse files
Add files via upload
1 parent 9109d77 commit ac4ab61

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <cstdio>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
const int MAX_N = 2e5 + 5;
7+
vector <int> position[MAX_N];
8+
9+
int main()
10+
{
11+
typedef long long LL;
12+
int no_of_elements, m;
13+
scanf("%d %d", &no_of_elements, &m);
14+
15+
vector <LL> A(no_of_elements + 1, 0);
16+
for(int i = 1; i <= no_of_elements; i++)
17+
scanf("%I64d", &A[i]);
18+
19+
int target = no_of_elements/m;
20+
for(int i = 1; i <= no_of_elements; i++)
21+
{
22+
position[A[i]%m].push_back(i);
23+
}
24+
25+
long long no_of_changes = 0;
26+
vector <int> extra;
27+
28+
for(int runs = 1; runs <= 2; runs++)
29+
{
30+
for(int i = 0; i < m; i++)
31+
{
32+
while(position[i].size() > target)
33+
{
34+
int index = position[i].back();
35+
36+
extra.push_back(index);
37+
38+
position[i].pop_back();
39+
}
40+
41+
while(position[i].size() < target && extra.size() > 0)
42+
{
43+
int index = extra.back();
44+
45+
no_of_changes += (A[index]%m < i ? i - A[index]%m : m - A[index]%m + i);
46+
47+
A[index] += (A[index]%m < i ? i - A[index]%m : m - A[index]%m + i);
48+
49+
position[i].push_back(index);
50+
51+
extra.pop_back();
52+
}
53+
}
54+
}
55+
56+
printf("%I64d\n", no_of_changes);
57+
for(int i = 1; i <= no_of_elements; i++)
58+
printf("%I64d ", A[i]);
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)