zoukankan      html  css  js  c++  java
  • Codeforces672D(SummerTrainingDay01-I)

    D. Robin Hood

    time limit per test:1 second
    memory limit per test:256 megabytes
    input:standard input
    output:standard output

    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

    input

    3 1
    2 2 2

    output

    0

    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.

    题意: 有n个人, 每个人都有一定的财富值, 每天有最多财富的人会把自己的一元钱给最少财富的人,求k天之后最富有的人跟最少财富的人的差值是多少。

    思路:二分最后一天时的最大值和最小值。avg为数组平均值,最大值在avg到MAX之间,最小值在0到avg之间。check条件为能否用k使得小于mid的数都变为mid。

     1 //2017-10-15
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 const int N = 510000;
    10 int arr[N];
    11 
    12 int main()
    13 {
    14     //freopen("inputI.txt", "r", stdin);
    15     std::ios::sync_with_stdio(false);
    16     cin.tie(0);
    17     int n, k;
    18     while(cin>>n>>k){
    19         long long sum = 0;
    20         int MAX = 0;
    21         for(int i = 0; i < n; i++){
    22             cin>>arr[i];
    23             MAX = max(MAX, arr[i]);
    24             sum += arr[i];
    25         }
    26         sort(arr, arr+n);
    27         int L = sum/n, R = (sum+n-1)/n;
    28         int l = 0, r = L, mininum = 0;
    29         while(l <= r){
    30             int mid = (l+r)>>1;
    31             long long tmp = 0;
    32             for(int i = 0; i < n; i++)
    33                   if(arr[i] < mid)
    34                       tmp += mid-arr[i];
    35                 else break;
    36             if(tmp <= k){
    37                 mininum = mid;
    38                 l = mid+1;
    39             }else r = mid-1;
    40         }
    41         l = R, r = MAX;
    42         int maxinum = 0;
    43         while(l <= r){
    44             int mid = (l+r)>>1;
    45             long long tmp = 0;
    46             int pos = lower_bound(arr, arr+n, mid)-arr;
    47             for(int i = pos; i < n; i++)
    48                   if(arr[i] > mid)
    49                       tmp += arr[i]-mid;
    50             if(tmp <= k){
    51                 maxinum = mid;
    52                 r = mid-1;
    53             }else l = mid+1;
    54         }
    55         cout<<maxinum-mininum<<endl;
    56     }
    57 
    58     return 0;
    59 }
  • 相关阅读:
    在Node.js环境下使用npm命令安装OpenLayers6.4.3时,提示错误“rollbackFailedOptional: verb npm-session”的解决办法
    Windows7-64环境中部署OpenLayers6.4.3详细步骤
    Navicat 导出csv数据乱码如何处理?
    Win10 如何右键新建.md文件
    关于loading加载的问题
    X度文库越来越不要脸了!
    Layui date 插件 闪烁 无法选择
    每日识字
    如何生成项目目录结构
    小米招聘 hiring
  • 原文地址:https://www.cnblogs.com/Penn000/p/7671342.html
Copyright © 2011-2022 走看看