传送门

首先减的顺序是无关紧要的,那么有一个显然的贪心

每次减都减最大或者最小的,因为如果不这样操作,最大的差值不会变小

那么直接把序列排序一下然后模拟一下操作过程即可,别一次只减 $1$ 就好

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline ll read()
{
ll x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e5+;
ll n,m,A[N],ans;
int main()
{
n=read(),m=read();
for(int i=;i<=n;i++) A[i]=read();
sort(A+,A+n+); ans=A[n]-A[];
for(int i=;i<(n/)+(n&);i++)
{
ll t=min(A[i+]-A[i] + A[n-i+]-A[n-i] , m/i);
ans-=t; m-=t*i;
}
if(!(n&))
{
ll t=min(A[n/+]-A[n/], m/(n/) );
ans-=t; m-=t*(n/);
}
printf("%lld\n",ans);
return ;
}

Codeforces 1244E. Minimizing Difference的更多相关文章

  1. codeforces 1244E Minimizing Difference (贪心)

    (点击此处查看原题) 题意分析 给出n个数,a1,a2...an,现在可以进行最多k次操作,每次操纵可以使得任意一个数自增或者自减,问经过最多k次操作后,n个数中的最大值-最小值最小为多少? 解题思路 ...

  2. Codeforces 903D Almost Difference

    Codeforces 903D Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes ...

  3. [CF#592 E] [二分答案] Minimizing Difference

    链接:http://codeforces.com/contest/1244/problem/E 题意: 给定包含$n$个数的数组,你可以执行最多k次操作,使得数组的一个数加1或者减1. 问合理的操作, ...

  4. Codeforces 1476G - Minimum Difference(带修莫队+根号平衡)

    Codeforces 题目传送门 & 洛谷题目传送门 震惊!我竟然独立切掉了这道 *3100 的题! 虽然此题难度的确虚高,感觉真实评分也就 2800~2900 罢.但感觉还是挺有成就感的( ...

  5. Minimizing Difference 【思维】

    题目链接: https://vjudge.net/contest/336389#problem/B 题目大意: 给出一个长度为n的数列以及操作次数k.k的范围为1e14.每次操作都可以选择给任意一个数 ...

  6. CodeForces - 1244E

    题意:给n个数,可以有k次的 + 1或 - 1,在k次操作之内,让n个数的最大值和最小值差最小. 思路:要让max和min的差值最小,也就等同于min--,max++,如果k==0结束操作,或者min ...

  7. codeforces #592(Div.2)

    codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...

  8. codeforces A. Difference Row

    link:http://codeforces.com/contest/347/problem/A 开始看起来很复杂的样子,但是刚写下样例,就发现因为中间的都消去了,其实起作用的就是最大值和最小值=_= ...

  9. codeforces A. Difference Row 解题报告

    题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2 ...

随机推荐

  1. moveUp()

    这个函数内容有点多,想讲一下大概思路: 向上移有两种情况1.前面为空白 这种情况有两个步骤 (1)将人当前的位置设置为空白(0), (2)再讲人前面的位置设置为人(2)2.前面为箱子 当前面为箱子时有 ...

  2. 关于AngularJS与其他前端框架混合使用的思考

    AngularJS 是一个为动态WEB应用设计的结构框架,拥有双向数据绑定,模板,MVVM,依赖注入,指令5大优点,Angular最有诱惑力的就是数据绑定功能,使用MVC模式进行开发,Angular在 ...

  3. LVS之ipvsadm命令

    目录: 安装 基本描述 用法 命令选项 示例 [安装] 可使用yum安装或者从官网下载安装包源码安装,两种方式皆可 先检查是否已经安装ipvsadm [root@v_machine1 ~]# yum ...

  4. leetcode-hard-ListNode-Copy List with Random Pointer-NO

    mycode 报错:Node with val 1 was not copied but a reference to the original one. 其实我并没有弄懂对于ListNode而言咋样 ...

  5. [java]将秒数转化为“天时分秒”的格式(转贴+修改)

    public class Time { // format seconds to day hour minute seconds style // Exmplae 5000s will be form ...

  6. linux 下项目的发布

    [wangq10@VM000001865 logs]$ [wangq10@VM000001865 ~]$ ls[wangq10@VM000001865 logs]$ apache-tomcat-7.0 ...

  7. RabbitMQ学习之:(六)Direct Exchange (转贴+我的评论)

    From: http://lostechies.com/derekgreer/2012/04/02/rabbitmq-for-windows-direct-exchanges/ RabbitMQ fo ...

  8. itchat库微信自动回复祝福语

    过年了,之前看到一些python文章介绍用itchat自动回复微信,我自己就写了一个. 官方文档https://itchat.readthedocs.io/zh/latest/,这个库挺简洁的,对着接 ...

  9. c99数组稀疏初始化

    #include <stdio.h> #include <stdlib.h> int main() { ] = {[]=,,[] =}; ; i < ; i++) { p ...

  10. sql拼接中的小错误

    字符串类型变量拼接到sql字符串上,容易忘记添加单引号,使用jdbcTemplate执行,报如下错误 正确写法如下: