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 . 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
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个数
要求这n个数分为m组,组内n/m个数,要求第一组数%m==0,第二组数%m==1
第m组数%m==m-1,对于不满足要求的数,每次可以加1,问你最少的操作次数和所有操作完成之和的n个数
拿样例来说吧
3 2 0 6 10 12
n==6,m==3
6个数,分为3组,每组两个
第一组%3==0
第二组%3==1
第三组%3==2
3和0一组,%3==0
7(6+1=7)和10一组 %3==1
2和14(12+2=14)一组 %3==2
最少操作次数3次
操作之后的数组:
3 2 0 7 10 14 做法:
利用set把可能的余数0到m-1存起来(都是%m嘛),这个叫做目标余数
然后从原数组第一个开始遍历
x=a[1]%m,x叫做原余数
找到和原余数最近的没有用过的目标余数(在set里面找)
原余数可能会大于目标余数(目标余数只能出现n/m次,因为一组只能由n/m个数,达到n/m次数的目标余数就会删除)
那么这时候目标余数就是取set里面第一个 取第一个的原因:对数组里面的数只能执行加的操作,比如现在原余数是2,但是set里面最大的目标余数都只有1(目标余数2出现了n/m次,被删了),因为只有加的操作,由要使得
原余数2变成set里面有的最近的目标余数,所以就取set里面第一个,比如5%3==2,2是原余数,找到最近的目标余数这样进行操作的次数就会最少,如果目标余数里面还有2的话,就不要进行加操作了,直接取目标余数就好了,但是现在目标余数没有2了,最大只有1
,所以我们目标余数取0(如果0还有的话,反正取set里面第一个(set默认升序)),取第一个的概念:有点环的概念 取过的目标余数就标记一下,满足了出现n/m次就从set里面删除
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define max_v 200005
int n,m;
LL a[max_v];
LL c[max_v];
LL sum;
int main()
{
while(~scanf("%d %d",&n,&m))
{
set<int> s;
set<int>::iterator it;
memset(c,,sizeof(c));
sum=; for(int i=;i<m;i++)
s.insert(i); for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
int x=a[i]%m;
int y;
if(x>*s.rbegin())//s中删除某个后,可能余数会大于s里面最大的,那么余数肯定选择s里面第一个
y=*s.begin();
else
y=*s.lower_bound(x);//大于等于x的第一个
c[y]++;//标记位加1
if(c[y]==n/m)//该余数用过了n/m次
s.erase(y);//删除用过了n/m次的余数
a[i]+=((y-x)+m)%m;
sum+=((y-x)+m)%m;
}
printf("%lld\n",sum);
for(int i=;i<=n;i++)
printf("%lld ",a[i]);
printf("\n");
}
return ;
}
CodeForces - 999D Equalize the Remainders (模拟+set)的更多相关文章
- 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 set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- 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 解析(思維)
Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...
- Codeforces 747C:Servers(模拟)
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
- Codeforces 740A. Alyona and copybooks 模拟
A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...
- Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces 670 A. Holidays(模拟)
Description On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Ma ...
随机推荐
- K:hash(哈希)碰撞攻击
相关介绍: 哈希表是一种查找效率极高的数据结构,很多语言都在内部实现了哈希表.理想情况下哈希表插入和查找操作的时间复杂度均为O(1),任何一个数据项可以在一个与哈希表长度无关的时间内计算出一个哈希值 ...
- windows操作系统用命令提示符查看占用端口号的进程
在开发中有时我们需要确定哪个占用了8080端口,在windows命令行窗口下执行: 命令执行后打印出来的结果如下所示:
- 微服务架构之spring cloud hystrix&hystrix dashboard
在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无 ...
- Install Java JDK JRE on Ubuntu/Debian with Apt-Get
Introduction As a lot of articles and programs require to have Java installed, this article will gui ...
- 安卓app开发-03-项目的基本开发步骤
android项目的基本开发步骤 这里分享一下开发 安卓 app 的流程,当然有些感觉不必要,其实不然,前期工作也是极为重要的额,就像开发的时候如果目标不对的话,到后期后很迷的,所以一定要提前做好规划 ...
- 2 pygraphviz在windows10 64位下的安装问题(反斜杠的血案)
可以负责任的说,这篇文档是windows10安装pygraphviz中,在中文技术网站中最新的文档,没有之一.是自己完全结合各种问题,包括调试等,总结出来的. 问题来源:主要是可视化RvNN网络的树结 ...
- Android实时获得经纬度,发送给c++服务端
Android 客户端: package com.example.admin.gpsget; import android.Manifest; import android.content.Conte ...
- elasticsearch集群搭建报错: not enough master nodes discovered during pinging
自己用一台 阿里云 服务器 搭建ES集群的时候,总是报上面的问题. 而且两个ES服务都是报同样的问题.自己的配置文件如下: es服务1配置文件 cluster.name: elasticsearch ...
- CentOS随笔 - 1.虚拟机VMware安装CentOS7系统
前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 需要安装CentOS首先你得下载安装镜像文件(地址: https://www.centos.org/downl ...
- Windows ->> Windows下安装MSI程序遇到2503和2502错误
三个步骤可以解决这个问题: 1) 以管理员身份开启命令行模式并键入msiexec /package <msi文件路径> 2) 修改组策略 计算机配置 ->> 管理模板 -> ...