zoukankan      html  css  js  c++  java
  • Codeforces Round #592 (Div. 2) E. Minimizing Difference

    E. Minimizing Difference

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    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

    Copy

    4 5
    3 1 7 5
    

    output

    Copy

    2
    

    input

    Copy

    3 10
    100 100 100
    

    output

    Copy

    0
    

    input

    Copy

    10 9
    4 5 5 7 5 4 5 2 4 3
    

    output

    Copy

    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.

    刚看见这道题的时候满脑子都是二分策略

    其实在纸上随便写写就能发现排好顺序周要么升高头
    要么就降低尾部 对于这两种方法我们贪心的选择消耗少的即可

    #include<bits/stdc++.h>
    using namespace std;
    int a[100005];
    int main()
    {
        int n;
        long long k;
        scanf("%d%lld",&n,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n);
        int pos1=1;
        int pos2=n;
        long long ans=a[n]-a[1];
        while(pos1<pos2)
        {
            long long tmp=0;
            if(pos1-1<=n-pos2)
            {
                pos1++;
                tmp=min(1LL*a[pos1]-a[pos1-1],k/(pos1-1));
                ans-=tmp;
                k-=1LL*tmp*(pos1-1);
            }
            else
            {
                pos2--;
                tmp=min(1LL*a[pos2+1]-a[pos2],k/(n-pos2));
                ans-=tmp;
                k-=1LL*tmp*(n-pos2);
            }
    //        cout<<tmp<<endl;
            //if(tmp==0) break;
        }
        printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    Codeforces 358 D. Dima and Hares
    sublime Text3配置及快捷键、插件推荐总结
    免费WiFi,仅仅为好久没联系的你们
    史上最简单的带流控功能的http server
    微软2014校园招聘笔试试题
    python 3Des 加密
    MySQL slave状态之Seconds_Behind_Master
    APP安全环节缺失,手游运营商怎样应对APP破解困境
    读完了csapp(中文名:深入理解计算机系统)
    static使用方法小结
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852192.html
Copyright © 2011-2022 走看看