D. Equalize the Remainders
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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 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.

Examples
input
Copy
6 3
3 2 0 6 10 12
output
Copy
3
3 2 0 7 10 14
input
Copy
4 2
0 1 2 3
output
Copy
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的更多相关文章

  1. Codeforces 999D Equalize the Remainders (set使用)

    题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...

  2. D. Equalize the Remainders (set的基本操作)

    D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  3. D. Equalize the Remainders set的使用+思维

    D. Equalize the Remainders set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...

  4. D. Equalize the Remainders 解析(思維)

    Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...

  5. 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 . ...

  6. CodeForces-999D Equalize the Remainders

    题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...

  7. CodeForces-999D Equalize the Remainders (贪心+神奇的STL)

    题意:给你一个n,m;其中n一定能被m整除,然后给你n个数 有一种操作   选择n个数中的任意一个,使其+1: 条件: Ci 属于[0,m-1]  Ci代表ai模m的余数为i的个数 且都等于n/m; ...

  8. Codeforces Round #490 (Div. 3)

    感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...

  9. [Codeforces]Codeforces Round #490 (Div. 3)

    Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...

随机推荐

  1. Asciinema:你的所有操作都将被录制

    如何实现类似于Jumpserver koko一样的终端录制回放功能呢?本文介绍一个神器 asciinema 是一款开源免费的终端录制工具,它可以将命令行输入输出的任何内容加上时间保存在文件中,同时还提 ...

  2. Matlab 文件格式化/Matlab Source File Formator

    由于需要使用到别人编写的Matlab代码文件,但是呢不同的人有不同的风格,有的写得就比较糟糕了. 为了更好地理解代码的内容,一个比较美观的代码会让人身心愉悦. 但是在网上并没有找到一个比较好的实现,此 ...

  3. 微擎 pdo_fetchall() 函数

    微擎 pdo_fetchall() 函数 注意点: 该函数内部直接执行原生 SQL 语句 如果在传递表名的时候使用了 tablename .则不加 ims_ 前缀 参数的传递通过 :param 的形式 ...

  4. Arduino 配置 ESP8266环境

    Arduino 配置 ESP8266环境 将 http://arduino.esp8266.com/stable/package_esp8266com_index.json 添加到 [附加开发板管理器 ...

  5. Tarjan-割点

    割点——tarjan #include <bits/stdc++.h> using namespace std; ; ; int n, m; int ans;//个数 * MAXM], n ...

  6. vux组件的全局注册引入

    安装好vux后,要引入全局组件是要在main.js中使用Vue.component引入的,不能直接使用Vue.use,不能直接使用Vue.use,不能直接使用Vue.use import router ...

  7. config system setting for elasticsearch

    1. disable swapping. swap 分区会导致garbage collections 花费太多时间,elasticsearch 响应变慢. 方法1:swapoff -a 方法2:vi ...

  8. Flex容器拖动(Bordercontainer为例)

    Bordercontainer的拖放到任意位置. mxml: 为Bordercontainer添加鼠标按下和弹起事件 <s:BorderContainer id="bdShow&quo ...

  9. (二)OpenStack---M版---双节点搭建---数据库安装和配置

    ↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 本章节只在Controller节点执行 1.安装Mariadb数据库来存储信息 2.NoSQL数 ...

  10. Linux下为知笔记和蚂蚁笔记测评,推荐蚂蚁笔记!(非广告)

    本人由于学习Linux,需要一款可以在Linux平台下可以运行的一款软件,了解到为知笔记之笔记(下文以W代替)和蚂蚁笔记(下文以M代替)比较出名,由于某云和某象笔记在linux平台下没有对应的软件,所 ...