题目链接:

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+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=5e5+;
int a[N];
int n,k;
int cmp(int x,int y)
{
return x>y;
}
int check(int x)
{
LL sum=;
for(int i=;i<=n;i++)
{
if(a[i]>x)
{
sum+=(LL)(a[i]-x);
}
else break;
}
if(sum>k)return ;
return ;
}
int ok(int x)
{
LL sum=;
for(int i=n;i>;i--)
{
if(a[i]<x)
{
sum+=(LL)(x-a[i]);
}
else break;
}
if(sum>k)return ;
return ;
}
int main()
{
int ans;
LL s=;
scanf("%d%d",&n,&k);
Riep(n)scanf("%d",&a[i]),s+=(LL)a[i];
int mmin,mmax;//mmin最大的数的下限,mmax为最小数的上限;
if(s%n==)
{
mmin=mmax=s/n;
}
else
{
mmin=s/n;
mmax=mmin+;
}
sort(a+,a+n+,cmp);
int l=mmax,r=1e9+;
while(l<=r)
{
int mid=(l+r)>>;
if(!check(mid))l=mid+;
else r=mid-;
}
ans=l;
l=,r=mmin;
while(l<=r)
{
int mid=(l+r)>>;
if(ok(mid))l=mid+;
else r=mid-;
}
ans=ans-r;
cout<<ans<<"\n";
return ;
}

codeforces 672D D. Robin Hood(二分)的更多相关文章

  1. 【15.93%】【codeforces 672D】Robin Hood

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. Codeforces Round #352 (Div. 1) B. Robin Hood 二分

    B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...

  3. Codeforces 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 a ...

  4. 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 ...

  5. Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)

    题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...

  6. codeforces 671B Robin Hood 二分

    题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...

  7. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. CF 672D Robin Hood(二分答案)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #352 (Div. 1) B. Robin Hood (二分)

    B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. spring--路由

    @RestController: Spring4之后新加入的注解,原来返回json需要@ResponseBody和@Controller配合. 即@RestController是@ResponseBo ...

  2. 装B技能GET起来!Apple Pay你会用了吗?

    科技圈儿有一个自带光环的品牌 它每次一有任何动静 不用宣传 也不用刻意营销 消息还是能传天下 2月18日 你敢说你的朋友圈儿没有被下面这个词儿刷屏? Apple Pay 这不,我就跟着凑凑热闹,开个小 ...

  3. hdu - 3594 Cactus (强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=3594 判断给定的图是否是强连通的,并且每条边都只属于一个连通分量. 判断强连通只需要判断缩点之后顶点数是否为1即 ...

  4. [Inside HotSpot] Serial垃圾回收器 (二) Minor GC

    Serial垃圾回收器Minor GC 1. DefNewGeneration垃圾回收 新生代使用复制算法做垃圾回收,比老年代的标记-压缩简单很多,所有回收代码都位于DefNewGeneration: ...

  5. SPOJ - PERMJUMP Permutation Jumping

    Discription John likes playing the game Permutation Jumping. First he writes down a permutation A of ...

  6. java基础之IO流(一)字节流

    java基础之IO流(一)之字节流 IO流体系太大,涉及到的各种流对象,我觉得很有必要总结一下. 那什么是IO流,IO代表Input.Output,而流就是原始数据源与目标媒介的数据传输的一种抽象.典 ...

  7. php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的

    原文呢:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict Onl ...

  8. CellularAutomation(细胞自己主动机)

    CellularAutomation(细胞自己主动机) 细胞自己主动机(英语:Cellular automaton).又称格状自己主动机.元胞自己主动机,是一种离散模型,在可算性理论.数学及理论生物学 ...

  9. 2.6.2 用NPOI操作EXCEL--设置密码才可以修改单元格内容

    2.6.2 用NPOI操作EXCEL--设置密码       有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完 ...

  10. 设计模式入门之訪问者模式Visitor

    //訪问者模式定义:表示一个作用于某对象结构中的各个元素的操作,它使你能够在不改变各元素类的前提下定义作用于这些元素的新操作. //从定义上看.这个模式跟装饰模式的定义非常类似(动态地给一个对象加入一 ...