Robin Hood CodeForces - 672D (二分)
大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差.
二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, 否则为$max(1,R-L)$
#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k, a[N];
int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) a[i]=rd();
int l=0,r=1e9,L,R;
while (l<=r) {
ll d = 0;
REP(i,1,n) if (a[i]<mid) d+=mid-a[i];
if (d<=k) L=mid,l=mid+1;
else r=mid-1;
}
l=0,r=1e9;
while (l<=r) {
ll d = 0;
REP(i,1,n) if (a[i]>mid) d+=a[i]-mid;
if (d<=k) R=mid,r=mid-1;
else l=mid+1;
}
int ans = max(0,R-L);
ll sum = 0;
REP(i,1,n) sum+=a[i];
if (sum%n) ans=max(ans,1);
printf("%d\n", ans);
}
Robin Hood CodeForces - 672D (二分)的更多相关文章
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 672D D. Robin Hood(二分)
题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 ...
- CF 672D Robin Hood(二分答案)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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. 1) B. Robin Hood (二分)
B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【15.93%】【codeforces 672D】Robin Hood
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【CodeForces】671 B. Robin Hood
[题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...
随机推荐
- 蓝桥杯c/c++省赛真题——日志统计
标题:日志统计 [问题描述]小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是:ts id 表示在ts时刻编号id的帖子收到一个" ...
- Kindle:自动追更之发送邮件
@echo off setlocal enabledelayedexpansion set from=Kindlekindle设置好信任的邮箱set pw=密码 set to=Kindle邮箱 cd ...
- react问题解决的一些方法
原文链接: https://segmentfault.com/a/1190000007811296?utm_source=tuicool&utm_medium=referral 初学者对Rea ...
- 复制ASP.NET的ASHX、aspx文件的注意事项
在复制ashx文件后,需要在夫指出的文件上右键——打开方式——点击“”源代码文本编辑器“” ashx在你新建的时候它已经指定了执行的命名空间你后面再去修改文件名或者里边的类名它的指定也不会变 这是因 ...
- 2018-2019-2 网络对抗技术 20165335 Exp4 恶意代码分析
实验内容: 一.使用schtacks进行系统运行监控,使用sysmon工具监控系统的具体进程,使用各种工具进行监控,并针对软件的启动回连,安装到目标机,以及其他的控制行为的分析,同时,对主机的注册表, ...
- Goland开发工具安装教程
开发工具: 文本类的编辑器:记事本,notepad,sublime text,atom... 通过命令执行程序 IED:集成开发环境(integrated development environmen ...
- 编译原理 #01# 简易词法分析器(js实现)
// 实验存档 效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- 【题解】Luogu P4396 [AHOI2013]作业
原题传送门 最快的解法好像是cdq,但窝只会莫队+线段树/树状数组的做法 题目要我们求1.在区间[l,r]中值域在[a,b]中有多少个数2.在区间[l,r]中值域在[a,b]中有多少个不同数 一眼就看 ...
- 机器学习总结(二)bagging与随机森林
一:Bagging与随机森林 与Boosting族算法不同的是,Bagging和随机森林的个体学习器之间不存在强的依赖关系,可同时生成并行化的方法. Bagging算法 bagging的算法过程如下: ...
- java @Service 引入什么包
maven中加入 <dependency> <groupId>org.springframework</groupId> <artifactId>spr ...