【模拟】Codeforces 671B Robin Hood
题目链接:
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的更多相关文章
- codeforces 671B Robin Hood 二分
题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CodeForces 672D Robin Hood
思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大 ...
- 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 ...
- 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】671 B. Robin Hood
[题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...
- 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 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 ...
- Codeforces Round #352 (Div. 1) B. Robin Hood
B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...
随机推荐
- 用GitHub Pages免费空间搭建Blog
前言 其实之前就知道可以用GitHub Pages搭建静态博客,不过之前一直忙着爬手册撸代码==,昨天终于把前端各种手册里的入门教程撸的差不多了(CSS布局撸的我要吐了好嘛),于是把代码什么的放一 ...
- JavaScript正则验证数字、英文、电话号、身份证号、邮箱地址、链接地址等
验证是否为数字:/^[0-9]*$/验证是否为汉字:/^[\u4e00-\u9fa5],{0,}$/验证x-y位的数字:/^\d{x,y}$/验证由26个英文字母组成的字符串:/^[A-Za-z]+$ ...
- hibernate通过判断参数动态组合Hql语句,生成基本通用查询
// public List find(Station entity) { List reuslt = null; // 字符串辅助类 StringBuffer hql = new StringBuf ...
- 如何将硬盘GPT分区转换为MBR分区模式
现在新出的笔记本普遍自带WIN8系统,硬盘分区一般都采用GPT格式,但是包括WIN7及以下的系统都无法安装在GPT格式的硬盘上,因此,如果我们需要安装WIN7系统,需要将硬盘分区从GPT转换成MBR格 ...
- 【转】iOS- 详解文本属性Attributes
原文: http://www.cnblogs.com/qingche/p/3574995.html?utm_source=tuicool 1.NSKernAttributeName: @10 调整字句 ...
- Strut2 采用token机制防御CSRF同时也可以防止表单重复提交
一 未配置Struts2 token的情况下测试 1.从表单提交数据,可以从下图看出,快速点击保存按钮,请求提交了两次 2.检查post提交的数据中未含有token参数 3.查看数据列表,有重复数据 ...
- 大规模字符串检索-压缩trie树
本文使用压缩trie树实现字符串检索的功能.首先将字符串通过编码转化为二进制串,随后将二进制串插入到trie树中,在插入过程中同时实现压缩的功能. 字符编码采用Huffman,但最终测试发现不采用Hu ...
- centos7上安装与配置Tomcat7(整理篇)
1.检查tomcat7是否已经安装 rpm -qa | grep tomcat ps -ef | grep tomcat 第一条命令查看是用rpm安装过tomcat,由于我们倾向于安装解压版的tomc ...
- git config找到下载的路径URL
有时想在别的机器上下载自己的上传的源代码,发现git不像svn那么方便一眼就能看到下载地址,这两天在网上查了一下,终于找到方法了. 一般本地的github目录下总会有一个.git目录,点击进入,可 ...
- extern "C" {} 来沟通C和C++
比如说你用C++开发了一个DLL库,为了能够让C语言也能够调用你的DLL输出(Export)的函数,你需要用extern "C"来强制编译器不要修改你的函数名. 通常,在C语言的头 ...