CodeForces-999D Equalize the Remainders
题目链接
https://vjudge.net/problem/CodeForces-999D
题面
Description
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=\(\frac{n}m\).
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 \(\frac{n}m\)
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 \(10^{18}\).
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
题解
当初比赛中做麻烦,直接写正解吧
用set维护现在个数还不够\(\frac{n}{m}\)的余数,然后从1到n循环,计算a[i]的余数,找到set中第一个大于等于这个余数的余数,把这个数的余数补成它,如果没有大于大于等于的,就找到set中的第一个数,这样每个余数不足的都由离他最近的数补成,从而使操作次数最小,用一个change数组记录每个数变化了多少,最后输出a[i]+change[i]即可
#include<bits/stdc++.h>
#define N 200050
using namespace std;
typedef long long ll;
int a[N];
int cnt[N];
int change[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
ll ans = 0;
set<int> s;
for (int i = 0; i < m; i++) {
s.insert(i);
}
for (int i = 1; i <= n; i++) {
int tmp = a[i] % m;
int x = tmp > *s.rbegin() ? *s.begin() : *s.lower_bound(tmp);
cnt[x]++;
if (cnt[x] == n / m) s.erase(x);
ans += (x - tmp + m) % m;
change[i] += (x - tmp + m) % m;
}
printf("%lld\n", ans);
for (int i = 1; i <= n; i++) {
printf("%d ", a[i] + change[i]);
}
return 0;
}
CodeForces-999D Equalize the Remainders的更多相关文章
- Codeforces 999D Equalize the Remainders (set使用)
题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...
- CodeForces - 999D Equalize the Remainders (模拟+set)
You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an , and a positive integer mm . ...
- D. Equalize the Remainders (set的基本操作)
D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- D. Equalize the Remainders set的使用+思维
D. Equalize the Remainders set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...
- D. Equalize the Remainders 解析(思維)
Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...
- codeforces 616E Sum of Remainders (数论,找规律)
E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 616E - Sum of Remainders
616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As ...
- codeforces 616E. Sum of Remainders 数学
题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...
- Codeforces 1037C Equalize
原题 题目大意: 给你两个长度都为\(n\)的的\(01\)串\(a,b\),现在你可以对\(a\)串进行如下两种操作: 1.交换位置\(i\)和位置\(j\),代价为\(|i-j|\) 2.反转位置 ...
随机推荐
- Scott Young-《如何高效学习》
1.如果你只用一种方式了解某样事物,那么你就没有真正了解它.事情真正含义的秘密取决于我们如何将其与我们所了解的其他事情相联系.很好联系的内容可使你将想法融于脑中,从各种角度看问题,直至你找到合适自己的 ...
- elasticsearch-dsl聚合-2
接续上篇,本篇介绍elasticsearch聚合查询,使用python库elasticsearch-dsl进行聚合查询操作. 条形图 聚合有一个令人激动的特性就是能够十分容易地将数据转换成图表和图形. ...
- IntelliJ IDEA 方法注释教程
首先Ctrl +Alt +S ,打开Settings ,找到Live Template,然后点击右侧的绿色的“+”,选择Template Group 然后给新建的Group随便命个名 选中自己刚才创建 ...
- ubuntu修改IP地址和网关的方法
一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用vi编辑器(也可以 ...
- python的多继承C3(mro)算法
多继承的继承顺序按照C3算法进行顺序继承 例一 按照深度A类从左往右有三条可继承的"路" 先按照深度优先的算法,将每一路的每一个节点加到列表中 B = [B,D,F,H] C = ...
- 【jQuery】手机验证码倒计时效果
<ul class="ulist"> <li class="group"> <label class="label&qu ...
- 访问远程mysql数据库,出现报错,显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“
在使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to t ...
- CentOS yum命令报错 Error: File /var/cache/yum/i386/6/epel/metalink.xml does not exist
最近在虚拟机上执行yum命令一直报错:Could not parse metalink https://mirrors.fedoraproject.org/metalink?repo=epel-7&a ...
- thymelef模板报错 the entity name must immediately follow the '&' in the entity reference
thymelef模板里面是不能实用&符号的 要用&转义符代替,官网也有文档说明可以用官方的通配符代替,官方文档http://www.thymeleaf.org/doc/tutorial ...
- 带密匙的php加密解密示例分享
<?phpheader("content-type:text/html;charset=utf-8");$id = "http://www.jb51.net&quo ...