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 ...
随机推荐
- 题解 SDOI2010 【栗栗的书架】
\[ Preface \] 看到这题洛谷标签有 主席树 ,还以为是什么二维主席树的玄学做法(雾 \[ Description \] 给出一个 \(R×C\) 的矩阵. 一共 \(m\) 次询问,每次询 ...
- MySQL存储引擎——MyISAM与InnoDB区别
注:本文来自:https://blog.csdn.net/xifeijian/article/details/20316775 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型, ...
- Gorm与数据库(单复数)表结构之间的映射
Gorm连接MySQL: import ( _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/gorm" ...
- 草台班子--Window10文件管理器默认显示图片DPI及位深信息
草台班子--Window10文件管理器默认显示图片DPI及位深信息 经常处理一些图片的DPI/PPI信息,总是需要找软件打开确认,通过Windows10文件浏览器可以默认显示图片的DPI信息 ...
- 杭电-------2032杨辉三角(C语言写)
#include<stdio.h> ][] = { }; void init() { int i, j; ; i < ; i++) { a[i][] = ; a[i][i] = ; ...
- oracle11g R2数据库的迁移(同windows系统迁移)使用RMAN
实验环境:windows 2008 R2 & windows 2008 R2 Oracle版本:11.2.0.1.0 源数据库端: 为保证在恢复之后的数据库中得到一致的数据,应禁止用户对数据的 ...
- zabbix-agent TIME_WAIT 过多(转)
一.系统环境 操作系统: Centos 6.4 64bit zabbix-agent 版本: Zabbix agent v2.2.7 (revision 50148) (24 October 2014 ...
- Arm开发板+Qt学习之路-论can网通讯受log日志的影响
日期:2016-05-25 最近开发过程中发现一个问题,使用两个开发板进行can网通讯,按照经验来说,通讯的速度应该是很快的,项目中将接口的超时时间设置为100ms,在某种情境下,会在短时间内发送多次 ...
- Leetcode:面试题 04.03. 特定深度节点链表
Leetcode:面试题 04.03. 特定深度节点链表 Leetcode:面试题 04.03. 特定深度节点链表 先贴一下自己写过一个模板,按层数遍历: https://www.cnblogs.co ...
- leetcode-简单-栈-逆波兰表达式
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话 ...