Robin Hood

题目链接

题意

给你n个人和他们的钱数,然后给你k天,每天可以从最高钱数的人那边取一块钱给最少钱数的人,问最后钱数最多的人和钱数最少的人相差多少;

思路

二分最钱数,能下降到的位置\(low\),和最低钱数能够上涨到的位置\(high\),如果\(low > high\),那么答案就是\(low-high\),

如果\(low <= high\)那么经过k天后如果所有的钱数和能够整除n则答案为0,否则相差1

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL node[600000];
LL n,k;
bool checklo(LL mid)
{
LL sum = 0;
for(int i = 0; i < n; i++)
if(node[i] > mid)
sum += node[i] - mid;
return sum <= k;
}
bool checkhi(LL mid)
{
LL sum = 0;
for(int i = 0; i < n; i++)
if(node[i] < mid)
sum += mid - node[i];
return sum <= k;
}
int main(void)
{
scanf("%lld %lld",&n,&k);
LL sum = 0;
for(int i = 0; i < n; i++)
{
scanf("%lld",&node[i]);
sum += node[i];
}
LL l = 0,r = 1e9;
LL loid = -1;
while(l <= r)
{
LL mid = (l+r)/2;
if(checklo(mid))
loid = mid,r = mid - 1;
else l = mid + 1;
} l = 0,r = 1e9;
LL hiid = -1;
while(l <= r)
{
LL mid = (l+r)/2;
if(checkhi(mid))
hiid = mid,l = mid + 1;
else r = mid - 1;
}
if(hiid < loid)
printf("%lld\n",loid - hiid);
else
{
if(sum%n)
printf("1\n");
else printf("0\n");
}
return 0;
}

Robin Hood的更多相关文章

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

  2. Curious Robin Hood(树状数组+线段树)

    1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...

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

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

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

  5. 【CodeForces】671 B. Robin Hood

    [题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...

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

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

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

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

  9. cf671B Robin Hood

    We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to s ...

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

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

随机推荐

  1. 58-Odd Even Linked List

    Odd Even Linked List My Submissions QuestionEditorial Solution Total Accepted: 29496 Total Submissio ...

  2. orcale => 含义

    => 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.ps(说实话,就是Oracle再执行存储过程中,类似于在word中进行替换一样的感觉,比如说你默认的情况下是你定义了默认参 ...

  3. Unity——Js和Unity互相调用

    Unity项目可以打包成WebGl,打包后的项目文件: Build中是打包后的Js代码: Index.html是web项目的入口,里面可以调整web的自适应,也可以拿去嵌套: TemplateData ...

  4. Freeswitch 安装爬坑记录1

    2 Freeswitch的安装 2.1 准备工作 服务器安装CentOS 因为是内部环境,可以关闭一些防火墙设置,保证不会因为网络限制而不能连接 关闭防火墙 查看防火墙 systemctl statu ...

  5. 学习java 7.18

    学习内容: Lambda表达式的格式:(形式参数)  ->  {代码块} 如果有多个参数,参数之间用逗号隔开 new Thread(  ()   ->   { System.out.pri ...

  6. acquire, acre, across

    acquire An acquired taste is an appreciation [鉴赏] for something unlikely to be enjoyed by a person w ...

  7. Oracle中如何自定义类型

    一:Oracle中的类型有很多种,主要可以分为以下几类:1.字符串类型.如:char.nchar.varchar2.nvarchar2.2.数值类型.如:int.number(p,s).integer ...

  8. Apache架构师的30条设计原则

    本文作者叫 Srinath,是一位科学家,软件架构师,也是一名在分布式系统上工作的程序员. 他是 Apache Axis2 项目的联合创始人,也是 Apache Software 基金会的成员. 他是 ...

  9. Linux学习 - shell脚本执行

    一.shell概述 shell是一个命令行解释器,为用户提供一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至是编写一些程序 shell还是一个功能强 ...

  10. 3.2 go WaitGroup代码示例

    sync.WaitGroup提供了一种安全的多协程处理方法,内部使用race.atomic来处理,避免了资源竞争及锁的产生. 主要的方法有Add.Done.Wait,可以等待一组协程全部执行完毕后,主 ...