题目连接: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. Android事件总线(一)EventBus3.0用法全解析

    前言 EventBus是一款针对Android优化的发布/订阅事件总线.简化了应用程序内各组件间.组件与后台线程间的通信.优点是开销小,代码更优雅,以及将发送者和接收者解耦.如果Activity和Ac ...

  2. web工程设计<mysql数据模型-数据类型的优化>

    Schema与数据类型优化 良好的逻辑设计和物理设计是高性能的基石,应该根据系统将要执行的查询语句来设计schema,这往往需要权衡各种因素. 一:选择优化的数据类型 ①:更小的通常更好 整数类型:M ...

  3. sql语句进阶教程

    转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...

  4. Install Google Chrome on Fedora 28/27, CentOS/RHEL 7.5 (在 fedora 28 等 上 安装 chrome)

    今天在使用 fedora 安装 chrome 的时候遇到了问题,今天进行将安装过程进行记录下来.需要安装第三方软件仓库. 我们需要进行安装 fedora-workstation-repositorie ...

  5. Head First Android --- Enable USB debugging on your device

    1. Enable USB debugging on your device    On your device, open “Developer options” (in Android 4.0 o ...

  6. Shell脚本应用(for、while循环语句和case分支语句)

    1.for:读取不同的变量值,逐个执行同一组命令,直到取值完毕退出,变量值以空格分隔 语法: for  变量值  in  取值列表 do 命令序列 done 2.while:重复测试某个条件,成立则执 ...

  7. 开启远程Windows系统3389端口

    1.Win7.Win2003.XP系统REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDen ...

  8. Beta冲刺(3/5)(麻瓜制造者)

    今日已完成 邓弘立:完成了登录功能的重构,完成了部分商品管理功能 符天愉:利用ci开始写队友写好的管理员界面,由于后台独立开始使用一个仓库,所以晚上将alpha的版本更新到了git,并且添加了.git ...

  9. <20190102>收录些比较低级错误导致的主板故障现象

    今天收录俩个比较低级的错误. 故障现象:   水冷排风扇高速运转, 并无法调控. 现在CPU散热的水冷排都设计了三条线,   温控4Pin , 水泵线 3Pin  , 接在机箱上USB口取电的灯线或者 ...

  10. JavaScript的运行机制

    先来看一段代码然后再来详细的说明js的运行机制,下面的一段代码执行顺序是什么 console.log(1); setTimeout(function () { console.log(2); }, 0 ...