zoukankan      html  css  js  c++  java
  • 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;
    }






  • 相关阅读:
    python35 1.守护进程 2.互斥锁3.IPC 4.生产者和消费者模型
    python34 多进程: 1.进程与程序的区别 2.阻塞 非阻塞 并行 并发(重点) 3.进程三种方式的切换 4.程序员永恒的话题 5.进程的创建以及销毁 6.进程的两种使用方式(重点) 7.join函数(重点) 8.process常用属性 9僵尸与孤儿进程(了解)
    Python33 1.UDP协议 2.与TCP区别 3.DNS服务器 4.进程----进入并发编程的学习 5.操作系统 6.多道技术
    Mybatis 属性配置
    SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession
    MyBatis 简单入门
    spring mvc 异步 DeferredResult
    Spring MVC 异步请求 Callable
    Servlet 异步处理
    @EnableWebMvc
  • 原文地址:https://www.cnblogs.com/csushl/p/9386536.html
Copyright © 2011-2022 走看看