Honk's pool(暴力,巧妙)

明若清溪天下绝歌 缱绻成说,不知该在哪处着墨;一生情深怎奈何世事 徒留斑驳,只一念痴恋成奢。

As we all know, Honk has nn pools, numbered as 11 ~ nn . There is a_iai​ liters water in the ii-th pool. Every day, Honk will perform the following operations in sequence.

  1. Find the pool with the most water (If there are more than one, choose one at random) and take one liter of water.

  2. Find the pool with the least water (If there are more than one, choose one at random) and pour one liter of water into the pool.

  3. Go home and rest (Waiting for the next day).

Please calculate the difference between the amount of water in the pool with the most water and the amount of water in the pool with the least water after the kk days.

Input

The input consists of multiple test cases. The input is terminated by the end of file.The number of data sets will not exceed 40

The first line of each test case contains two integers nn and kk, which indicate the number of pools and the number of days to operate the pool.

The second line of each test case contains nn integers, and the ii-th number represent a_iai​ indicating the initial amount of water in the ii-th pool.

1 \le n \le 5000001≤n≤500000, 1 \le k \le 10^91≤k≤109, 1 \le a_i \le 10^91≤ai​≤109.

Output

For each test case, print one line containing the answer described above.

样例输入1复制

4 100
1 1 10 10

样例输出1复制

1

样例输入2复制

4 3
2 2 2 2

样例输出2复制

0
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int p[500010];
int n,k,ct;
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%d",&p[i]);
        sort(p,p+n);
        for(int i=0; i<k; i++)
        {
            ct=n-1;
            while(ct>0&&p[ct]==p[ct-1])
                ct--;
            p[ct]--;
            ct=0;
            while(ct<n-1&&p[ct]==p[ct+1])
                ct++;
            p[ct]++;
        }
        printf("%d\n",p[n-1]-p[0]);
    }
//    for(int i=0;i<n;i++)
//        cout<<p[i]<<' ';
//    cout<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值