zoukankan      html  css  js  c++  java
  • Codeforces Round #352 (Div. 2) D. Robin Hood 二分

    D. Robin Hood
     

    We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

    There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.

    After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.

    Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.

    Input

    The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.

    The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.

    Output

    Print a single line containing the difference between richest and poorest peoples wealth.

    Examples
    input
    4 1
    1 1 4 2
    output
    2
     
    Note

    Lets look at how wealth changes through day in the first sample.

    1. [1, 1, 4, 2]
    2. [2, 1, 3, 2] or [1, 2, 3, 2]

    So the answer is 3 - 1 = 2

    In second sample wealth will remain the same for each person.

    题意:

      给你一个序列

      每一天过后,序列的最大值-1,最小值+1, 问你k天之后,最大值-最小值是多少

    题解:

      在给定k天下

      我们二分最大值尽可能的 小,最小值最可能的大就可以了

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 3e6+20, M = 1e7, mod = 1e9+7,inf  = 1e9+7;
    typedef long long ll;
    
    int a[N],n,k;
    int check1(int mx) {
        ll kk = 0,buji = 0;
        for(int i=1;i<=n;i++) {
            if(a[i]>mx) kk+=(a[i]-mx);
            else {
                buji+=(mx - a[i]);
            }
        }
        if(kk<=k&&buji>=kk) return 1;
        else return 0;
    }
    int check2(int mi) {
        ll kk = 0,buji = 0;
        for(int i=1;i<=n;i++) {
            if(a[i]<mi) kk+=(mi - a[i]);
            else buji += (a[i] - mi);
        }
        if(kk<=k&&buji>=kk) return 1;
        else
        return 0;
    }
    int main() {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        int mx = a[n],mi = a[1];
        int r = a[n], l = a[1];
        while(l<=r) {
            int mid = (l+r)>>1;
            if(check1(mid))  r = mid-1,mx = mid;
            else l = mid+1;
        }
        r = a[n], l = a[1];
        while(l<=r) {
            int mid = (l+r)>>1;
            if(check2(mid))  l = mid+1, mi = mid;
            else r = mid-1;
        }
        //cout<<mx<<" "<<mi<<endl;
        cout<<mx - mi<<endl;
        return 0;
    }
  • 相关阅读:
    【1018 | Day 47】Django概述
    【1016 | Day 46】前端之jQuery
    【1015 | Day 45】前端之BOM和DOM
    【1014 | Day 44】前端之JavaScript
    weex 项目搭建
    用weex create 创建app项目 报 ERROR in index.web.js from UglifyJs 错误!
    最常见的 20 个 jQuery 面试问题及答案
    整理总结的一些前端面试题
    ECMAScript 原始值和引用值
    JavaScript Window Navigator
  • 原文地址:https://www.cnblogs.com/zxhl/p/5491000.html
Copyright © 2011-2022 走看看