CF 352 D 罗宾汉发钱 模拟题+贪心
1 second
256 megabytes
standard input
standard output
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of thei-th person.
Print a single line containing the difference between richest and poorest peoples wealth.
4 1
1 1 4 2
3 1
2 2 2
Lets look at how wealth changes through day in the first sample.
- [1, 1, 4, 2]
- [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
题意:给出n个人,每个人有一个初始的价值ai,给出k个操作,每个操作必须从当前最富有的那个人拿一个单位的价值给当前最穷的人,问在经历k次操作之后,贫富差距多大
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=+; ll a[maxn];
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
ll sum=;
for(int i=;i<n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
sort(a,a+n); int maxn=a[n-],minn=a[],tp=k;
for(int i=n-;i>=;i--)
if(maxn>a[i])
{
if((n--i)*(maxn-a[i])<=tp)//乘法int会爆
{
tp-=(n--i)*(maxn-a[i]);
maxn=a[i];
}//能全部减下去
else
{
maxn-=tp/(n--i);
break;
}//无法进一步减下去
} tp=k;minn=a[];
for(int i=;i<n;i++)
if(minn<a[i])
{
if(i*(a[i]-minn)<=tp)
{
tp-=i*(a[i]-minn);
minn=a[i];
}
else
{
minn+=tp/i;
break;
}
} if(minn>=maxn) printf("%d\n",sum%n==?:);
else printf("%d\n",maxn-minn);
}
return ;
}
分析:一开始被吓到了,,后来稍微看了下题解,再自己仔细做了下,,还是觉得很水的.....
建立模型:将所有人的财富按从小到大排序,然后首先从最右边开始切,最多切k个格子,算出
切了后的最大的值maxn,然后再从左往右补,算出补了后的最小的值minn,如果maxn>=minn,说明穷人与富人之间出现了反转,则要特判一下所有才丰富能否均分。
CF 352 D 罗宾汉发钱 模拟题+贪心的更多相关文章
- cf 443 D. Teams Formation](细节模拟题)
cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...
- CF 1008B Turn the Rectangles(水题+贪心)
There are n rectangles in a row. You can either turn each rectangle by 90 degrees or leave it as it ...
- Codeforces Round #375 (Div. 2) Polycarp at the Radio 优先队列模拟题 + 贪心
http://codeforces.com/contest/723/problem/C 题目是给出一个序列 a[i]表示第i个歌曲是第a[i]个人演唱,现在选出前m个人,记b[j]表示第j个人演唱歌曲 ...
- CF 420B Online Meeting 模拟题
只是贴代码,这种模拟题一定要好好纪念下 TAT #include <cstdio> #include <cstring> #include <algorithm> ...
- ACdreamoj(1105)模拟题
题意:射一次激光最多能够攻击到几个敌人(因为激光非常强大,能够在击中敌人后穿过它,而瑶瑶自己的坦克因为有特殊装置,所以不会被激光击中.激光也会直接穿过它) . 表示此处为空地 * 表示此处为障碍(激光 ...
- NOIP模拟题汇总(加厚版)
\(NOIP\)模拟题汇总(加厚版) T1 string 描述 有一个仅由 '0' 和 '1' 组成的字符串 \(A\),可以对其执行下列两个操作: 删除 \(A\)中的第一个字符: 若 \(A\)中 ...
- 8.22 NOIP 模拟题
8.22 NOIP 模拟题 编译命令 g++ -o * *.cpp gcc -o * *.c fpc *.pas 编译器版本 g++/gcc fpc 评测环境 位 Linux, .3GHZ CPU ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- poj 1888 Crossword Answers 模拟题
Crossword Answers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 869 Accepted: 405 D ...
随机推荐
- selenium Grid2环境搭建和基本使用
Selenium Grid简介 利用Selenium Grid可以使主节点(hub)的测试用例在不同主机即分支点(node)运行.可以使一份测试用例在不同环境下(操作系统.浏览器)执行自动化测试.Se ...
- typescript中新增的基本数据类型
javascript中有7种数据类型,分别是:boolean,number,string,null,undefined和object,以及在es6中新增的一种类型 symbol.而typescript ...
- Python 入门 之 print带颜色输出
Python 入门 之 print带颜色输出 1.print带颜色输出书写格式: 开头部分: \033[显示方式; 前景色 ; 背景色 m 结尾部分: \033[0m 详解: 开头部分的三个参数: 显 ...
- java中如果删除导入的jar包,工程出现叹号解决方案
第一步:在工程上右键 第二步:选中build Path 第三步:选择Configue bulid path 第四步:选择liberary 第五步:鼠标点击带红色叉叉的 第六步:点击edit 第七步:点 ...
- session+验证码 学习
分析 LoginServlet类 @WebServlet("/loginServlet") public class LoginServlet extends HttpServle ...
- 算法分析方法之平摊分析(Amotized Analysis)
- SVM处理多分类问题
"one-against-one" approach from sklearn import svm X = [[0], [1], [2], [3]] Y = [0, 1, 2, ...
- Idea java 程序打jar包(maven)
1.准备好控制台程序 2.引用的项目打包(公共类接口) 3.开发打包 点击运行 打包结果如下
- python CGI环境搭建
本文web服务器使用的为apache. 1. 安装apache yum install -y httpd 2. 配置apache 修改apache配置文件/etc/httpd/conf/httpd.c ...
- Chrome去掉标签页8个框
最终效果: 解决思路: 在标签页鼠标右击弹出的菜单项中存在“查看页面源码”一项,由此可确定chrome的标签页是由一个html页面控制的,只要修改其页面源码便能去掉8个框. 经网上查阅确定了标签页的源 ...