• codeforces 672D D. Robin Hood(二分)


    题目链接:

    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.

    题意:

    每天把最大数减1,最小的数加1,问k天之后最大的数和最小的数的差是多少;

    思路:

    分别二分k天之后的最大的数和最小的数,相减就是结果了,不过要先求出这两个二分的上下限,具体的看代码;

    AC代码:

    #include <bits/stdc++.h>
    /*#include <iostream>
    #include <queue>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    */
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=0x3f3f3f3f;
    const int N=5e5+25;
    int a[N];
    int n,k;
    int cmp(int x,int y)
    {
        return x>y;
    }
    int check(int x)
    {
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]>x)
            {
                sum+=(LL)(a[i]-x);
            }
            else break;
        }
        if(sum>k)return 0;
        return 1;
    }
    int ok(int x)
    {
        LL sum=0;
        for(int i=n;i>0;i--)
        {
            if(a[i]<x)
            {
                sum+=(LL)(x-a[i]);
            }
            else break;
        }
        if(sum>k)return 0;
        return 1;
    }
    int main()
    {
        int ans;
        LL s=0;
        scanf("%d%d",&n,&k);
        Riep(n)scanf("%d",&a[i]),s+=(LL)a[i];
        int mmin,mmax;//mmin最大的数的下限,mmax为最小数的上限;
        if(s%n==0)
        {
            mmin=mmax=s/n;
        }
        else
        {
            mmin=s/n;
            mmax=mmin+1;
        }
        sort(a+1,a+n+1,cmp);
        int l=mmax,r=1e9+1;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(!check(mid))l=mid+1;
           else r=mid-1;
        }
        ans=l;
        l=1,r=mmin;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(ok(mid))l=mid+1;
            else r=mid-1;
        }
        ans=ans-r;
        cout<<ans<<"
    ";
        return 0;
    }
  • 相关阅读:
    vue 按需加载
    需要打印真实尺寸大小等需求的,css的单位可以使用mm等做单位
    d3 比例尺
    d3 根据数据绘制svg
    d3 svg简单学习
    d3 使用随机数据生成条形图
    d3 画简单的柱形图
    d3 使用数据
    d3 数据绑定
    d3 添加元素相关api
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5484573.html
Copyright © 2020-2023  润新知