zoukankan      html  css  js  c++  java
  • Codeforces 671B/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
    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次操作后,最大和最小的数的差。

    思路 : 题目可以简化成找最终状态下的最大值和最小值,那么分别进行二分最大值和最小值,此外注意二分最大、最小时的不同之处。

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <string.h>
     5 using namespace std;
     6 
     7 const int MAX = 500010;
     8 const int INF = 0x3f3f3f3f;
     9 int a[MAX], n;
    10 
    11 int findmax(int x, int k)
    12 {
    13     for(int i = 0; i < n; i++)
    14     {
    15         if(a[i] > x)
    16             k-=a[i] - x;
    17         if(k < 0)
    18             return  0;
    19     }
    20     return 1;
    21 }
    22 int findmin(int x, int k)
    23 {
    24     for(int i = 0; i < n; i++)
    25     {
    26         if(a[i] < x)
    27             k-=x - a[i];
    28         if(k < 0)
    29             return  0;
    30     }
    31     return 1;
    32 }
    33 
    34 int main()
    35 {
    36     int k;
    37     __int64 sum = 0;
    38     int t1, t2;
    39     cin >> n >> k;
    40     t1 = 0;
    41     t2 = INF;
    42     for(int i = 0; i < n; i++)
    43     {
    44         scanf("%d", a + i);
    45         sum += a[i];
    46         t1 = max(t1, a[i]);
    47         t2 = min(t2, a[i]);
    48     }
    49     int mx = sum / n + (sum%n>0 ?1 : 0);
    50     int mi = sum / n;
    51 
    52     int l , r, maxx , minn;
    53     /////mi
    54     l = t2, r = mi;
    55     while(l <= r) //注意等于
    56     {
    57         int mid = (l + r) >> 1;
    58         if(findmin(mid, k))
    59         {
    60             l = mid + 1;
    61             minn =  mid;
    62         }
    63         else r = mid - 1;
    64     }
    65     ////ma
    66     l = mx, r = t1;
    67     while(l < r)
    68     {
    69         int mid = (l + r) >> 1;
    70         if(findmax(mid, k))
    71         {
    72             r = mid;
    73             maxx = max(mid, maxx);
    74         }
    75         else l = mid + 1;
    76     }
    77     maxx = (l +  r) >>1;   //结束状态时取
    78     printf("%d
    ", maxx - minn);
    79 }
    View Code
  • 相关阅读:
    JAVA WEBSERVICE服务端&客户端的配置及调用(基于JDK)
    An internal error occurred during: "Launching New_configuration"
    Android 创建虚拟机时“提示no system images installed for this target”
    [转] 传说中的WCF(2):服务协定的那些事儿
    [转] 传说中的WCF
    python的包管理
    python入门常用方法(转json,模拟浏览器请求头,写入文件)
    python读写数据篇
    python跳坑手记
    python爬虫入门篇
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5500569.html
Copyright © 2011-2022 走看看