题目

  • 题意
  • 官方题解:
  • 百度翻译
  • 思路
  • ac代码

题意

给出一列数,至多n个操作使其中的数+1或-1,要求得到最小的差值(最大值-最小值);

You are given a sequence a1_{1}1​,a2_{2}2​,…,an_{n}n​ consisting of nn integers.

You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.

Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.

Input
The first line contains two integers n and k (2≤n≤105^{5}5,1≤k≤1014^{14}14)— the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.

The second line contains a sequence of integers a1_{1}1​,a2_{2}2​,…,an_{n}n​(1≤ai≤109^{9}9)

Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than kk times.

Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3,3,5,5] and the difference between maximum and minimum is 2. You still can perform one operation after that, but it’s useless since you can’t make the answer less than 2.

In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations.

官方题解:

百度翻译

假设得到的数组中的最大值应该是R,最小值应该是L。让我们估计使用这些属性生成数组所需的操作数。所有小于l的元素都应该增加到l,所有大于r的元素都应该减少到r,我们不必对剩余的元素执行任何操作。
为什么呢?假设我们构造了一个 L∉A 和 R∉A的答案。如果我们增加到L的元素的数量不小于我们减少到R的元素的数量,那么我们可以构造最小等于L 1和最大等于R 1的答案,并且不需要更多的操作。如果我们增加到LL的元素的数量小于我们减少到R的元素的数量,那么我们构造L+1的最小值和最大值R+ 1的答案。所以我们可以移动范围[l,r],使它的一个端点属于
现在我们可以解决如下问题:在结果数组中迭代最大值,找到二进制搜索所能得到的最大最小值,然后反求:在得到的数组中对最小值进行迭代,并用二进制搜索求出最大的最大值。为了检查我们需要多少操作,例如,要使所有值都不小于ll,我们可以通过另一个二进制搜索找到需要更改的元素的数量(让这些元素的数量为m),并用前缀sums找到它们的和(让它们的和为s)。然后,所需的操作数正好是lm-s。可以使用相同的方法来查找使所有元素不大于r的操作数。
这是问题本来应该解决的方式,但不幸的是,我们没有找到一个更容易贪婪的解决方案。

思路

就是一道贪心题·,但由于操作次数过大,范围需定义为 long long分情况(到每个数的个数大小)从两边向中间推进,当两边数相等时结束。(感觉题挺简单,但测试的时候,连题都没看完。一是英语垃圾,二还是学的不精 )

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e5+10;
typedef long long  ll;
ll m,n;
ll dp[M],book[M],s[M];
int main()
{scanf("%lld%lld",&m,&n);for(ll i=1; i<=m; i++){scanf("%d",&dp[i]);book[i]=1;}sort(dp,dp+m+1);ll k=1;s[k]=dp[1];for(ll i=2; i<=m; i++){if(s[k]==dp[i])book[k]++;else{k++;s[k]=dp[i];}}ll a=1,b=k;while(n){if(s[a]==s[b])break;if(book[a]>book[b]){ll bb=book[b]*(s[b]-s[b-1]);if(n<=bb){s[b]=s[b]-(n/book[b]);break;}n-=bb;book[b-1]+=book[b];b--;}else{ll aa=book[a]*(s[a+1]-s[a]);if(n<=aa){s[a]=s[a]+(n/book[a]);break;}n-=aa;book[a+1]+=book[a];a++;}}printf("%lld\n",s[b]-s[a]);return 0;
}

Minimizing Difference CodeForces - 1244E(贪心题)相关推荐

  1. 贪心 ---- Codeforces Global Round 8,B. Codeforces Subsequences[贪心,贪的乘法原理]

    题目链接 给出字符串,统计子串(子串字母可以跳跃)是codeforces的数量. 本题要求,给出子串最少数量k,构造字符串s,要求字符串s包含的字母数量最少,输出这个最少的字符串s. 题目要求是至少有 ...

  2. 【qduoj - 1121】小明的贪心题(Dijkstra最短路 + 最短路条数)

    题干: 小明的贪心题 描述 小明来到青岛上学已经一年了,他给青岛这座城市画了一张地图.在这个地图上有n个点,小明的起始点为1号点,终点为n号点,并且地图上的所有边都是单向的.小明知道从i号点到j号点的 ...

  3. Codeforces 802 补题

    codeforces802 A-O Helvetic Coding Contest 2017 online mirror  A  Heidi and Library (easy) 水题 同B #inc ...

  4. #713. 徐老师的学习计划(思维贪心题)

    很简单的 一道贪心题,需要用一个类似于筒的思想,没输入一个数后就把它对应的那个存储出现次数筒++,如果那个筒* 2 - 1后答案 > n,就输出N,否则到最后输出Y. 代码: #include ...

  5. codeforces 1428E. Carrots for Rabbits(贪心(非常优秀的贪心题),结构体重载运算符)

    题目链接:https://codeforces.ml/contest/1428/problem/E 题意:给定n个数,要求将这些数拆分为k个数,是这些数的平方和最小. 题解:结构体,一开始想到的是每次 ...

  6. Codeforces 985C (贪心)

    传送门 题面: C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. 【ZOJ - 2968 】Difference Game (贪心,思维模拟)

    题干: Now you are going to play an interesting game. In this game, you are given two groups of distinc ...

  8. CodeForces - 93B(贪心+vectorpairint,double +double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  9. codeforces 1041a(水题)

    http://codeforces.com/problemset/problem/1041/A(题目链接) There was an electronic store heist last night ...

最新文章

  1. Android Studio 简介
  2. 写代码时发现......还是 SpringBoot 牛逼!一篇拿下
  3. Filter的细节三学习笔记
  4. Flutter组件学习(二)—— Image
  5. 使用SAP云平台 + JNDI访问Internet Service
  6. springboot 参数校验详解
  7. RuntimeError: Failed to register operator torchvision::_new_empty_tensor_op. +torchtorchversion版本匹配
  8. python魔术方法abstract_python学习之面向对象高级特性和魔术方法
  9. python解释器安装步骤_怎么安装python解释器
  10. 系列3—BabeLua常用技巧
  11. 最近发现一款超牛的记单词软件,分享一下
  12. 魔兽争霸III冰封王座宽屏分辨率的修改方法
  13. 男生学计算机会计,男生学习会计专业好吗
  14. SSM+服装管理系统 毕业设计-附源码080948
  15. 移动最小二乘实现点云插值(上采样 | 增采样)详细讲解❤️❤️❤️
  16. Python学习手册(致敬谢公子)
  17. POJ1655 Balancing Act 题解
  18. Tensorflow中scope命名方法
  19. JAVA-ApplicationContext的使用
  20. 天龙八部元宝兑换代码

热门文章

  1. Android之javax.net.ssl.SSLPeerUnverifiedException: Hostname ip not verified:解决办法
  2. Android之ndk中JNIENV env->NewStringUTF (*env)->NewStringUTF
  3. php基础教程 第九步 自定义函数
  4. 豆瓣评分9分+,每一部看完不禁感慨!这里是神州大地!
  5. 高糊马赛克秒变高清,表情帝:这还是我吗?
  6. 80岁COBOL码农:扶我起来,这个bug我会修!
  7. 想象中的同居生活 VS 真实的同居生活
  8. 只有学霸才懂的学习技巧,看完脑洞大开,绝对涨姿势!
  9. git checkout 单个文件_git 如何回退单个文件
  10. 日历签到 java_我的Android案例―签到日历