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
题意:构造出这样的一组序列,使得这个数组中的每个数字取模m之后的数字即0~m-1都出现过n/m次。问最少需要操作多少次。
思路:对于余数出现次数少于n/m次的数,我们放到set中去。
对于出现次数多于n/m的数,我们二分去找set中第一个大于这个数的数。如果操作之后这个数的出现次数等于n/m次了,就将之从集合中删除。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=2e5+100;
ll a[maxx];
int b[maxx];
int n,m;int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) {scanf("%I64d",&a[i]);b[a[i]%m]++;}set<ll> s;for(int i=0;i<m;i++){if(b[i]<n/m) s.insert(i);}ll ans=0;for(int i=1;i<=n;i++){if(b[a[i]%m]<=n/m) continue;b[a[i]%m]--;set<ll> ::iterator it=s.lower_bound(a[i]%m);if(it!=s.end())//找到了{ans+=*it-a[i]%m;a[i]+=*it-a[i]%m;b[*it]++;if(b[*it]==n/m) s.erase(it);}else//没有找到的话,就要变成集合中的第一个元素。{ans+=(*s.begin()+m-a[i]%m)%m;a[i]+=(*s.begin()+m-a[i]%m)%m;b[*s.begin()]++;if(b[*s.begin()]==n/m) s.erase(s.begin());}   }cout<<ans<<endl;for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
}

努力加油a啊,(o)/~

Equalize the Remainders(set二分+思维)相关推荐

  1. Equalize the Remainders(思维)

    Equalize the Remainders You are given an array consisting of n integers a1,a2,-,an, and a positive i ...

  2. 【CodeForces - 999D】Equalize the Remainders(思维,贪心)

    题干: You are given an array consisting of nn integers a1,a2,-,ana1,a2,-,an, and a positive integer mm ...

  3. Showstopper [POJ3484] [二分] [思维]

    Description 给你n个数列,问哪一个数字在所有的数列中出现了奇数次(最多一个). Sample Input 1 10 1 2 10 11 10 1 1 10 11 10 1 4 4 1 1 ...

  4. 【题解】CF1070E Getting Deals Done(二分+思维)难度⭐⭐⭐

    CF1070E Getting Deals Done 题意翻译 题目描述 Polycarp有很多工作要做.最近他学会了一条新的时间管理技巧:"如果任务需要五分钟或更短时间,请立即执行&quo ...

  5. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  6. 2019 Multi-University Training Contest 1 - 1004 - Vacation - 二分 - 思维

    http://acm.hdu.edu.cn/showproblem.php?pid=6581 一开始想了好几个假算法.但是启发了一下潘哥,假如时间知道的话就可以从头开始确定各个车的位置.那么直接 \( ...

  7. CodeForces - 1370D Odd-Even Subsequence(二分+思维)

    题目链接:点击查看 题目大意:给出一个数组 a ,现在要求选出一个长度恰好为 k 的子序列 s ,使得的值最小 题目分析:当知道了 k 的大小之后,就可以计算出子序列奇数位置和偶数位置各有多少个数了, ...

  8. CH - 0805 防线(二分+思维)

    题目链接:点击查看 题目大意:假设现在有一个数轴,一共有n组防具,每组防具给出的形式为S,D,E,在数轴上的分布为:S,S+D,S+2D....S+KD,保证S+KD<=E 现在规定数轴上某个点 ...

  9. Anton and Fairy Tale CodeForces - 785C(二分+思维)

    Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right ...

最新文章

  1. html分页自动加载数据库,AngularJS实现分页显示数据库信息
  2. java.lang.StackOverflowError 解决方法
  3. 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
  4. 备忘--Sinfors 设备出厂IP
  5. lua——赢三张牌型处理相关算法(上)——牌值数据
  6. stats | 线性回归(四)——显著性检验和模型评价
  7. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例
  8. Switch case 超经典练习题
  9. spark Drive 与Executor
  10. Linux C语言解析并显示.bmp格式图片
  11. habernet备份mysql_harbor 安装
  12. 如何在camera 驱动中配置HDR的曝光模块
  13. 小米 android 7.0下载地址,小米5安卓7.0公测版固件下载地址 仅限开发版
  14. python模拟登录淘宝参数在哪获取_如何用 Python 模拟登录淘宝?
  15. CSS使用小操作(隐藏滚动,实现三角行等...)
  16. 山东省计算机技能大赛通报,比赛成绩通报
  17. AT89C51(Atmel)芯片制作简易的频率计
  18. CISP查询具体步骤
  19. Latex快速制表工具
  20. JS逆向瑞数4算法专题一

热门文章

  1. 我的世界服务器无限装备指令2020,我的世界区域保护指令_我的世界区域保护指令除了自己手机版刷屏2020_攻略...
  2. 博客等级及其特权福利
  3. p4: php5ts.dll p5: 5.6.14.0,服务器httpd.exe 应用程序错误
  4. java wmi_WMI依赖服务使用WMI查询
  5. C++中default标识的构造函数的说明
  6. Android开发之拍照后图片旋转的问题
  7. php干扰函数,php – 类构造函数干扰另一个类
  8. MailCore2 SDK API
  9. 解决svn log显示no author,no date的方法之一
  10. android studio 65536错误的解决