File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Contests/Div 3 490/Programs Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments