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 ...
随机推荐
- N个骰子的点数和的概率分布
程序设计思路: 假设有n个骰子,关键是需要统计每个点数出现的次数.首先分析第一个骰子点数和有1到6的点数,计算出1到6的每种点数 的次数,并将结果用一个数组pos1记录.然后分析有两个骰子时, 点数为 ...
- 替换__thread的一种方式,实现TLS功能
TLS是由于多线程编程带来的产物,主要是为了解决线程资源局部化,具体内容网上有很多介绍.有很多地方已经支持了该功能,但有些地方没有,下面是GCC的一些介绍,反正具体看实际使用情况: 5.51 Thre ...
- Aspose.cells异步读写操作
public class AsyncExcel : Excel { static readonly object _objForlock = new object(); //public List&l ...
- hone hone clock人体时钟
hone hone clock是个十分有趣的人体时钟,这个时钟代码分两种一种是背景透明的,一种 是白色背景的,把你喜欢的代码添加到你的网页中适当位置即可.两种代码如下: <script char ...
- springMVC3学习(七)--Interceptor拦截器
Spring为我们提供了:org.springframework.web.servlet.HandlerInterceptor接口, org.springframework.web.servlet.h ...
- java分割excel文件可用jxl
excel导入是经常使用到的功能,如果文件数据量大的话还是建议分割后导入,java常用的API是poi和jxl,我采用的是jxl,那么让我们来看下怎么用jxl来实现分割. 需要在pom中导入jxl的包 ...
- ice 有道德的黑客!
ice 有道德的黑客! 在开始正文之前,请帮忙为当前 排名前 10 唯一的 .Net 开源软件 FineUI 投一票: 投票地址: https://code.csdn.net/2013OSSurve ...
- MFC控件(9):network address control
这个控件的名字倒是取的不错,一看就知道是让你输入IP地址或host name的. 不过一打开看到那控件的样子就完全是个Edit control.不过该控件对应的类也确实是继承自类CEdit. 先拖个控 ...
- 添加第三方类库造成的Undefined symbols for architecture i386:编译错误
1.原因: 如果是源码编译的话,一般就只某些头文件没有添加到src编译里面.但是对于添加库编译,一般是库的编译路径设置不正确(比如arm的版本.模拟器或者真机的不同版本库引用错误或者重复引用一起编译器 ...
- 第三届蓝桥杯Java高职组决赛第一题
题目描述: 看这个算式: ☆☆☆ + ☆☆☆ = ☆☆☆ 如果每个五角星代表 1 ~ 9 的不同的数字. 这个算式有多少种可能的正确填写方法? 173 + 286 = 459 295 + 173 = ...