题目链接:

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. 苹果系统开发中的混合编程(2):Swift和C的相互调用

      在进行Swift和C之间的相互调用时,有必要先了解一下两种语言之间的类型转换关系:   C 类型 Swift 类型 bool CBool char, signed char CChar unsig ...

  2. 分享10款激发灵感的最新HTML5/CSS3应用

    1.HTML5/CSS3实现iOS Path菜单 菜单动画很酷 Path菜单相信大家都不陌生吧,它在iOS中非常流行,今天我们要分享的菜单就是利用HTML5和CSS3技术来模拟实现这款iOS Path ...

  3. ThinkPHP中的模型

    ThinkPHP中的模型 1.什么是模型(Model) 模型表示企业数据和业务规则,实际项目开发中,主要实现与数据库进行操作. 2.模型的定义规则 模型类的命名规则是除去表前缀的数据表名称,采用驼峰法 ...

  4. Aborting a running program

    In the event that a calculation appears to be running excessively long, one can abort thecalculation ...

  5. C#工具介绍

    VisualStudio是微软的官方提供的.NET开发工具. 除了VisualStudio外,还有一些开源的.NET开发IDE. MonoDevelop.SharpDevelop等. 开发未必需要使用 ...

  6. Redis源码研究--双向链表

    之前看的内容,占个位子,以后补上. ----------8月4日--------------- 双向链表这部分看的比较爽,代码写的中规中矩,心里窃喜,跟之前学的<数据结构>这本书中差不多. ...

  7. Discuz 3.X 门户文章插入图片自动添加 alt 标签

    最近用 Discuz 搭建了个网站--儿童安全座椅网(www.bbseat.com.cn),用到了门户功能,不得不说Discuz 的功能还是非常强大的,但在使用过程中发现在发表文章时添加了图片却不能像 ...

  8. python: 实现sha1小工具

    File1: sha1.py File2: sha1.bat ------------------ File1: sha1.py import hashlib import os,sys def Ca ...

  9. C#生成随机字符串

    //<summary> ///得到随机字符. ///</summary> ///<param name="intLength">Length o ...

  10. Spark 大数据平台

    Apache Spark is an open source cluster computing system that aims to make data analytics fast - both ...