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 ...
随机推荐
- 实验:基于http的yum源
实验:基于http的yum源 selinux,firewalld已经关闭',系统为CentOS7 repodata所在的目录就是yum源 下面介绍了如何把本地光盘通过httpd服务器变成yum源:多个 ...
- CSS如何修改tr边框属性
有很多时候,我们都要自定义为表格合并边框,这个只要 table{ border-collapse:collapse; } 就可以了 参数: separate 默认值.边框会被分开.不会忽略border ...
- 宋宝华:Linux设备驱动框架里的设计模式之——模板方法(Template Method)
本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 宋宝华 来源: 微信公众号linux阅码场(id: linuxdev) 前言 <设计模式>这本经典 ...
- 领扣(LeetCode)二维区域和检索 个人题解
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
- Opencv的线性滤波和非线性滤波
线性滤波 :方框滤波 均值滤波 高斯滤波 非线性滤波: 中值滤波 双边滤波 这几个滤波都是起模糊作用 去除噪点 不废话了 下面是代码 #include <opencv2/opencv.h ...
- spring security进阶 使用数据库中的账户和密码认证
目录 spring security 使用数据库中的账户和密码认证 一.原理分析 二.代码实现 1.新建一个javaWeb工程 2.用户认证的实现 3.测试 三.总结 spring security ...
- RevitAPI 隐藏UI读取Revit文件
1.1. 新建一个控制台项目 1.2. 添加Revit API引用 我们找到revit安装目录下的这两个DLL添加到项目引用中 RevitNET.dll RevitAPI.dll 修改属性:复制本地: ...
- java快速复习 一 基础语法
最近看很多算法书,比较不错的有不少都是java语言描述,所以用一天时间快速研究并整理java ,参考资料:java入门经典 Call this file "Example2.java&qu ...
- Java描述设计模式(23):访问者模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 电竞是游戏比赛达到"竞技"层面的体育项目.利用电子设备作为运动器械进行的.人与人之间的智力对抗 ...
- DG中模拟failover故障与恢复
问题描述:情形是当主库真正出现异常之后,才会执行的操作,那么我们执行过failover 之后,如何在重新构建DG,这里我们利用flashback database来重构.模拟前主库要开启闪回区,否则要 ...