题目链接:

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. 命令行启动tomcat,怎么配置

    进和你tomcat的安装目录进入里面bin目录下列可以直接在cmd中运行(要进入tomcat的bin目录),也可直接双击startup.bat 启动tomcatshutdown.bat 关闭tomca ...

  2. git merge 到 非当前 branch

    1. Add a remote alias for your local repository, ex: git remote add self file:///path/to/your/reposi ...

  3. 关于iOS自定义UITabBar的几种方法

    作为iOS开发最常用的两个多视图控制器 NavigationController 和 TabBarController 已经很强大了,基本上在大部分的应用中都能看到它们的影子.但是在使用的过程中,系统 ...

  4. Spring IoC容器的设计——BeanFactory应用场景2

    1.BeanFactory接口设计了getBean方法,这个方法是使用IoC容器API的主要方法,通过这个方法,可以取得IoC容器中管理的Bean,Bean的取得是通过指定名字来索引的. 2.如果需要 ...

  5. javascript构造函数小记

    function outer(){ function inner(){} return inner; } var a=outer(); var b=outer(); var c=new outer() ...

  6. Codevs 1080 线段树联系

    题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现在动态地提出一些问题和修改:提问的形式是求某一个特定的子区间[a,b]中所有元素的和:修改的规则是指定某一个格子x,加上或 ...

  7. 【风马一族_Python】 实施kNN算法

    一.在PyCharm 5.0.4(编写python程序的IDE) 编写kNN.py文件的代码 -------------------------- 1. kNN.py  运算符模块 --------- ...

  8. 《DNS加密更新》RHEL6

    DNS加密更新: 继DNS更新之后,现在又玩DNS加密更新,差不多. DNS更新指定一台主机,那台主机或多台主机来更新它,其他主机没权限. DNS加密更新,谁有密码说就可以更新. 做过上次的更新之后, ...

  9. JQ中的html()、text()、val()的用法

    <input type="text" val=""> 用val(); <sapn>你好</sapn>  用text() &l ...

  10. Mac上安装brew

    用过ubuntu系统的都知道,上面有一个命令apt-get 很方便可以快速的安装很多软件 特别lamp环境 都是一键安装. 在mac上也有类似的命令 brew brew用法可以访问官网地址  http ...