Codeforces Round #352 (Div. 2) D. Robin Hood (二分法+判断平衡态)

解题思路: 由求最小值和求最大值各自二分。

由l*(a[l+1]-a[l]):求取填 1~l 到a[l+1]的高度需要多少的钱,如果大于剩余的k 则可执行 若否 判断剩余的k是否为l,若否最小值为a[l],否则 为a[l]+k/l;

由(n-r+1)*(a[r]-a[r-1]):求去掉n~n-r+1的这部分需要多少钱,如果大于剩余的k 则执行 若否 判断剩余的k是否为n-r+1,若是最大值为a[n-r+1]-k/(n-r+1);

如果最大值小于最大值,直接printf

如果最小值大于等于最大值,证明计算经过了平衡态,因为富人变穷会得到金币,所以会得到一个稳定状态;

当为sum(金币总和)刚好为n的倍数时,金币相差为0,若否金币相差为1;

#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;

int a[500100];
int n,k;
ll sum;

int main(){
    scanf("%d%d",&n,&k);

    sum=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];

    sort(a+1,a+n+1);

    int l=1,r=n;
    int t[2]={k,k};
    while(t[0]&&l<n){
        if(t[0]>=1LL*l*(a[l+1]-a[l])) t[0]-=1LL*l*(a[l+1]-a[l]),l++;
        else break;
    }

    while(t[1]&&r>1){
        if(t[1]>=1LL*(n-r+1)*(a[r]-a[r-1])) t[1]-=1LL*(n-r+1)*(a[r]-a[r-1]),r--;
        else break;
    }

    int judge[2]={a[l]+t[0]/l,a[r]-t[1]/(n-r+1)};

    if(judge[0]<judge[1]){
        printf("%d\n",judge[1]-judge[0]);
    }else printf("%d\n",(int)(sum%n!=0));

    return 0;
} 
时间: 2024-10-22 21:25:50

Codeforces Round #352 (Div. 2) D. Robin Hood (二分法+判断平衡态)的相关文章

Codeforces Round #352 (Div. 2) D. Robin Hood

题目连接请戳此处 题意:n个人每人有一定的财富值ci,每天Robin Hood从最富的人手中取财富1分给最穷的人(取后最穷, 即可以退回),若有多个最穷的人,任选一个给出财富值.问k天后最富的人和最穷的人财富差值为多少. 1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109 1 ≤ ci ≤ 109 分析: 每一天随着财富值的取和给,最穷的人和最富的人都在动态更新. 最后要求的是  (richest-poorest)min,那么  要使这个等式最小,只需求出k天后richest的最小值和po

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

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #352 (Div. 2) ABCD

Problems # Name     A Summer Camp standard input/output 1 s, 256 MB    x3197 B Different is Good standard input/output 2 s, 256 MB    x2870 C Recycling Bottles standard input/output 2 s, 256 MB    x664 D Robin Hood standard input/output 1 s, 256 MB  

Codeforces Round #352 (Div. 2) C. Recycling Bottles(枚举)

思路:分析完这道题后会发现  当两个人捡到第一个瓶子后, 之后走的路的最小值都是不会变的了. 所以只要找出两个走到各自的第一个瓶子是最小值的情况的时候(其中还有一个人不走,一个人走的情况).   如果当有两个人或有一个人到其第一个瓶子的权值大于瓶子到回收点时,选取权值小的那个. 而且计算最小值的时候 只需取出两个权值数组中最小的两个数 即可 不用全部遍历来选取. #include<iostream> #include<cstdio> #include<cmath> #i

Codeforces Round #352 (Div. 2) B - Different is Good

A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants allsubstrings 

Codeforces Round #352 (Div. 2) A Summer Camp

Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard problems. This is your first year at summer camp, and you are asked to solve the following problem. All integers starting with 1 are written in one line. Th

Codeforces Round #352 (Div. 2) C. Recycling Bottles 贪心

C. Recycling Bottles It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottle

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/