题目链接:

http://codeforces.com/contest/672/problem/D

题意:

给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取消操作,现在给你操作的次数,问操作之后最大数减最小数的最小值。

题解:

问题要求得是min(k次操作之后的最大数-k次操作之后的最小数),而这两个数可以独立求出来,我们先用二分求k次操作之后的最小数的最大取值,然后,再用二分求k次操作之后的最大数的最小可能取值。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f;
typedef __int64 LL; LL arr[maxn];
int n, k,Ma,Mi; bool ok1(int x) {
LL cnt = ;
for (int i = ; i < n; i++) {
cnt += max((LL), x - arr[i]);
}
if (cnt <= k) return true;
return false;
}
int bs1() {
int l = Mi,r = Ma+;
while (l + < r) {
int mid = l + (r - l) / ;
if (ok1(mid)) l = mid;
else r = mid;
}
return l;
}
bool ok2(int x) {
LL cnt = ;
for (int i = ; i < n; i++) {
cnt += max((LL), arr[i]-x);
}
if (cnt <= k) return true;
return false;
}
int bs2() {
int l = Mi-, r = Ma;
while (l + < r) {
int mid = l + (r - l) / ;
if (ok2(mid)) r = mid;
else l = mid;
}
return r;
} void init() {
Mi = INF; Ma = -;
} int main() {
while (scanf("%d%d", &n, &k) == && n) {
init();
LL sum = ;
int mi, ma;
for (int i = ; i < n; i++) {
scanf("%I64d", arr + i);
sum += arr[i];
Ma = max((LL)Ma, arr[i]), Mi = min((LL)Mi, arr[i]);
}
mi = bs1();
ma = bs2();
int ans = ;
if (mi >= ma) {
if (sum%n) ans = ;
}
else {
ans = ma - mi;
}
printf("%d\n", ans);
}
return ;
}

Codeforces Round #352 (Div. 2) D. Robin Hood的更多相关文章

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

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

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

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

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

  5. Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)

    题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...

  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. 2) ABCD

    Problems     # Name     A Summer Camp standard input/output 1 s, 256 MB    x3197 B Different is Good ...

  8. Codeforces Round #352 (Div. 2)

    模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...

  9. Codeforces Round #352 (Div. 2) (A-D)

    672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...

随机推荐

  1. SQL 复制订阅 异常后 强制删除

    最近做数据库同步备份工作,将 主库 通过SQLService 自带的 [复制] 订阅出去后,因为 订阅方(从库) 发生异常,主库 无法确定 从库的订阅,就想清理了,订阅重新做同步,结果.....主库上 ...

  2. 【Linux C中文函数手册】之 内存和字符串函数

    内存和字符串函数 1) bcmp 比较内存内容 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp表头文件 #include<stri ...

  3. Memcached解决单台服务器故障问题

    <beitmemcached> <add key="name1" value="server1:port" /> <add key ...

  4. UI1_UIButton

    // // AppDelegate.m // UI1_UIButton // // Created by zhangxueming on 15/6/30. // Copyright (c) 2015年 ...

  5. 2014028-jQuery与正则表达式[转]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. [javascript|基本概念|一元操作符]学习笔记

    只操作一个值的操作符 递增/递减操作符 前置型/后置型 前置型:操作符位于操作数前面 e.g.: var a = 30; ++a; 等同于 var a = 30; a = a + 1; --> ...

  7. 1 。 LightOJ 1234 打表法(数据太大,把数据缩小100倍)

    原题链接http://acm.hust.edu.cn/vjudge/contest/121397#problem/A Description In mathematics, the nth harmo ...

  8. cadence16.3破解方法

    今天安装cadence16.3,安装了两遍都失败了,百思不得其解,结果总是出现在license上,如下图所示: 后面当然就只能启动demo版了,于是网上找,参照以前古老的法子,终于解决问题! 首先,开 ...

  9. Ubuntu14.04 开启MySQL的remote access

    一旦开启MySQL的remote access, 即可实现在windows系统下连接ubuntu下的mysql 步骤如下: sudo vim /etc/mysql/my.cnf bind-addres ...

  10. centos下redis和nginx软件的安装

    我们这章以redis和nginx软件为例,介绍一下centos下软件的安装. 需要软件包(可以先下载好复制到centos指定目录下,也可直接用wget命令获取): nginx-1.7.9.tar re ...