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& ...
随机推荐
- 下拉框的选择跳转事件(jqure)
<select id="coupontype"> <option value="@CouponType.全部" href="@Url ...
- 20150511---Timer计时器(备忘)
private void timer1_Tick(object sender, EventArgs e) { TimeSpan ts = , , ); string str = ts.Hours + ...
- 汇编中PTR常见的几种用法
汇编中PTR的用法确实是令人比较头疼的,我特意搜集了一些PTR的应用实例,可以从例子中揣摩出规律: 1.MOV WORD PTR [DI],OFFSET BUF1 2.SUB BYTE ...
- void指针
指针有两个属性:指向变量/对象的地址 和长度 但是指针只存储地址,长度则取决于指针的类型 编译器根据指针的类型从指针指向的地址向后寻址 指针类型不同则寻址范围也不同,比如: int*从指定地址向后寻找 ...
- iOS 系统架构及常用框架
1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...
- ADO.NET笔记——SQL注入攻击
相关知识: 可以通过字符串的拼接来构造一个SQL命令字符串,但是SQL命令字符串的拼接确是造成“SQL注入攻击”的重要原因. 考虑下列例子:从ProductCategory表中检索出Name为“Bik ...
- 基于ArcGIS API for JavaScript的统计图表实现
感谢原作者分享:https://github.com/shevchenhe/ChartLayer,在使用的过程中,需要自己进行调试修改,主要还是_draw函数,不同的ArcGIS JS API函数有差 ...
- SQL server数据库内置账户SA登录设置
SQL server数据库内置账户SA登录不了 设置SQL Server数据库给sa设置密码的时候 提示18456 解决步骤: 第二步:右击sa,选择属性: 第三步:点击状态选项卡:勾选授予 ...
- C# WinForm设置TreeView选中节点
这里假定只有两级节点,多级方法类似.遍历节点,根据选中节点文本找到要选中的节点.treeView.SelectedNode = selectNode; /// <summary> /// ...
- js日期相关函数总结分享
一个倒计时程序,因为经常要在手机端访问,所以没有引用jquery,对于用习惯jquery的我还真不习惯. 下面简单说明js日期相关函数,并说明实现倒计时的原理 var dateTo=new Date( ...