D. Equalize the Remainders set的使用+思维
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的使用+思维的更多相关文章
- Codeforces 999D Equalize the Remainders (set使用)
题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...
- D. Equalize the Remainders (set的基本操作)
D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- D. Equalize the Remainders 解析(思維)
Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...
- 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 . ...
- CodeForces-999D Equalize the Remainders
题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...
- CodeForces-999D Equalize the Remainders (贪心+神奇的STL)
题意:给你一个n,m;其中n一定能被m整除,然后给你n个数 有一种操作 选择n个数中的任意一个,使其+1: 条件: Ci 属于[0,m-1] Ci代表ai模m的余数为i的个数 且都等于n/m; ...
- CoderForces999D-Equalize the Remainders
D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #490 (Div. 3)
感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...
- [Codeforces]Codeforces Round #490 (Div. 3)
Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...
随机推荐
- 项目中你不得不知的11个Java第三方类库
项目中你不得不知的11个Java第三方类库 博客分类: Java综合 JavaGoogle框架单元测试Hibernate Java第三方library ecosystem是一个很广阔的范畴.不久前有人 ...
- AJ学IOS 之ipad开发Popover的调色板应用_popover显示后其他控件仍然能进行交互
AJ分享,必须精品 一:效果 后面的是xcode的控制台 二:代码 ViewController #import "ViewController.h" #import " ...
- Linux常用命令02(远程管理)
01 关机/重启 序号 命令 对应英文 作用 01 shutdown 选项 时间 shutdown 关机/重新启动 1.1 shutdown shutdown 命令可以 安全 关闭 或者 重新启动系统 ...
- XFS文件系统的备份与恢复
永久修改主机名:hostnamectl set-hostname oldboy临时修改主机名:hostname xfsdump备份xfsdump -f 备份的文件位置 要备份的分区或者磁盘 免交互备份 ...
- 使用 Chrome 插件 Vimium 打造黑客浏览器
之前一直用 cVim,与 Vimium 功能类似,但是之后不在更新了,故转战到 Vimium. 简介 官网:http://vimium.github.io/ Vimium 是 Google Chrom ...
- linux下DNS服务器搭建,正反向解析配置
dns服务器之前自己搭建玩过,一段时间不搞,加上当时没写文档,基本忘光光了,这次老实了,写个文档记下来,方便以后查阅. 1.服务器准备 为了避免不必要的问题,关闭防火墙,关闭selinux,hosts ...
- windows/linux下如何更换Python的pip源
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:xlixiaohui PS:如有需要Python学习资料的小伙伴可以 ...
- day8作业
# 一:for循环 # 1.1 for循环嵌套之打印99乘法表 for i in range(1,10): for j in range(1,i+1): print("{} * {} = { ...
- css特效sh
1 opacity=0.5: 透明度 2 选择器 .btn1:ho ...
- EF-三种映射
更改实体的类名称,字段名称,来映射表名称,表字段. 1,用EF自带的特性方式: 直接加上特性,更新对应的类名,字段名以及引用类,字段名的相关地方 2,参考NHibernate建立一个EF自带的映射 ...