codeforces 672D D. Robin Hood(二分)
题目链接:
1 second
256 megabytes
standard input
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.
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.
Print a single line containing the difference between richest and poorest peoples wealth.
4 1
1 1 4 2
2
3 1
2 2 2
0
Lets look at how wealth changes through day in the first sample.
- [1, 1, 4, 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(二分)的更多相关文章
- 【15.93%】【codeforces 672D】Robin Hood
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- codeforces 671B Robin Hood 二分
题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CF 672D Robin Hood(二分答案)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
随机推荐
- python 获取时间 存入文件
1读文件: file_path_name = '/home/robot/bzrobot_ws/src/bzrobot/bzrobot_comm/led_show_data/'+file_name+'. ...
- 数据库设计三范式(3NF)
问:当时你数据库是如何设计的? 答:当时是按照三范式规范设计的: 第一范式: 1:数据库的原子性,即保证数据库表的每一列都不可分割的 第二范式: 1:原子性,即保证数据库表的每一列都不可分割 2:表中 ...
- 实验十二 swing图形界面设计
1.源程序 package information;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event ...
- csu1527: Bounty Hunter
Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Status id=134 ...
- win7系统使用engine进行开发报错,“未能加载文件或程序集”
http://www.gisall.com/wordpress/?p=7161 使用vs2010加 arcengine 开发winfrom应用,新建了uc,拖了几个控件后,编译,报未能加载文件或程序集 ...
- fatal error C1189: #error : core.hpp header must be compiled as C++
两次opencv工程需要设置为C++编译:找了一半天的解决方法. I am building a C application that uses OpenCV. when compiling, I g ...
- BUPT复试专题—网络传输(2014网研)
题目描述 网络的高效互联与智能传输是提升海量用户服务请求映射效率的重要措施.在这个任务中,你需耍在最小的传输时间内,将数据源传输到指定的网络节点中.我们给定的网络一共包含N个节点,其中节点1为数据源. ...
- BUPT复试专题—密码(2009)
题目描述 输入 有多组输入,每组: 第一行:由26个小写字母组成的串以空格隔开,如 b a c e u f g h i j k l m n o p q r s t v w x y z d v y ...
- Golang Global Variable access
golang 中全局变量的问题. ------------------------------------------------------------------ 17down votefavor ...
- 手写 redux 和 react-redux
1.手写 redux redux.js /** * 手写 redux */ export function createStore(reducer) { // 当前状态 let currentStat ...