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 ...
随机推荐
- nuget包管理nuget服务器发布包时出现请求报错 406 (Not Acceptable)
在window服务器上部署nuget服务器时,发布包时出现请求报错 406 (Not Acceptable) 验证用户名.密码正确的情况下,还是出现上面错误.后面跟踪服务器日志,发现window\te ...
- CentOS 搭建git服务
git服务器的搭建是非常简单的. 1. 安装git yum install git 2. 创建用户git groupadd git adduser git -g git passwd git 3. 创 ...
- meterpreter 持久后门
创建持久后门 当成功获取目标系统的访问权限后,需要寻找方法来恢复与目标主机的连接,而无需再进入目标系统.如果目标用户破坏了该连接,例如重新启动计算机,此时使用后门将允许自动重新与目标系统建立连接.为了 ...
- Sitecore8.2 GeoIP - 在8.2的引擎盖下发生了什么?
访客互动 - 访客会话的开始 访问者访问Sitecore网站,这被视为一种新的互动.Sitecore对交互的定义是“......联系人与品牌联系的任何一点,无论是在线还是离线”.在我们的例子中,这是网 ...
- 日常开发工作常用linux命令
:wq 保存退出 :q! 强制退出 vi 查看 vim 编辑 rpm -qa|grep jdk 命令查看当前的jdk情况 yum -y remove java java-1.7.0-openjdk* ...
- (cvpr2019 ) Better Version of SRMD
SRMD的内容上篇,已经介绍,本文主要介绍SRMD的升级版,解决SRMD的诸多问题, 并进行模拟实验. 进行双三次差值(bicubic)===>对应matlab imresize() %% re ...
- npm install --save 、--save-dev 、-D、-S 的区别
备注:<=> 意为等价于: 1.npm install <=> npm i --save <=> -S --save-dev <=> -D npm ...
- 7.JAVA基础复习——JAVA中的设计模式单例模式
设计模式:是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 总体来说设计模式分为23种三大类: 创建型模式,共 ...
- BIOS备忘录之SPI(fingerprint)设备
Reset和INT信号使用的是GPIO功能,需要显式的使用(INT信号使用了GPIO的int number,RST信号使用了GPIO的absolute number): 问题举例 漏电导致功能异常:在 ...
- Docker Registry私有仓库搭建
部署registry 准备一个registry.mydocker.com 的证书 对私有registry取名registry.mydocker.com 目录规划 仓库数据目录:/data/docker ...