zoukankan      html  css  js  c++  java
  • CF 352 D 罗宾汉发钱 模拟题+贪心

    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 thei-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个人,每个人有一个初始的价值ai,给出k个操作,每个操作必须从当前最富有的那个人拿一个单位的价值给当前最穷的人,问在经历k次操作之后,贫富差距多大

     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <algorithm>
    #include <set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    #define MM(a,b) memset(a,b,sizeof(a));
    const double eps = 1e-10;
    const int  inf =0x7f7f7f7f;
    const double pi=acos(-1);
    const int maxn=100+500000;
    
    ll a[maxn];
    int main()
    {
        int n,k;
        while(~scanf("%d %d",&n,&k))
        {
            ll sum=0;
            for(int i=0;i<n;i++)
                {
                    scanf("%lld",&a[i]);
                    sum+=a[i];
                }
            sort(a,a+n);
    
            int maxn=a[n-1],minn=a[0],tp=k;
            for(int i=n-1;i>=0;i--)
              if(maxn>a[i])
               {
                    if((n-1-i)*(maxn-a[i])<=tp)//乘法int会爆
                    {
                        tp-=(n-1-i)*(maxn-a[i]);
                        maxn=a[i];
                    }//能全部减下去
                    else
                    {
                        maxn-=tp/(n-1-i);
                        break;
                    }//无法进一步减下去
               }
    
            tp=k;minn=a[0];
            for(int i=0;i<n;i++)
               if(minn<a[i])
               {
                   if(i*(a[i]-minn)<=tp)
                   {
                       tp-=i*(a[i]-minn);
                       minn=a[i];
                   }
                   else
                   {
                       minn+=tp/i;
                       break;
                   }
               }
    
            if(minn>=maxn)  printf("%d
    ",sum%n==0?0:1);
            else printf("%d
    ",maxn-minn);
        }
        return 0;
    }

    分析:一开始被吓到了,,后来稍微看了下题解,再自己仔细做了下,,还是觉得很水的.....

    建立模型:将所有人的财富按从小到大排序,然后首先从最右边开始切,最多切k个格子,算出

    切了后的最大的值maxn,然后再从左往右补,算出补了后的最小的值minn,如果maxn>=minn,说明穷人与富人之间出现了反转,则要特判一下所有才丰富能否均分。

  • 相关阅读:
    快排
    Single Number II
    简单工厂和工厂方法
    Implement strStr()
    Linked List Cycle II
    Linked List Cycle
    适配器模式
    Struts2的ActionContext
    javaScript学习随笔
    Tomcat 基本配置(转)
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5528877.html
Copyright © 2011-2022 走看看