Minimizing Difference CodeForces - 1244E

Problem Description

You are given a a a sequence a 1 , a 2 , … , a n a_{1},a_{2},…,a_{n} a1,a2,,an consisting of n n n 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 k k times.

Input

The first line contains two integers n n n and k k k ( 2 ≤ n ≤ 1 0 5 , 1 ≤ k ≤ 1 0 14 ) (2≤n≤10^{5},1≤k≤ 10^{14}) (2n105,1k1014) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.The second line contains a a a sequence of integers a 1 , a 2 , … , a n a_{1},a_{2},…,a_{n} a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1≤a_{i}≤10^{9} 1ai109).

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 k k k 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 3,3,5,5], and the difference between maximum and minimum is 2 2 2. You still can perform one operation after that, but it’s useless since you can’t make the answer less than 2 2 2.In the second example all elements are already equal, so you may get 0 0 0 as the answer even without applying any operations.

题目大意

第一行给出n和k,第二行给出n个数,每一步都可以把任意一个数进行加1或减1,最多进行k次,问数列经历变化后,极差最小能是多少?

大概思路

首先进行排序,例如第三组数据 4 5 5 7 5 4 5 2 4 3,k为9
4 5 5 7 5 4 5 2 4 3 2 3 4 4 4 5 5 5 5 7 {\color{DarkRed} \begin{matrix} 4&5 &5&7&5&4&5&2&4&3 \\ 2& 3&4&4&4&5&5&5&5&7 \end{matrix}} 42535474544555254537
首先我们可以把2进行加一变成3,此时k为9,k-1,那么数列变为
4 5 5 7 5 4 5 2 4 3 2 3 4 4 4 5 5 5 5 7 3 3 4 4 4 5 5 5 5 7 {\color{DarkRed} \begin{matrix} 4&5 &5&7&5&4&5&2&4&3 \\ 2& 3&4&4&4&5&5&5&5&7 \\ 3& 3&4&4&4&5&5&5&5&7 \end{matrix}} 423533544744544455555255455377
然后发现我们可以把两个3变为4,或者把一个7变为5;都是需要两步,那么在这里我们把两个3变成4,此时k为8,k-2之后数列就变为,此时k为:
2 3 4 4 4 5 5 5 5 7 3 3 4 4 4 5 5 5 5 7 4 4 4 4 4 5 5 5 5 7 {\color{DarkRed} \begin{matrix} 2& 3&4&4&4&5&5&5&5&7 \\ 3& 3&4&4&4&5&5&5&5&7 \\ 4& 4&4&4&4&5&5&5&5&7 \end{matrix}} 234334444444444555555555555777
然后我们可以把4个4变为5,需要4步,也可以把一个7变为5需要两步,因此我们先把7变成5,此时k为6,k-2数列变为了
3 3 4 4 4 5 5 5 5 7 4 4 4 4 4 5 5 5 5 7 4 4 4 4 4 5 5 5 5 5 {\color{DarkRed} \begin{matrix} 3& 3&4&4&4&5&5&5&5&7 \\ 4& 4&4&4&4&5&5&5&5&7 \\ 4& 4&4&4&4&5&5&5&5&5 \end{matrix}} 344344444444444555555555555775
然后我们发现要想把5个4变成5需要5步,或者把5个5变成4需要5步,但是k的值为4不可能完成,所以极差最小为1;
我们可以定义一个数记录数列的极差,他每成功变化一次那么就会减一但是出现这种情况例如 5 5 1 1 \begin{matrix} 5& 5 &1&1 \end{matrix} 5511 那么就需要减去k/它长度i了,因为你可以减去多次;
代码如下:

#include<iostream>
#include<algorithm> 
using namespace std;
#define ll long long
ll a[201010];
int main(){
	ll n,k,ans=0;
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	sort(a+1,a+n+1);
        ans=a[n]-a[1];
	if(n%2){
		for(int i=1;i<=(n/2);i++){
			ll t=min(a[i+1]-a[i]+a[n-i+1]-a[n-i],k/i);
			ans-=t;
			k-=t*i;
		}
	}
	else {
		for(int i=1;i<(n/2);i++){
			ll t=min(a[i+1]-a[i]+a[n-i+1]-a[n-i],k/i);
			ans-=t;
			k-=t*i;
		}
	}
	if(n%2==0){
		ll t=min(a[n/2+1]-a[n/2],k/(n/2));
		ans-=t;
		k-=t*(n/2);
	}	
	printf("%lld\n",ans);
	return 0;
}

实践是检验真理的唯一标准

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值