大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差.

二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, 否则为$max(1,R-L)$

#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k, a[N];
int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) a[i]=rd();
int l=0,r=1e9,L,R;
while (l<=r) {
ll d = 0;
REP(i,1,n) if (a[i]<mid) d+=mid-a[i];
if (d<=k) L=mid,l=mid+1;
else r=mid-1;
}
l=0,r=1e9;
while (l<=r) {
ll d = 0;
REP(i,1,n) if (a[i]>mid) d+=a[i]-mid;
if (d<=k) R=mid,r=mid-1;
else l=mid+1;
}
int ans = max(0,R-L);
ll sum = 0;
REP(i,1,n) sum+=a[i];
if (sum%n) ans=max(ans,1);
printf("%d\n", ans);
}

Robin Hood CodeForces - 672D (二分)的更多相关文章

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

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

  2. codeforces 672D D. Robin Hood(二分)

    题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...

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

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

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

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

  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. 【15.93%】【codeforces 672D】Robin Hood

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

  9. 【CodeForces】671 B. Robin Hood

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

随机推荐

  1. caffe程序中出现的db.cpp:#line(行号) unknown database backend问题

    报错原因:lmdb不可用 解决方法:Makefile.config将此处更改 CPU_ONLY := 1 #如果只使用CPU的话就改这个,使用GPU的不需要改 USE_OPENCV := 1 #有安装 ...

  2. day20 python常用模块

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀.     但其实import加载的模块分为四个通用类别: 1 使用pytho ...

  3. mondodb和mysql的区别

    我想从安全性和数据量这两个方面进行阐述. mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成, MongoDB ...

  4. Hibernate的load和get方法的区别

    这次我们聊一下Hibernate3.2 Session加载数据时get和load方法的区别,我总结的如下: 1. 对于get方法,hibernate会确认一下该id对应的数据是否存在,首先在sessi ...

  5. 火狐开发----如何快速的安装火狐XPI文件

    第一步:火狐的自动安装扩展程序,https://addons.mozilla.org/zh-CN/firefox/addon/autoinstaller/ 第二步:安装wget工具,这个Linux应该 ...

  6. 关于LinQ中“from"前置的原因

    原文地址:http://blog.csdn.net/yuzifen/article/details/6754003 概括来说是:为了IDE的智能感知(Intelisence)功能,(或说为了进行类型推 ...

  7. iOS 开发 Tips

    1.MVVM 的优点 MVVM 兼容 MVC,可以先创建一个简单的 View Model,再慢慢迁移. MVVM 使得 app 更容易测试,因为 View Model 部分不涉及 UI. MVVM 最 ...

  8. PL/SQL数据类型

    在定义变量或常量时,必须要指定一个数据类型,PL/SQL是一种静态类型化的程序设计语言,静态类型化又称为强类型化,也就是说类型会在编译时而不是在运行时被检查,这样在编译时便能发现类型错误,以便增强程序 ...

  9. 复旦高等代数II(18级)每周一题

    本学期将继续进行高等代数每周一题的活动.计划从第一教学周开始,到第十五教学周结束,每周的周末公布一道思考题(预计15道),供大家思考和解答.每周一题将通过“高等代数官方博客”(以博文的形式)和“高等代 ...

  10. Vue学习(一)Vue目录结构

    安装教程网上一大把,可以自己搜索.记录下学习过程. 认识下Vue的目录结构,取自:https://www.cnblogs.com/dragonir/p/8711761.html vue 文件目录结构详 ...