题目链接:

  http://codeforces.com/problemset/problem/671/B

题目大意:

  N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人。总共K次,问最后贫富差距。

  钱被拿走是立刻结算,所以可能拿走后这个人变最穷的人再还回去。

  最富或最穷的人可能有多个,随机选择,并且不会影响最终答案。

  (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109)

题目思路:

  【模拟】

  直接排序离散化数据,之后模拟就行。

  细节挺多的要处理清楚。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 500004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
double anss;
LL b[N];
LL sum;
struct xxx
{
LL num,large;
}a[N];
bool cmp(LL aa,LL bb)
{
return aa<bb;
}
int work()
{
int i;
LL k,maxx,minn;
k=m;i=lll;
while(i && k>=a[i].num*(a[i].large-a[i-].large))
{
k-=a[i].num*(a[i].large-a[i-].large);
a[i-].num+=a[i].num;
a[i--].num=;
if(!i)return (sum%n!=);
}
maxx=a[i].large-k/a[i].num;
a[i+].large=maxx,a[i+].num=a[i].num-k%a[i].num;
a[i].large=maxx-;a[i].num-=a[i+].num;
lll=i+;
//==========================================
k=m;i=;
while(k> && k>=a[i].num*(a[i+].large-a[i].large) && a[i].large<maxx)
{
if(a[i].num==){i++;continue;}
k-=a[i].num*(a[i+].large-a[i].large);
a[i+].num+=a[i].num;
a[i++].num=;
}
if(a[i].large>=maxx)return (sum%n!=);
if(a[i+].num==)
minn=a[i].large+k/a[i].num;
else
minn=a[i].large+k/(a[i].num);
return maxx-minn;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
scanf("%d",&m);
sum=;mem(a,);
for(i=;i<=n;i++)
{
scanf("%d",&b[i]);
sum+=b[i];
}
sort(b+,b++n,cmp);
lll=;a[lll].large=b[];a[lll].num=;
for(i=;i<=n;i++)
{
if(b[i]==b[i-])a[lll].num++;
else a[++lll].large=b[i],a[lll].num=;
}
j=work();
printf("%d\n",j);
}
return ;
}
/*
// //
*/

【模拟】Codeforces 671B Robin Hood的更多相关文章

  1. codeforces 671B Robin Hood 二分

    题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...

  2. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. CodeForces 672D Robin Hood

    思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大 ...

  4. 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 ...

  5. 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 ...

  6. 【CodeForces】671 B. Robin Hood

    [题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #352 (Div. 1) B. Robin Hood

    B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...

随机推荐

  1. HDU 4971 - A simple brute force problem【最大权闭合图】

    有n(20)个工程,完成每个工程获得收益是p[i],m(50)个需要解决的难题,解决每个难题花费是c[i] 要完成第i个工程,需要先解决ki个问题,具体哪些问题,输入会给出 每个难题之间可能有依赖关系 ...

  2. (转)高性能I/O模型

    本文转自:http://www.cnblogs.com/fanzhidongyzby/p/4098546.html 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO ...

  3. JQ 日期格式化

    将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...

  4. br与p标签区别

    首先,相同之处是br和p都是有换行的属性及意思其次,区别<br />是只需一个单独使用,而<p>和</p>是一对使用再次,br标签是小换行提行,p标签是大换行(分段 ...

  5. C# Activator.CreateInstance()

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  6. CI 笔记3 (easyui 和 js 排错)

    开始使用easyui作为后台框架,做layout布局,浏览器白屏,报告异常,除错过程步骤如下: 浏览器加载easyui后,布局的north,south,west,east,center,没有起作用,在 ...

  7. 进程识别号(PID)的理解

    PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...

  8. 线程同步(AutoResetEvent与ManualResetEvent)

    前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...

  9. java_设计模式_适配器模式_Adapter Pattern(2016-08-09)

    概念 将一个接口转换成客户希望的另外一个接口.(该模式使得原本不兼容的类可以一起工作). UML图 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式. (1)对象的适配器模式结构图 (2)类 ...

  10. Android JIN返回结构体

    一.对应类型符号 Java 类型     符号 boolean     Z byte     B char     C short     S int     I long     J float   ...