发表文章

[最新] CodeForces - 999D D. Equalize the Remainders SET+思维

sdz20172133 3月前 1

版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/sdz20172133/article/details/81501689

题目链接

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

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个数分别对m取余,余数是在0~m-1之间,且每一个都有n/m(保证整数)个。举个例子,n为6,m为3,则每一个数对m去余,保证余为0,1,2,且每一个都有n/m=2个,如果不满足,数可以+1,求最小的加1数量,并输出改变后的数组。

分析:

我们先用num[i]数组记录下每一个取余为i的个数,然后把小于n/m压入一个set,为什么为什么要用set,第一,如果num[i]超了,我们肯定要贪心选择后面离它最近的未满的(4完了后1),所以这个容器要有序,第二,我们可以用一个lower_bound函数,这个函数具体解析(点这里),他的前提条件就是非降序列。

这题比赛是没想到一开始要枚举全部数,yi'k一开始枚举的是全部余数,位置一直不好找。思维啊。

代码实现:

#include<bits/stdc++.h>
using namespace std; 
long long a[200005];
long long num[200005];
set<long long > s;
int main()
{
	   int n,m;
	   
	   scanf("%d%d",&n,&m);
	   for(int i=1;i<=n;i++)
	    {
	    	scanf("%lld",&a[i]);
	    	num[a[i]%m]++;
	    }
	    for(int i=0;i<m;i++)
		{
			if(num[i]<n/m)
				s.insert(i);
		}
		
		set<long long >::iterator it;
		long long ans=0;
		for(int i=1;i<=n;i++)
		{
			long long  t=a[i]%m;
			if(num[t]<=n/m)
				continue;
			  it=s.lower_bound(t); //找不到返回尾位置
			  num[t]--;
			  if(it!=s.end())
			  {
			  	a[i]=a[i]+*it-t;
			  	ans+=*it-t;
			  	num[*it]++;
			  	if(num[*it]==n/m)
			  		s.erase(*it);
			  }
			  else
			  {
			  	
			  	a[i]=a[i]+*s.begin()-t+m;  //注意+m,可能为负数
			  	ans+=*s.begin()-t+m;
			  	num[*s.begin()]++;
			  	if(num[*s.begin()]==n/m)
			  		s.erase(*s.begin());
			  }
			  if(s.size()==0)
			  	break;
			  
		}
		   printf("%lld\n",ans);
		for(int i=1;i<=n;i++)
         if(i!=n)
         printf("%lld ",a[i]);
          else
        printf("%lld",a[i]);
		cout<<endl;
	
}

 

相关推荐
最新评论 (0)
返回
发表文章
sdz20172133
文章数
538
评论数
0
注册排名
713280