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 ...
随机推荐
- tf.nn.sigmoid_cross_entropy_with_logits 分类
tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,,labels=None,logits=None,name=None) logits和la ...
- 多线程设置flag标志位实现同步
信号灯解决同步问题 我尽量注释了代码,可以很容易理解了. package Thread; /** * 信号灯 * 借助标志位 */ public class FlagThread { public s ...
- unity3d的键盘和鼠标输入
一.键盘的输入 •GetKey,GetKeyDown,GetKeyUp三个方法分别获取用户键盘按键的输入 1. GetKey:用户长按按键有效: bool down = Input.GetKeyDow ...
- Problem L. World Cup
题目大意:有A,B,C,D四个队伍,两两之间有一个比赛,假如A和B比赛,如果平局,各加一分,如果说A胜,给A加3分,不给B加分,B胜同理 给出A,B,C,D,的得分,判断形成这种局面有多少种方式. 思 ...
- D. 蚂蚁平面
D. 蚂蚁平面 单点时限: 2.0 sec 内存限制: 512 MB 平面上有 n只蚂蚁,它走过的路径可以看作一条直线 由这n 条直线定义的某些区域是无界的,而另一些区域则是有界的. 有界区域的最大个 ...
- google无法播放mp4 chrome无法播放h264
写在前面 我在chrome上无法播放h264+Acc的mp4,在firefox.ie都可以播放,而且此mp4在vlc终可以正常播放. 视频链接:http://106.14.221.185:7001/p ...
- 中间人攻击-Arp之局域网内DNS欺骗
基础知识 网关是啥? 网关是工作在OSI七层模型中的传输层或者应用层,用于高层协议的不同网络之间的连接,网关就好比一个房间通向另一个房间的一扇门. ARP协议 假设A(192.168.1.2)与B(1 ...
- C# WCF 之优势及特性
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...
- 科学计算包Numpy
Numpy 用于科学计算的python模块,提供了Python中没有的数组对象,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换以及随机数生成等功能,并可与C++.FO ...
- Spring框架中文件目录遍历漏洞 Directory traversal in Spring framework
官方给出的描述是Spring框架中报告了一个与静态资源处理相关的目录遍历漏洞.某些URL在使用前未正确加密,使得攻击者能够获取文件系统上的任何文件,这些文件也可用于运行SpringWeb应用程序的进程 ...