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 ...
随机推荐
- SQL2008R2 无法读取此系统上以前注册的服务器的列表--网上方法不可行
C:\Users\<username>\AppData\Local\Temp,然后创建两个命名为1和2的文件夹
- jQuery Validation让验证变得如此容易(二)
上一个例子我们是统一引用jquery.validate.js这样所有必填字段的提示信息都将是This field is required. 现在要改成动态提示,比如姓名如果为空则提示姓名不能为空,密码 ...
- jquery扩展方法
jquery插件的开发包括两种:一种是类级别的插件开发,即给jquery添加新的全局函数,相当于给jquery类本身添加方法. jquery的全局函数就是属于jquery命名空间的函数,另一种是对象级 ...
- infiniband学习总结
一.什么是infiniband InfiniBand架构是一种支持多并发链接的“转换线缆”技术,它是新一代服务器硬件平台的I/O标准.由于它具有高带宽.低延时. 高可扩展性的特点,它非常适用于服务器与 ...
- HtmlAgilityPack实战代码
C#采集代理服务器ip并设置IE代理--HtmlAgilityPack实战代码 今天在博客园看到一篇文章,说是C#采集某某的数据,其实做采集小软件很久了, 用的最好的还是HtmlAgilityPack ...
- Hibernate配置XML连接数据库
一.hibernate.cfg.xml 1.配置连接Oracle <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate ...
- NCache:最新发布的.NET平台分布式缓存系统
NCache:最新发布的.NET平台分布式缓存系统在等待Microsoft完成Velocity这个.NET平台下的分布式内存缓存系统的过程中,现在让我们将目光暂时投向其他已经有所建树的软件开发商.Al ...
- PHP5.3 里面数组的的实现方式
typedef struct _Bucket { char *key; void *value; struct _Bucket *next; } Bucket; typedef struct _Has ...
- HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP
dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...
- 欢迎关注我的微信公众账号:Linux技巧(微信号:irefactoring),一起学习Linux知识~~~
最近很少写博客了,一方面是近期工作比较繁忙,第二是把精力都放在GitHub和读源码去了. 申请了一个微信公众账号:Linux技巧 微信ID:irefactoring(意思是爱重构) ========= ...