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

时间:2022-11-10 16:02:26

B. Robin Hood

题目连接:

http://www.codeforces.com/contest/671/problem/B

Description

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.

Sample Input

4 1

1 1 4 2

Sample Output

2

题意

每次你要让最大数减一,然后让最小数加一

然后操作k次

问你最大值减去最小值是多少

题解:

首先这道题模拟是可以的,但是太麻烦了……

所以还是直接二分就好了

二分一个最小值

然后再二分一个最大值

因为你会加k,使得小于那个最小值的数都加成为大于等于他的数

你会减去k,使得大于那个数,都降为小于等于的那个数

所以直接二分就完啦

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n,k,a[maxn];
long long sum=0;
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum+=a[i];
sort(a+1,a+1+n);
int l1=sum/n,r1=(sum+n-1)/n;
int l=0,r=l1,ansl=0;
while(l<=r)
{
int mid=(l+r)/2;
long long need=0;
for(int i=1;i<=n;i++)if(a[i]<=mid)need+=mid-a[i];
if(need<=k)ansl=mid,l=mid+1;
else r=mid-1;
}
l=r1,r=1e9;
int ansr=0;
while(l<=r)
{
int mid=(l+r)/2;
long long need=0;
for(int i=1;i<=n;i++)if(a[i]>mid)need+=a[i]-mid;
if(need<=k)ansr=mid,r=mid-1;
else l=mid+1;
}
cout<<ansr-ansl<<endl;
}

Codeforces Round #352 (Div. 1) B. Robin Hood 二分的更多相关文章

  1. Codeforces Round &num;352 &lpar;Div&period; 2&rpar; D&period; Robin Hood 二分

    D. Robin Hood   We all know the impressive story of Robin Hood. Robin Hood uses his archery skills a ...

  2. Codeforces Round &num;352 &lpar;Div&period; 2&rpar; D&period; Robin Hood &lpar;二分答案&rpar;

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

  3. Codeforces 671B/Round &num;352&lpar;div&period;2&rpar; D&period;Robin Hood 二分

    D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...

  4. Codeforces Round &num;352 &lpar;Div&period; 1&rpar; B&period; Robin Hood (二分)

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

  5. Codeforces Round &num;352 &lpar;Div&period; 1&rpar; B&period; Robin Hood

    B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...

  6. Codeforces Round &num;352 &lpar;Div&period; 2&rpar; D&period; Robin Hood

    题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...

  7. Codeforces Round &num;352 &lpar;Div&period; 2&rpar; ABCD

    Problems     # Name     A Summer Camp standard input/output 1 s, 256 MB    x3197 B Different is Good ...

  8. Codeforces Round &num;352 &lpar;Div&period; 2&rpar;

    模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...

  9. Codeforces Round &num;352 &lpar;Div&period; 2&rpar; &lpar;A-D&rpar;

    672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...

随机推荐

  1. linux几种快速清空文件内容的方法

    linux几种快速清空文件内容的方法 几种快速清空文件内容的方法: $ : > filename #其中的 : 是一个占位符, 不产生任何输出. $ > filename $ echo & ...

  2. myawr &colon; mysql性能监控

    myawr以mysql instance 为单位,每隔一段时间进行采样,然后把数据保存到数据库,以便分析.目前myawr脚本收集的信息包括5个部分: 1 系统方面的:负载.cpu.io.网络.swap ...

  3. &lbrack;LintCode&rsqb; Permuation Index

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  4. Java(JVM运行时)各种内存区域详解及扩展

    本文整理于  Java内存与垃圾回收调优 Java 堆内存 从几个sample来学习Java堆,方法区,Java栈和本地方法栈 首先来一张图让我们理清楚java运行时状态: 诚然,如上图所示:java ...

  5. 3D 转换

    用X.Y.Z分别表示空间的3个维度,三条轴互相垂直.如下图 1.左手坐标系 2.透视(perspective) 电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现 ,通过 ...

  6. 一般处理程序装配数据到html页的原理

    相应html页面并保存状态输出原理:(有状态请求)请求页面提交给后台,获取值进行处理之后再根据name标记读取原html文件文字将值替换再一并返回给页面:(在response时替换)比如原模板< ...

  7. NUnit单元测试资料汇总

    NUnit单元测试资料汇总 从安装到配置 首先到官网http://www.nunit.org/下载如下图的资料,安装NUnit-2.6.1.msi包. 然后挂在VS2010外部工具这个地方来使用,工具 ...

  8. 【Floyd】BZOJ1491&colon; &lbrack;NOI2007&rsqb;社交网络

    Description   Solution n<=100自然联想Floyd 设两个数组d[n][n]存最短距离,t[n][n]存最短路径条数 更新d的时候顺便更新t,乘法原理 if(d[i][ ...

  9. CSS之精灵图(雪碧图)与字体图标

    本文内容: 精灵图 字体图标 首发日期:2018-05-01 精灵图: 在以前,每个图片资源都是独立的一张张图片,浏览器访问网站中的不同网页时是重复获取这一张张图片的,这代表需要访问很多次资源. 为了 ...

  10. python--事务操作

    #coding=utf-8 import sys import MySQLdb class TransferMoney(object): def __init__(self,conn): self.c ...