题目连接: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. 你不可不知的Java引用类型之——软引用

    定义 软引用是使用SoftReference创建的引用,强度弱于强引用,被其引用的对象在内存不足的时候会被回收,不会产生内存溢出. 说明 软引用,顾名思义就是比较"软"一点的引用. ...

  2. Xamarin是无懈可击还是鸡肋?浅谈对Xamarin的学习

    微软宣布跨平台已经有几个年头,当C#代码可以在其他平台运行时,我相信对于每个热爱.net的程序猿还是十分欣慰的,最近工作需要在一直研究和学习.net的跨平台开发Xamarin,网上对其优点总结也是一大 ...

  3. 第七章 鼠标(CHECKER1)

    CHECKER1程序将客户区划分成25个矩形,构成一个5*5的数组.如果在其中一个矩形内单击鼠标,就用X形填充该矩形.再次单击,则X形消失. /*--------------------------- ...

  4. Python & 机器学习之项目实践

    机器学习是一项经验技能,经验越多越好.在项目建立的过程中,实践是掌握机器学习的最佳手段.在实践过程中,通过实际操作加深对分类和回归问题的每一个步骤的理解,达到学习机器学习的目的. 预测模型项目模板不能 ...

  5. Python scikit-learn (metrics): difference between r2_score and explained_variance_score?

    I noticed that that 'r2_score' and 'explained_variance_score' are both build-in sklearn.metrics meth ...

  6. selenium+Python3.5获取验证码

    其中PIL为Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. PIL第三方库安装 pip install PI ...

  7. "error lnk1158 无法运行rc.exe”解决方案

    最近使用VS2012编译时,出现" error lnk1158 无法运行rc.exe”的问题,无法编译生成.exe文件,连最基本的HelloWorld控制台程序都无法运行,重置了VS的默认设 ...

  8. 关于elk中filebeat定义好日志输出,但是redis里面却没有输出内容的问题

    这两天在搞elk的时候,filebeat中指定输出日志至Broker(此处Broker采用redis作为缓存),但是redis中却没有内容,所以就开始排查来 filebeat采用RPM安装的方式来的. ...

  9. Alpha冲刺! Day7 - 砍柴

    Alpha冲刺! Day7 - 砍柴 今日已完成 晨瑶:列了各模块目前的进度情况:确定了纯多媒体流星预览页的显示方式:给工具包函数列表新增了与服务器端的交互:玩华为软件云发现刚好可以试试它的测试,于是 ...

  10. 【Beta Scrum】冲刺! 1/5

    0. Alpha阶段遗留问题 项目 功能/页面 功能/页面 WEB端 图片在线编辑 文件上传跨域问题 app端 作业展示页面 1. Beta计划表 功能 说明 web端 登录 完成web端登录页面及功 ...