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.反转位置 ...
随机推荐
- [Pytorch] pytorch笔记 <三>
pytorch笔记 optimizer.zero_grad() 将梯度变为0,用于每个batch最开始,因为梯度在不同batch之间不是累加的,所以必须在每个batch开始的时候初始化累计梯度,重置为 ...
- 2018.8.18 servlet使用的会话跟踪除session外还有哪些方式
解释HTTP HTTP是一种无连接的协议,如果一个客户端只是单纯地请求一个文件(HTML或GIF),服务器端可以响应给客户端,并不需要知道一连串的请求是否来自于相同的客户端,而且也不需要担心客户端是否 ...
- 2017.11.21 基于JSP+Servlet+JavaBean实现复数运算(二)
代码的实现 最基本的MVC模式 //input.jsp 输入界面 <%@ page language="java" import="java.util.*" ...
- Mybatis-generator自动生成
第一步:导入架包 <build> <plugins> <plugin> <groupId>org.mybatis.generator</group ...
- centos 6.5 配置nginx环境
1.卸载系统中默认的php和httpd [root@x ~]# yum remove httpd* php* Loaded plugins: fastestmirror Setting up Remo ...
- java mysql多次事务 模拟依据汇率转账,并存储转账信息 分层完成 dao层 service 层 client层 连接池使用C3p0 写入库使用DBUtils
Jar包使用,及层的划分 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3 ...
- C#如何使用异步编程【BeginInvoke/EndInvoke】
怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...
- OCCI的迭代修改
传统的在执行多行DML(INSERT.UPDATE.DELETE)时,我们是多次调用executeUpdate():注意!当我们调用一次此函数时,则执行一次网络往返,当数据量大时则效率非常低.不过 O ...
- mysql 自增主键为什么不是连续的?
由于自增主键可以让主键索引尽量地保持递增顺序插入,避免了页分裂,因此索引更紧凑 MyISAM 引擎的自增值保存在数据文件中 nnoDB 引擎的自增值,其实是保存在了内存里,并且到了 MySQL 8.0 ...
- spring-bean(xml方式管理)
特点 每一次加载XML文件时候,都会将配置文件中包含的配置实例化. ID与name区别:name不是唯一的,但是可以使用特殊字符 Class:生成类的实例 Bean的作用域: 三种实例化方式 类的构造 ...