#CodeForces - 1244E

You are given a sequence a1,a2,…,ana1,a2,…,an 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 kk times.

Input
The first line contains two integers nn and kk (2≤n≤105,1≤k≤1014)(2≤n≤105,1≤k≤1014) — 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,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109).

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][3,3,5,5], and the difference between maximum and minimum is 22. You still can perform one operation after that, but it’s useless since you can’t make the answer less than 22.

In the second example all elements are already equal, so you may get 00 as the answer even without applying any operations.
这一题的意思是给你一组数据,给你一个整数,让你经过可用步数内得到这组数据的最大值-最小值的差为最小(给的整数就是最大移动步数,每次只能+1或-1)。首先我们可以吧给的数据排序,从大到小或者从小打到都可以,接着我们从两边将同样大的数据同时+1或-1接着*有几个这样的数据,比较哪边消耗的步数更少就采用哪个,这样到最后就能得到最后的数据,将最大值-最小值就是答案了。
下面是代码部分:

#include<string.h>
#include<algorithm>
using namespace std;
long long a[110000];
int main()
{
	int i,j,k;
	long long n,m,sum=0,s;
	scanf("%lld %lld",&n,&m);
	for(i=1;i<=n+1;i++)
	{
		scanf("%lld",&a[i]);	
	}
	sort(a+1,a+n+1);
	for(i=1;i<=n/2;i++)
	{
		s=(a[i+1]-a[i]+a[n-i+1]-a[n-i])*i;
		if(m>=s)
		{
			m=m-s;
		}	
		else
		{
			sum=a[n-i+1]-a[i]-m/i;
			break;
		}
	}
	printf("%lld",sum);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值