D. Equalize the Remainders

set的学习::https://blog.csdn.net/byn12345/article/details/79523516

注意set的end()和rbegin()的区别。

end()是指向最后一个元素的下一个,rbegin()是指向最后一个元素。

题目大意:给你一个n长度的数组,给一个模数m,问对m取模,余数从0到m-1的每一种都是n/m 保证m一定是n的除数。

每一个操作对数字+1,问最少的操作满足题目,输出操作之后的数组。

这个用set模拟比较好写。

首先将0到m-1的每一个数都放到set里面,然后遍历这个数组,对于每一个数,如果这个数的这个余数已经有了n/m

贪心的考虑这个数首先是加的越少越好。

如果这个数的余数已经没有比它更大的数存在了,那么就应该选最小还需要的约数加上去,也就是这个set的头部,

因为set是排过序的,头部是最小的数字,

如果还存在比这个数的余数更大的,那就贪心的加在大于等于这个数的最小的数。

然后就可以求出贡献,也可以求出这个数要加上去的结果。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#define fir first
#define sec second
using namespace std;
typedef long long ll;
const int maxn=2e5+;
ll a[maxn],val[maxn];
int res[maxn];
set<int>s; int main(){
int n,m;
s.clear();
scanf("%d%d",&n,&m);
for(int i=;i<m;i++) s.insert(i);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
ll ans=;
memset(res,,sizeof(res));
for(int i=;i<=n;i++){
ll t=a[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;
val[i]=(x-t+m)%m;
}
printf("%lld\n",ans);
for(int i=;i<=n;i++) printf("%lld ",a[i]+val[i]);
printf("\n");
return ;
}

D. Equalize the Remainders set的使用+思维的更多相关文章

  1. Codeforces 999D Equalize the Remainders (set使用)

    题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...

  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 解析(思維)

    Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...

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

  5. CodeForces-999D Equalize the Remainders

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

  6. CodeForces-999D Equalize the Remainders (贪心+神奇的STL)

    题意:给你一个n,m;其中n一定能被m整除,然后给你n个数 有一种操作   选择n个数中的任意一个,使其+1: 条件: Ci 属于[0,m-1]  Ci代表ai模m的余数为i的个数 且都等于n/m; ...

  7. CoderForces999D-Equalize the Remainders

    D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  8. Codeforces Round #490 (Div. 3)

    感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...

  9. [Codeforces]Codeforces Round #490 (Div. 3)

    Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...

随机推荐

  1. C与C++的函数声明中省略参数的不同意义

    一直都以为C/C++中形如 int func(); 这样的函数声明其意义就是一个参数 void(没有参数)的函数.然而今天在看C++的时候突然看到这么一句: 对于带空参数表的函数,C和C++有很大的不 ...

  2. js数组的遍历(API)

    1.for 循环 普通遍历方法,可优化,存下数组的length,避免每次都去获取数组的length,性能提升 for(var i=0;i<arr.length;i++){ console.log ...

  3. 从CentOS 7.0升级到7.7版本

    我平时都在VirtualBox上装虚拟机,方法是导入ova镜像文件,比如导入从网上下载的CentOS7-base.ova,该ova为CentOS 7.0版本,而现在最新版本为7.7,为此进入系统后第一 ...

  4. Spring Cloud 系列之 Gateway 服务网关(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Gateway 服务网关(一) 本篇文章讲解 Gateway 网关的多种路由规则.动态路由规则(配合服务发现的路由规则 ...

  5. F - Dragon Balls

    Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficu ...

  6. JavaScript数据类型 —— 基础语法(2)

    JavaScript基础语法(2) 数据类型 js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Undefined,Null),和一种复杂数据类型(Object) ...

  7. mac上搭建mysql环境配置和Navicat连接mysql

    mac上搭建mysql环境配置 1.下载mysql for mac: https://downloads.mysql.com/archives/community/ 注意:mysql版本要和你的MAC ...

  8. 高德局部刷新标记点,bug解决

    将接口返回的经纬集合点在高德地图上标记展示, 如果实时刷新地图标记点,不加优化,则会造成过多的带宽消耗 所以,地图只需加载一次,局部更新标记点就好了 代码: <template> < ...

  9. mac、window版编辑器 webstorm 2016... 永久破解方法。

    第一步:从官网下载最新版本的webstorm编辑器(建议在官网下载,防止第三方插件恶意攻击!): 下载链接  http://www.jetbrains.com/webstorm/  , 点击 DOWN ...

  10. nignx location index的用法

    来源:https://blog.csdn.net/qq_32331073/article/details/81945134#_10 index指令的作用 在前后端分离的基础上,通过Nginx配置,指定 ...