zoukankan      html  css  js  c++  java
  • CodeForces

    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.

    Example

    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.

     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 #include<stack>
    16 #include<vector>
    17 long long   n,m;
    18 long long  a[510000];
    19 long long  minfind(long long  zhi)
    20 {
    21     long long sum=0;
    22     for(int i=1;i<=n;i++)
    23     {
    24         if(a[i]<=zhi)
    25             sum+=zhi-a[i];
    26     }
    27     return sum;
    28 }
    29 long long maxfind(long long zhi)
    30 {
    31     long long sum=0;
    32     for(int i=n;i>=1;i--)
    33     {
    34         if(a[i]>zhi)
    35             sum+=a[i]-zhi;
    36     }
    37     return sum;
    38 }
    39 int main()
    40 {
    41     scanf("%d %d",&n,&m);
    42 
    43     long long sum=0;
    44     for(int i=1;i<=n;i++)
    45     {
    46         scanf("%d",&a[i]);
    47         sum+=a[i];
    48     }
    49     sort(a+1,a+1+n);
    50     long long t;
    51     long long kaishi=0;
    52     long long jieshu=sum/n;
    53     long long mid;
    54     while(kaishi<=jieshu)
    55     {
    56         mid=(kaishi+jieshu)>>1;
    57         if(m>=minfind(mid))
    58         {
    59             t=mid;
    60             kaishi=mid+1;
    61         }
    62         else
    63             jieshu=mid-1;
    64     }
    65     long long min1=t;
    66     kaishi=(sum+n-1)/n;
    67     jieshu=1e9;
    68     while(kaishi<=jieshu)
    69     {
    70         //cout<<kaishi<<"_"<<jieshu<<endl;
    71         mid=(kaishi+jieshu)>>1;
    72         if(m>=maxfind(mid))
    73         {
    74             t=mid;
    75             jieshu=mid-1;
    76         }
    77         else
    78             kaishi=mid+1;
    79 
    80     }
    81     //cout<<min1<<"_"<<t<<endl;
    82     cout<<t-min1<<endl;
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    HDU2034 人见人爱 A
    二分查找
    利用向量积(叉积)计算三角形(多边形)的面积
    找出能被5或6整除,但是不能被两者同时整除的数 Exercise05_11
    找出分数最高的前两个学生 Exercise05_09
    金融应用,计算将来的学费 Exercise05_07
    千克与磅之间的转换 Exercise05_05
    将千克转换成磅 Exercise05_03
    统计正数和负数的个数,然后计算这些数的平均值 Exercise05_01
    回文数
  • 原文地址:https://www.cnblogs.com/dulute/p/7966721.html
Copyright © 2011-2022 走看看