Codeforces Round #352 (Div. 2) D 二分

本文介绍了一道关于财富再分配的算法题目,通过模拟Robin Hood每天从最富有的人拿一枚硬币给最贫穷的人的过程,求解在指定天数后的最大财富差距。



链接:戳这里


D. Robin Hood
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Input
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 the i-th person.

Output
Print a single line containing the difference between richest and poorest peoples wealth.

Examples
input
4 1
1 1 4 2
output
2
input
3 1
2 2 2
output
0
Note
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次操作之后,贫富差距多大


思路:

每次都要从最富的人那里拿一个给最穷的人,所以每次的最穷最富的人都在跟新

简化一下题意,从富有的一部分人那里拿出k给部分穷人,而且穷富人可以来回切换

我们分别找出L,R

L表示穷人经过k次操作之后价值均等于L (L最大

R表示富人经过k次操作之后价值均等于R(R最小

R-L>0  anw=R-L

R-L<0 anw=(a1+a2+...+an)/n==0?0:1  因为经过k此操作之后有些穷人变成了富人,所以在R~L之间存在一个值使得序列相等,不相等的话也就相差1


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,k;
int a[500100];
ll solve1(int x){
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]<x) ans+=1LL*(x-a[i]);
    }
    return ans;
}
ll solve2(int x){
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]>x) ans+=1LL*(a[i]-x);
    }
    return ans;
}
int main(){
    scanf("%d%d",&n,&k);
    ll sum=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
    int l=1,r=1e9,mid,ans1=1,ans2=1;
    while(l<r){
        mid=(l+r)/2;
        if(solve1(mid)<=(ll)k) l=mid+1,ans1=mid;
        else r=mid;
    }
    l=1;r=1e9;
    while(l<r){
        mid=(l+r)/2;
        if(solve2(mid)<=(ll)k) r=mid,ans2=mid;
        else l=mid+1;
    }
    if(ans2-ans1>0) cout<<ans2-ans1<<endl;
    else printf("%d\n",sum%n==0?0:1);
    return 0;
}


### Codeforces Round 1005 Div. 2 题目解析 #### A. Unique Number 对于给定的字符串`a`,目标是在其中删除一部分字符使得剩余部分形成字符串"2020"。通过枚举可能的情况来判断是否存在满足条件的结果[^1]。 ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define endl &#39;\n&#39; void solve() { int n; string a; cin >> n >> a; for (int i = 0; i <= 4; i++) { if (a.substr(0, i) + a.substr(n - (4 - i), 4 - i) == "2020") { puts("YES"); return; } } puts("NO"); } signed main() { int _ = 1; cin >> _; while (_--) solve(); } ``` 此代码实现了上述逻辑,尝试不同的分割方式并验证是否能构成所需模式。 #### B. Concatenation of Arrays 该问题可以通过线性扫描数组完成而无需采用更复杂的算法如二分查找。具体实现上,先读入数据并对输入数组进行排序处理之后再执行必要的计算操作以得出最终答案[^2]。 ```cpp bool check(int x) { int res = 0; for (int i = 1; i <= x; ++i) { if (i != x) res += q[i]; else res += (n - i + 1) * q[i]; } return res >= m; } void solve() { int yu = 0, res = 0; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> q[i]; sort(q + 1, q + 1 + n); int l = 1, r = n; while (l < r) { int mid = l + r >> 1; if (check(mid)) r = mid; else l = mid + 1; } cout << l - 1 + m << &#39;\n&#39;; } ``` 尽管这里展示了基于二分法的方法,实际上只需要一次遍历就能解决问题。 #### C. Price Tags 当所有包的成本都相等时,则仅需一个价格标签就可以描述所有的商品组合情况;特别地,在样例测试用例中提到如果每个成本都是某个特定值d1=4的情况下,那么只需设置单个统一的价格即能满足需求[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值