大意: 给定数组$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. 【电子书分享】Learning PySpark下载,包含pdf、epub格式

    <Learning PySpark>电子书下载链接: 链接:http://pan.baidu.com/s/1skAC6LZ 密码:kbse,包括pdf.epub格式: (--学习愉快--)

  2. World is Exploding (容斥 + 统计)

    题意:满足题目中的式子,a < b && c < d && Va < Vb && Vc > Vd 思路:先求不讨论位置重合的情况 ...

  3. centos7将网卡名字改成eth样式

    ll /etc/sysconfig/grub lrwxrwxrwx 1 root root 17 Jun 12 2016 /etc/sysconfig/grub -> /etc/default/ ...

  4. post请求中body数据类型

    1.application/json:json格式,如下: {"input1":"xxx","input2":"ooo" ...

  5. sqflite插件简单使用 key======================

    https://blog.csdn.net/weixin_34183910/article/details/86029912 https://blog.csdn.net/u013255127/arti ...

  6. ajax的三次封装简单概况

    原生ajax:                readyState         准备状态                status             页面状态               ...

  7. git-bash的alias别名设置

    正常需要设置别名时,直接使用 alias gs="git status" 输入上边的命令之后,就可以使用gs(命令)代替git status(命令),这是一种设置别名简化输入,提升 ...

  8. 0x11栈之Editor

    参考链接:https://blog.csdn.net/SSLGZ_yyc/article/details/81700623 对顶栈的思想: 建立两个栈,栈A存储从序列开头到当前光标的位置的一段序列,栈 ...

  9. [darknet]查看错误结果 sight of wrong

    import os import numpy import cv2 bad_label_file = open("bad_valid.list",'r') names = [] f ...

  10. Python3 tkinter基础 Radiobutton 设置相同的value值,产生连锁效果

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...