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 ...
随机推荐
- SugarSync网盘之NSDateFormatter
NSDateFormatter 在获取sugarsync网盘的accessToken时候,得到了过期时间.但是这里的过期时间采用的是世界标准时间UTC,而该网盘是国外的,所以在国内显示的时间就不对了, ...
- 【转】几点 iOS 开发技巧
[译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...
- apache Alias使用问题
今天在配置apache的过程中,使用了Alias,但是由于配置错误导致403 forbidden错误,不能正常访问. 首先理解一下Alias,Alias就是别名的意思,假如我的项目目录在/home/w ...
- Python实现LDAP用户名密码验证
网上借鉴了不少东西,下面是python代码,备份后用. 思路,因为每个用户的组都不一样,这样就导致了dn不一致的情况, 据需要先根据用户名获取该用户的dn,然后再bind用户名和密码进行验证. 反正是 ...
- wpf中xps文档合并功能实现
跟着上一篇的xps文档套打的文章,近期一直在研究xps打印技术,其中用户提到了一个需求,要求能够多页面进行打印,我的想法是,先生成xps文件,然后将文件读取出来以后,合并成一个文件来处理. 如果要是直 ...
- 迟到的 WPF 学习 —— 入门
之所以说"迟到的",是因为我太晚才开始学习 WPF 了,之前 WPF 刚发布的时候有过粗浅了解,那时的 WPF 还非常简陋,VS 提供的内置控件十分匮乏,让我这样的非常依赖 Win ...
- python学习之路二(字符串,字典,序列和元组)
# -*- coding: utf-8 -* ''' Created on 2013-7-26 @author: lixingle ''' #!/usr/bin/python import math# ...
- javascript闭包1
javascript闭包 在学习javascript闭包之前,需要先了解一下"作用域链". 每一段javascript代码都有一个与之关联的作用域链(scope chain),这个 ...
- 没有标准化字符串的后果(IOS)
对于NSString肯定会经常用到,谈谈最近在项目中遇到的一个奇特的现象.如下:我们知道文件系统的命名都是用的字符串,比如你给文件取名“a.pdf”,然后保存文件后,那个文件的名字就真的是 " ...
- MyEclipse取消Show in Breadcrumb的方法
eclipse中的Show in Breadcrumb是快速导航条,可以清晰的看到我们当前的类,属性或方法的导航 定位. 如果不喜欢的话,取消掉的方法如下: Window -> Customiz ...