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. MariaDB——在Linux中查找数据库路径,并进入数据库

    1.直接在命令行运营mysql,如果出现下图,说明数据库路径没有设置到环境变量里. 2.找出数据库路径,可以用ps -ef | grep my 命令,查找后台正在执行的命令任务中,包含my字母开头的所 ...

  2. 【NetCore】RabbitMQ 封装

    RabbitMQ 封装 代码 https://gitee.com/wosperry/wosperry-rabbit-mqtest/tree/master 参考Abp事件总线的用法,对拷贝的Demo进行 ...

  3. 谈一谈 DDD

    一.前言 最近 10 年的互联网发展,从电子商务到移动互联,再到"互联网+"与传统行业的互联网转型,是一个非常痛苦的转型过程.在这个过程中,一方面会给我们带来诸多的挑战,另一方面又 ...

  4. JVM1 JVM与Java体系结构

    目录 JVM与Java体系结构 虚拟机与Java虚拟机 虚拟机 Java虚拟机 JVM的位置 JVM的整体结构 Java代码执行流程 JVM的架构模型 基于栈的指令级架构 基于寄存器的指令级架构 两种 ...

  5. OC Swift 走马灯效果

    我们常见走马灯样式的功能,下面整理一下 Object-C 与 Swift 的实现代码 OC UILabel *label3 = [[UILabel alloc] initWithFrame:CGRec ...

  6. Enumeration遍历http请求参数的一个例子

    Enumeration<String> paraNames=request.getParameterNames(); for(Enumeration e=paraNames;e.hasMo ...

  7. NSURLSession下载文件-代理

    - 3.1 涉及知识点(1)创建NSURLSession对象,设置代理(默认配置) ```objc //1.创建NSURLSession,并设置代理 /* 第一个参数:session对象的全局配置设置 ...

  8. JDBC(2):JDBC对数据库进行CRUD

    一. statement对象 JDBC程序中的Connection用于代表数据库的链接:Statement对象用于向数据库发送SQL语句:ResultSet用于代表Sql语句的执行结果 JDBC中的s ...

  9. Spring Boot中使用Dubbo

    高并发下Redis会出现的问题: 缓存穿透 缓存雪崩 热点缓存 一.定义commons工程11-dubboCommons (1) 创建工程 创建Maven的Java工程,并命名为11-dubboCom ...

  10. Spring Boot Actuator:健康检查、审计、统计和监控

    Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查.审计.统计和HTTP追踪等.所有的这些特性可以通过JMX或者HTTP endpoints来获得. ...