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=nm
.
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 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
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
题目大意: 有n个数,还有一个m要使得下面的n个数比如 a1,a2,,,an 这n个数对m取模得到的 0~m-1 种数相等。。。
这题目看得我一脸懵逼。。。。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<set>
using namespace std;
#define ll long long
ll a[200000+5];
ll cnt[200000+5];
int main()
{
set<ll>s;
ll n,m;
cin>>n>>m;
ll num=n/m;
memset(cnt,0,sizeof(cnt));
for(ll i=0;i<n;i++)
{
scanf("%lld",&a[i]);
cnt[a[i]%m]++;
}
for(ll i=0;i<m;i++)
{
if(cnt[i]<num)
s.insert(i);
}
ll ans=0;
set<ll >::iterator it;
for(ll i=0;i<n;i++)
{
if(s.size()==0)
break;
ll x=a[i]%m;
if(cnt[x]<=num)
continue;
cnt[x]--;
it=s.lower_bound(x);
if(it!=s.end())
{
a[i]=a[i]+*it-x;
ans+=*it-x;
cnt[*it]++;
if(cnt[*it]==num)
s.erase(*it);
}
else
{
a[i]=a[i]+m-x+*s.begin();
ans=ans+m-x+*s.begin();
cnt[*s.begin()]++;
if(cnt[*s.begin()]==num)
s.erase(*s.begin());
}
}
cout<<ans<<endl;
for(ll i=0; i<n; i++)
printf("%lld ",a[i]);
return 0;
}