CodeForces 672D Robin Hood
思维。
当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$。我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算。
需要计算的就是,将最小的几个数总共加$k$次,最小值最大会是多少,以及将最大的几个数总共减$k$次,最大值最小可能是多少。两者相减就是答案。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
int n;
LL k,a[maxn],ans,sum;
LL p[maxn],c[maxn];
LL ans1,ans2; void work()
{
for(int i=;i<=n;i++) p[i]=p[i-]+a[i];
for(int i=;i<=n;i++) c[i]=(i-)*a[i]-p[i-];
int pos; for(int i=;i<=n;i++) if(c[i]<=k) pos=i;
LL tmp=k; tmp=tmp-c[pos]; ans1=a[pos];
ans1=ans1+tmp/pos; memset(p,,sizeof p); memset(c,,sizeof c);
for(int i=n;i>=;i--) p[i]=p[i+]+a[i];
for(int i=n;i>=;i--) c[i]=p[i+]-(n-i)*a[i];
for(int i=n;i>=;i--) if(c[i]<=k) pos=i;
tmp=k; tmp=tmp-c[pos]; ans2=a[pos];
ans2=ans2-tmp/(n-pos+); printf("%lld",ans2-ans1);
} int main()
{
scanf("%d%lld",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum=sum+a[i];
}
sort(a+,a++n); if(sum%n==)
{
LL x=; sum=sum/n;
for(int i=;i<=n;i++) x=x+abs(sum-a[i]);
if(k>=x/) printf("0\n");
else work();
} else
{
LL tt=a[n];
a[n]=a[n]-sum%n;
LL x=; sum=sum/n;
for(int i=;i<=n;i++) x=x+abs(sum-a[i]);
if(k>=x/) printf("1\n");
else { a[n]=tt; work(); }
} return ;
}
CodeForces 672D Robin Hood的更多相关文章
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CF 672D Robin Hood(二分答案)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【模拟】Codeforces 671B Robin Hood
题目链接: http://codeforces.com/problemset/problem/671/B 题目大意: N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人 ...
- codeforces 671B Robin Hood 二分
题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...
- codeforces 672D D. Robin Hood(二分)
题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【15.93%】【codeforces 672D】Robin Hood
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 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】671 B. Robin Hood
[题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...
随机推荐
- centos安装及配置
centos安装 从网上下载最新的版本,用ultraiso做好系统盘,启动.安装过程没有什么可以说的,与其他发行版有点不同的是需要将安装包放到u盘根目录下(u盘容量最好大点,8~16G最好).开始没注 ...
- VMware NAT方式 CentOS 6.8配置静态IP
一.打开虚拟机设置,配置网络连接,如下图 二.编辑 /etc/sysconfig/network,以配置网关 vim /etc/sysconfig/network NETWORKING=yes HOS ...
- Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
The error that occurs is:Fatal error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY' in /w ...
- ios学习笔记第三天之UI控件
- revel框架教程之缓存和Job
Go语言实战 - revel框架教程之缓存和Job 所有的网站应该都会有一个非常简单的需求,首页一秒之内打开. 满足的方式主要有两种: 页面静态化,效果最好,对服务器基本没负担,只要带宽足够就好了 ...
- Python:开发Sublime插件,方便PHP开发
Python:开发Sublime插件,方便PHP开发 背景 最近在学习PHP,开发环境选择了Sublime2,开发过程发现执行PHP程序非常不方便,需要自己在浏览器中输入路径以进行调试,这点不如Dre ...
- 如何优化运行在webkit上的web app
如何优化运行在webkit上的web app 近来公司有个web app 项目运行在移动版android系统上,发现在电脑上跑的很流畅的web页面在移动版webkit上非常不流畅.根本无法和nativ ...
- .Net项目框架
摘要:本文描述了在用VS.NET进行B/S开发时采用的框架结构,一般建立类库项目和Web项目,在Web基本aspx页面类中调用类库中方法,同时在aspx页面类中不需要写任何对数据库操作的SQL代码,便 ...
- mybatis逆向工程生成代码
1 什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po. ...
- 谷歌、flick网站图片 一次性下载 javaWeb项目 多线程下载,
源码下载:http://download.csdn.net/detail/liangrui1988/5760473