D. Equalize the Remainders (set的基本操作)
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 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.
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个数 你可以对这些数 + 1 操作
所有数对n取模后1->m-1 每一个数都出现n/m次
求最少的操作次数
你第一次扫一遍 看看比 n/m大的 放入set里面 因为应该对超过了n/m的进行操作
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + ;
const int INF = 0x7fffffff;
LL n, m, num, ans, a[maxn], b[maxn]; int main() {
scanf("%lld%lld", &n, &m);
for (int i = ; i < n ; i++) {
scanf("%lld", &a[i]);
b[a[i] % m]++;
}
set<LL>st;
ans = , num = n / m;
for (int i = ; i < m ; i++)
if (b[i] < num) st.insert(i);
for (int i = ; i < n ; i++) {
if (b[a[i] % m] <= num) continue;
b[a[i] % m]--;
set<LL>::iterator it;
it = st.lower_bound(a[i] % m);
if (it != st.end()) {
ans += *it - a[i] % m;
a[i] += *it - a[i] % m;
b[*it]++;
if (b[*it] == num) st.erase(it);
} else {
LL temp = m - a[i] % m;
ans += *st.begin() + temp;
a[i] += *st.begin() + temp;
b[*st.begin()]++;
if (b[*st.begin()] == num) st.erase(st.begin());
}
}
printf("%lld\n", ans);
for (int i = ; i < n ; i++)
printf("%lld ", a[i]);
printf("\n");
return ;
}
D. 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 ...
- 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; ...
- CoderForces999D-Equalize the Remainders
D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- 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 ...
随机推荐
- R语言学习笔记(十六):构建分割点函数
选取预测概率的分割点 cutoff<- function(n,p){ pp<-1 i<-0 while (pp>=0.02) { model.predfu<-rep(&q ...
- 2019年第十届蓝桥杯C/C++程序设计本科B组省赛 E迷宫
试题 E: 迷宫 本题总分: 分 [问题描述] 下图给出了一个迷宫的平面图,其中标记为 的为障碍,标记为 的为可 以通行的地方. 迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它 ...
- 39-Role以及Claims授权
asp.net core多鼓励使用claims授权 1-使用role授权 在类或方法上贴上Roles,这样就知道有user的角色才可以访问 [Authorize(Roles="user&qu ...
- Angularjs 跨域post数据到springmvc
先贴网上己有解决方案链接: http://www.tuicool.com/articles/umymmqY (讲的是springmvc怎么做才可以跨域) http://my.oschina.net/ ...
- luogu4172 [WC2006]水管局长
就是用 lct 维护最小生成树 ref #include <algorithm> #include <iostream> #include <cstdio> #in ...
- js字符编码笔记
一. 什么是unicode? ascii码能表示的字符非常有限(128个字符),这对英文来说足够了,但是对法文.中文.土耳奇文等文字则远远不够,于是就产生了新的编码规则-unicode,unicod ...
- Spring框架中ModelAndView、Model、ModelMap的区别
转自:http://blog.csdn.net/liujiakunit/article/details/51733211 1. Model Model 是一个接口, 其实现类为ExtendedMode ...
- 生产者与消费者-N:1-基于list
多个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...
- react实现页面切换动画效果
一.前情概要 注:(我使用的路由是react-router4) 如下图所示,我们需要在页面切换时有一个过渡效果,这样就不会使页面切换显得生硬,用户体验大大提升: but the 问题是 ...
- Prometheus 普罗米修斯监控
周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下 这里只介绍客户端的配置 1:首先在POM中添加依赖 <dependency> <groupI ...