大意: 给定数组$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. ES6,ES5,ES3,对比学习~

    在简书上看到一个博主写的文章,感觉很有用.留下:https://www.jianshu.com/p/287e0bb867ae  Excuse me?这个前端面试在搞事!https://zhuanlan ...

  2. Debian下安装中文包和输入法

    Debian下安装中文包和输入法 #aptitude install locales(没有aptitude的话可以先安装apt-get insall aptitude )  #dpkg-reconfi ...

  3. 原生js获取 一个dom元素距离页面可视区域的位置值 -- getBoundingClientRect

    getBoundingClientRect() 这个方法返回一个矩形对象,包含四个属性:left.top.right和bottom.分别表示元素各边与页面上边和左边的距离. var box=docum ...

  4. Python实现图像直方图均衡化算法

    title: "Python实现图像直方图均衡化算法" date: 2018-06-12T17:10:48+08:00 tags: [""] categorie ...

  5. Delphi获取本机所有的IP

    安装Indy uses  IdStackWindows; var Isw:TIdStackWindows; slist:TStringList; begin Isw:=TIdStackWindows. ...

  6. java基础语法-char数据类型

    1.java中的char描述了UTF-16中的一个代码单元,因此对于基本的多语言层面可以随意的用char表示 ;//int值只能在0到65535即0000到FFFF 对于其他16个代码级别(两个代码单 ...

  7. tab切换的效果——仿照今日头条APP的切换效果

    说点废话;不知道是哪一版本起头条的tab切换效果就变了,一直琢磨着这个事,去度娘那里也没有什么结果:正好这两天有空就尝试做了一下:用前端的技术来实现: 先看效果吧:上面的tab随着slide滑动,上面 ...

  8. python模块部分----模块、包、常用模块

    0.来源:https://www.cnblogs.com/jin-xin/articles/9987155.html 1.导入模块 1.1模块就是一个python文件,模块名是文件名 1.2导入模块的 ...

  9. 单点登录系统和CAS的简介

    ---恢复内容开始--- 什么是单点登录? 单点登录(Single Sign On),简称为SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以 ...

  10. 自制操作系统Antz(13) 显示图片

    显示图片只是在多媒体课上看着bmp格式图片的突发奇想,然后就实现在了我自己的操作系统 Antz系统更新地址 Linux内核源码分析地址 Github项目地址 效果图: 显示图片的原理 在之前显卡操作时 ...