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 ...
随机推荐
- [转帖]glib gslibc libc 的关系与区别
https://blog.csdn.net/Com_ma/article/details/78692092 [glibc 和 libc] glibc 和 libc 都是 Linux 下的 C 函数库. ...
- Windows文件共享配置与遇到的问题
一.Windows 7 访问共享文件权限不足 问题 最近在 Windows 10 上共享了一个文件夹,并创建了一个用户,用于在别人访问该共享文件夹时进行认证,但是在一个同事的电脑(Windows7,当 ...
- C++多线程基础学习笔记(七)
一.std::async和std::future的用法 std::async是一个函数模板,std::future是一个类模板 #include <iostream> #include & ...
- LZH的多重影分身 qduoj 思维 差分
LZH的多重影分身 qduoj 思维 差分 原题链接:https://qduoj.com/problem/591 题意 在数轴上有\(n\)个点(可以重合)和\(m\)条线段(可以重叠),你可以同时平 ...
- Python之模块IO
目录 Python之模块IO io概叙 io类层次结构 io模块的类图 io模块的3种I/O 原始I/O,即RawIOBase及其子类 文本I/O,即TextIOBase及其子类 字节I/O(缓存I/ ...
- Python-RabbitMQ-fanout(广播模式)
生产者:fanout_publiser.py import pika import sys connection = pika.BlockingConnection(pika.ConnectionPa ...
- oa_mvc_easyui_后台布局(3)
1.新建HomeController控制器,并创建视图,后台的主页 2.easyUI的引用: <link href="~/Content/default/easyui.css" ...
- 链接校验——是否是协议http://或https://开头的
if(str.substr(0,7)!="http://" && str.substr(0,7)!="https://"){ return 'y ...
- 解决stackoverflow加载慢的插件
浏览stackoverflow的时候,比较慢,网上找到一个大神写的小工具 挺管用,给推荐下. gitthub地址: https://github.com/justjavac/ReplaceGoogle ...
- SVM处理多分类问题
"one-against-one" approach from sklearn import svm X = [[0], [1], [2], [3]] Y = [0, 1, 2, ...