题目连接: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] 设置AlertDialog中按钮的可用(Enable)状态

    弹出一个保存文件的对话框,要控制输入内容限制,同时内容为空时保存按钮不可用. 原文地址请保留http://www.cnblogs.com/rossoneri/p/4140184.html 直接上代码: ...

  2. LeetCode题解Maximum Binary Tree

    1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...

  3. C#重试公用类

    //Retry机制 public static class RetryExecutor { /// <summary> /// 重试零个参数无返回值的方法 /// </summary ...

  4. EF 更新部分字段写法

    EF 更新部分字段写法 1.EF默认是查询出来,修改后保存: 2.设置不修改字段的IsModified为false,此方法不需要先从数据库查询出实体来(最优方法): db.Set<T>() ...

  5. 转:更改SQLServer实例默认字符集

    需求 安装数据库时,将字符集安装成了“SQL_Latin1_General_CP1_CI_AS”,现在需要将其更改为“Chinese_PRC_CI_AS”.   方法 重新生成系统数据库 ,然后还原配 ...

  6. git版本控制工具基本用法讲解(转)

    一.安装Git 在linux系统使用非常方便,只需要打开shell界面,并输入: ? 1 sudo apt-get install git-core 按下回车后输入密码,即可完成Git的安装.但我们可 ...

  7. WeakHashMap源码解读

    1. 简介 本文基于JDK8u111的源码分析WeakHashMap的一些主要方法的实现. 2. 数据结构 就数据结构来说WeakHashMap与HashMap原理差不多,都是拉链法来解决哈希冲突. ...

  8. github拓展,以及ModelForm的使用

    github - git  init/add/commit/reset/log/status/stash pop/checkout/branch    新入职到公司,地址:   git clone h ...

  9. centos7下安装docker(12.3容器之间的连通性)

    我们接着盗图,如下: 在这张图上,可以看到,如果两个容器使用同一个bridge,那么两个容器之间是互相能通的 可以看到两个容器在同一个bridge下是可以互相ping通的 当两个容器在不同的bridg ...

  10. 采用spring的schedule注解配置定时任务

    1 在springmvc配置文件中新增以下配置 <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 --> <task:scheduled-tasks s ...