题目连接:Equalize the Remainders

题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1。输出最少操作次数,和操作后的序列(可以输出任意一种)。

题解:用一个set来维护所有余数x(当前余数为x的数个数没凑够n/m个),对于每个数假设这个数的余数为t,当余数为t的数个数没凑够n/m时那这个数就不需要改变,如果已经凑够了,那就在set中找到第一个大于等于t的数(注意这里t可能比set中最大数的还要大,遇到这种情况就要将t变成set中最小数,举个例子m=5,余数为4和为0的数字凑够了,此时又来一个余数为4的数,该数应该变为余数为1)维护答案和序列,ans += (x-t+m)%m,out[i] += (x-t+m)%m。


 #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int MAX_N =2e5+;
int N,M,T,S;
LL vec[MAX_N];
LL res[MAX_N]; // 当前模的个数
set<int> s;
LL out[MAX_N];
int main()
{
while(cin>>N>>M){
memset(res,,sizeof(res));
memset(out,,sizeof(out));
s.clear();
for(int i=;i<M;i++){
s.insert(i);
}
for(int i=;i<N;i++){
scanf("%lld",&vec[i]);
}
LL ans = ;
for(int i=;i<N;i++){
LL t = vec[i]%M;
LL x ;
if(t > *s.rbegin()) x = *s.begin();
else x = *s.lower_bound(t);
res[x] ++;
if(res[x] == N/M) s.erase(x);
ans += (x - t + M)%M;
out[i] = (x - t + M)%M;
}
cout<<ans<<endl;
for(int i=;i<N;i++){
printf("%lld ",vec[i] + out[i]);
}
cout<<endl;
}
return ;
}

Codeforces 999D Equalize the Remainders (set使用)的更多相关文章

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

  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 616E Sum of Remainders (数论,找规律)

    E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces 616E - Sum of Remainders

    616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As ...

  7. codeforces 616E. Sum of Remainders 数学

    题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...

  8. Codeforces 1037C Equalize

    原题 题目大意: 给你两个长度都为\(n\)的的\(01\)串\(a,b\),现在你可以对\(a\)串进行如下两种操作: 1.交换位置\(i\)和位置\(j\),代价为\(|i-j|\) 2.反转位置 ...

  9. CodeForces-999D Equalize the Remainders

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

随机推荐

  1. python第四十二天 socket ---ssh

    用scoket 写一个简版的ssh 服务端: #!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan import socket,os s= ...

  2. NAudio音频文件转换

    1.NuGet安装 NAudio,项目及demo的网址:https://github.com/naudio/NAudio Encode to MP3, WMA and AAC with MediaFo ...

  3. HTML 5 <input> list 属性

    定义和用法 list 属性引用数据列表,其中包含输入字段的预定义选项. 可以用来做关联搜素

  4. 开发测试技巧|辅助开发调试:goolge浏览器利用F12在控制台输入脚本实现表单自动填充

    一个开发测试技巧的指引和截图,利用google浏览器的F12调试和Console执行,注入JavaScript脚本实现表单的自动填充和测试. 原文链接: http://www.lookdaima.co ...

  5. 路由交换01-----ICMP协议

    路由交换协议------ICMP ICMP协议 ICMP (InternetControl Message Protocol)协议是TCP/IP协议簇的核心协议之一,用来在网络设备之间传递各种差错和控 ...

  6. pandas的to_csv()使用方法

    1.首先查询当前的工作路径: import osos.getcwd() #获取当前工作路径2.to_csv()是DataFrame类的方法,read_csv()是pandas的方法dt.to_csv( ...

  7. swift的@objc总结

    One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C. @objc相 ...

  8. Android事件处理第一节(View对Touch事件的处理)

    http://ipjmc.iteye.com/blog/1694146 在Android里Touch是很常用的事件,尤其实在自定义控件中,要实现一些动态的效果,往往要对Touch进行处理.Androi ...

  9. C/C++每日小练(七)——墓地雕塑

    墓地雕塑 题目描写叙述: 在一个周长为10000的圆上等距分布着n个雕塑. 如今又有m个新雕塑增加(位置能够任意放).希望全部n+m个雕塑在圆周上均匀分布.这就须要移动当中一些原有的雕塑.要求n个雕塑 ...

  10. Excel中concatenate函数的使用方法

    你还在为Excel中concatenate函数的使用方法而苦恼吗,今天小编教你Excel中concatenate函数的使用方法,让你告别Excel中concatenate函数的使用方法的烦恼. 经验主 ...