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 ...
随机推荐
- git配置用户名
git config --global user.name [name] git config --global user.email [email_address]
- SpringMVC中利用HandlerExceptionResolver完成异常处理
在解决Controller层中的异常问题时,如果针对每个异常处理相对较为繁琐.在SpringMVC中提供了HandlerExceptionResolver用于处理捕获到的异常,从而重新定义返回给前端的 ...
- Hadoop学习笔记(2)-HDFS的基本操作(Shell命令)
在这里我给大家继续分享一些关于HDFS分布式文件的经验哈,其中包括一些hdfs的基本的shell命令的操作,再加上hdfs java程序设计.在前面我已经写了关于如何去搭建hadoop这样一个大数据平 ...
- A 蚂蚁觅食
A. 蚂蚁觅食(一) 单点时限: 1.0 sec 内存限制: 512 MB 一只饥饿的小蚂蚁外出觅食,幸运的小蚂蚁发现了好多食物,但是它只有一次搬食物的机会.可因为力气太小了,它不能搬走重量超过自己体 ...
- [转]PHP利用PCRE回溯次数限制绕过某些安全限制
这次Code-Breaking Puzzles中我出了一道看似很简单的题目pcrewaf,将其代码简化如下: <?php function is_php($data){ return preg_ ...
- HuggingFace-transformers系列的介绍以及在下游任务中的使用
内容介绍 这篇博客主要面向对Bert系列在Pytorch上应用感兴趣的同学,将涵盖的主要内容是:Bert系列有关的论文,Huggingface的实现,以及如何在不同下游任务中使用预训练模型. 看过这篇 ...
- C#集合类型——Hashtable、Dictionary之浅谈
Hashtable表 数组.数组集合.List集合都是通过索引来访问里面成员.哈希表则是通过键来访问成员值.键不可为空,值可为空. 比如: Hashtable hash=new Hashtable ...
- 可以用 Python 编程语言做哪些神奇好玩的事情?除了生孩子不能,其他全都行!
坦克大战 源自于一个用Python写各种小游戏的github合集,star数1k.除了坦克大战外,还包含滑雪者.皮卡丘GOGO.贪吃蛇.推箱子.拼图等游戏. 图片转铅笔画 帮助你快速生成属于自己的铅笔 ...
- php json接口demo
<?php class Student { public $no; public $username; public $password; } $student=new Student(); $ ...
- zookeeper笔记(二)
title: zookeeper笔记(二) zookeeper ALC权限控制 getAcl path 可以查看某个node的权限 设置权限: 2. world方式 setAcl <path&g ...