• CoderForces999D-Equalize the Remainders


    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 (0rm1)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==cm1=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 (1n2105,1mn1≤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 (0ai1090≤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 m1m−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;
    }






  • 相关阅读:
    Ionic Android开发环境搭建 下
    Ionic Android开发环境搭建 上
    百度地图API 简单使用
    json2.js 的使用
    第三回 Bootstrap3.x 起步
    第二回 认识CDN
    WPF 实现的等待效果界面
    AutoFac使用
    SQL语句优化
    MySQL索引的使用
  • 原文地址:https://www.cnblogs.com/csushl/p/9386536.html
Copyright © 2020-2023  润新知