CoderForces999D-Equalize the Remainders
3 seconds
256 megabytes
standard input
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.
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.
In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from 00to 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.
6 3
3 2 0 6 10 12
3
3 2 0 7 10 14
4 2
0 1 2 3
0
0 1 2 3
题意:本题就是给你n,m,保证n能被m整除,给你n个数,对这些数操作+=1,使得这些数%m后,得到的数是从0~m-1,且没个数出现n/m次。
题解:贪心,对于数量少的先不处理,对于多于n/m的数使其变为离他最近的数量不到n/m的数,记录需要操作的次数,跑一边就可以得到结果;
AC代码为:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
const int INF=0x3f3f3f3f;
typedef long long LL;
vector<int> v[maxn];
int a[maxn],n,m;
LL ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;ans=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
v[a[i]%m].push_back(i);
}
int temp=0,flag=n/m;
for(int i=0;i<m;i++)
{
while(v[i].size() > flag)
{
temp=max(temp,i);
while(v[temp%m].size()>=flag) temp++;
int num=min(flag-v[temp%m].size(),v[i].size()-flag);
int c_num=temp-i;
while(num--)
{
ans+=c_num;
a[v[i].back()]+=c_num;
v[temp%m].push_back(v[i].back());
v[i].pop_back();
}
}
}
cout<<ans<<endl;
for(int i=0;i<n;i++) i==n-1? cout<<a[i]<<endl : cout<<a[i]<<" ";
return 0;
}
CoderForces999D-Equalize the Remainders的更多相关文章
- Codeforces 999D Equalize the Remainders (set使用)
题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...
- 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 - 999D Equalize the Remainders (模拟+set)
You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an , and a positive integer mm . ...
- CodeForces-999D Equalize the Remainders
题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...
- CodeForces-999D Equalize the Remainders (贪心+神奇的STL)
题意:给你一个n,m;其中n一定能被m整除,然后给你n个数 有一种操作 选择n个数中的任意一个,使其+1: 条件: Ci 属于[0,m-1] Ci代表ai模m的余数为i的个数 且都等于n/m; ...
- Codeforces Round #490 (Div. 3)
感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...
- [Codeforces]Codeforces Round #490 (Div. 3)
Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...
随机推荐
- RabbitMQ 的高可用集群
RabbitMQ 的高可用性 RabbitMQ 是比较有代表性的,因为是基于主从(非分布式)做高可用的 RabbitMQ 有三种模式:单机模式.普通集群模式.镜像集群模式. 单机模式 单机模式,生产几 ...
- [Xilinx笔记] 《FPGA伴你玩转USB3.0与LVDS》 读书笔记
2019年10月15日09:31:10 第一部分·哪些要看(优先级由高到低) 0.8.1 LVDS数据收发实例 8.2 带CRC校验的LVDS数据收发实例 1.3.6 USB3.0 控制器 ...
- 关于RAID 5的介绍与创建
一.简介 定义: RAID 5是RAID 0和RAID 1的折中方案.RAID 5具有和RAID0相近似的数据读取速度,只是多了一个奇偶校验信息,写入数据的速度比对单个磁盘进行写入操作稍慢.同时由于多 ...
- python学习基础—day01
一. python是什么? 优势:简单, 可以跨平台 劣势:执行效率没有C语言那么高 python是解释型语言,逐行编译解释,在不同的系统windows与Linux,需要不同的解释器来编译. 而编译型 ...
- boost.asio新框架的设计概念总结
1.66版本,boost.asio库重新设计了框架,目前最新版为1.71.读了几天代码后,对框架中相关概念总结.因为是泛型编程的库,所以分析的概念层的设计. 可通过boost官方文档,strand的1 ...
- IDEA最常用快捷键汇总+快速写出Main函数
IDEA可以说是当下Java程序员日常开发的神器,但是想要发挥这款神器的牛逼威力,必须得熟练使用它的各种快捷键才行.本篇总结下使用IDEA(也就是IntelliJ IDEA )进行日常开发中最常用的快 ...
- Mysql 添加字段 修改字段 删除字段
1.添加字段(alter.add) mysql> alter table users add name varchar(30) not null after id; 2.修改字段(alter.m ...
- scss--函数 (Functions)--unitless
(Sass::Script::Value::Bool) unitless($number) Returns whether a number has units. Examples: unitless ...
- 【译】为什么永远都不要使用MongoDB Why You Should Never Use MongoDB
背景 最近在学习DDIA(Designing Data-Intensive Applications)这本分布式领域非常急经典的入门书籍,里面第二章<数据模型与查询语言>,强调了对一对多. ...
- 痞子衡嵌入式:恩智浦机器视觉模块OpenMV-RT那些事(1)- 初体验
大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是机器视觉模块OpenMV-RT初体验. 近些年机器视觉应用一直是个很火的方向,想象一下机器如果能长上"眼睛",是不 ...