CodeForces - 999D Equalize the Remainders 线段树 单点更新 查询

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

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 

题解:线段树维护 区间是否有没达到规定数目的数 

先预处理,已经达到数目的 然后更新删除; 需要修改的x 先查询(x,m-1)是否有没达到,若没有在查找(0,x),统计余数数目,达到规定数目,在更新删除即可

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=2*1e5+10;
typedef long long ll;
int n,m;
struct node{
	ll x;
	int flag;
}a[N];
int vis[N];
struct node1{
	int l,r;
	int val;
}tree[N<<2];
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	tree[cur].val=r-l+1;
	if(l==r)
	{
		tree[cur].val=1;
		return;
	}
	int mid=(r+l)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
}
void update(int pos,int cur,int val)
{
	tree[cur].val--;
	if(tree[cur].l==tree[cur].r)
	{
		return;
	}
	if(pos<=tree[cur<<1].r) update(pos,cur<<1,val);
	else update(pos,cur<<1|1,val);
}
int cnt;
void query(int pl,int pr,int cur)
{
	if(cnt!=-1)return;
	if(tree[cur].val==0) return;
	if(tree[cur].l==tree[cur].r)
	{
		cnt=tree[cur].l;
		return;
	}
	if(pl<=tree[cur<<1].r) query(pl,pr,cur<<1);
	if(pr>=tree[cur<<1|1].l) query(pl,pr,cur<<1|1);
}
int main()
{
	scanf("%d%d",&n,&m);
	int ans=n/m;
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i].x);
	ll res=0;
	build(0,m-1,1);
	for(int i=1;i<=n;i++)
	{
		int mm=a[i].x%m;
		if(vis[mm]<ans)
		{
			vis[mm]++;
			if(vis[mm]==ans) update(mm,1,-1);
			a[i].flag=1;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i].flag) continue;
		int mm=a[i].x%m;
		cnt=-1;
		query(mm,m-1,1);
		if(cnt==-1) query(0,mm,1);
		vis[cnt]++;
		if(vis[cnt]==ans) update(cnt,1,-1);
		if(cnt>a[i].x%m) res+=cnt-a[i].x%m,a[i].x+=cnt-a[i].x%m;
		else res+=m-a[i].x%m+cnt,a[i].x=a[i].x/m*m+cnt+m;
	}
	cout<<res<<endl;
	for(int i=1;i<=n;i++)
		printf("%lld%c",a[i].x," \n"[i==n]);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值