Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接:
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #352 (Div. 1) B. Robin Hood
B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...
- Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- 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 ...
- Codeforces Round #352 (Div. 2) ABCD
Problems # Name A Summer Camp standard input/output 1 s, 256 MB x3197 B Different is Good ...
- Codeforces Round #352 (Div. 2)
模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...
- Codeforces Round #352 (Div. 2) (A-D)
672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...
随机推荐
- 2015影响因子Excel版
现在终于有影响因子2015的Excel版了,这个版本除了还包括杂志全称和缩写等基本信息还,包括中科院分区, 应该是迄今2015影响因子最奢华的版本了吧. 看了这个版本,发现中国的SCI杂志还是不少的, ...
- socket.io问题,io.sockets.manager.rooms和io.sockets.clients('particular room')这两个函数怎么用?
为什么我用nodejs用这个两个函数获取都会出错呢?是不是socket的api改了?请问现在获取房间数和当前房间的客户怎么获取?什么函数?谢谢!!急求! 网友采纳 版本问题.io.socket ...
- c# SQL CLR 之一
CLR就是公共运行时,本文就对c#编写SQL StoredProcedures的过程进行简单讲解. [步骤] 2. 3. 7.打开设置 8. 注意删除方式:注意删除Assembly时,一定要先把引用此 ...
- web HTML5 调用摄像头的代码
最近公司要求做一个在线拍照的功能,具体代码如下: <html> <head> <title>html5调用摄像头拍照</title> <style ...
- java基础学习01
学习主意:边看视频,边编写代码,学习成果体现在代码
- jdk、maven配置
JDK环境变量配置1.新建系统变量 1)变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.7.0_15 2)变量名:CLASSPATH 变量值:.;%JAVA_ ...
- 近期H5项目开发小结
前言:2016差不多又过了半啦,最近参与了公司好几个h5项目(严格来说,也只能算是推广页面活动).主要是新品牌的推广需要,当然也有给公司以前老客户做的案例.今天主要总结下为新品牌开发的2个h5推广:就 ...
- CSS3 animation小动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 使用VideoView播放、暂停、快进视频
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&qu ...
- php新手:XAMMP打开开源php代码
1.启动XAMPP 打开XAMPP启动 Apache 和 MySql 如果发现默认的80端口被IIS占用了 请参考 这个 如何改变apache被占用的端口 2.将源代码复制到 磁盘(XAMPP安装目 ...