codeforces 671B Robin Hood 二分

题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人

问k次操作以后,物品最多的人和物品最少的人相差几个物品

分析:如果次数足够多的话,最后的肯定在平均值上下,小的最多被补到sum/n,大最多减少到sum/n,或者sum/n+1

然后就二分最小值,看所有小的是否能在k次被填满

二分最大值,看所有大的是否都在k次被抹平

然后就是二分的写法,小和大的不一样,需要加等号,避免死循环

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = 5e5+5;
int a[N],k,n;
bool checkmax(int x){
   int tmp=k;
   for(int i=1;i<=n;++i){
    if(a[i]>x)tmp-=(a[i]-x);
    if(tmp<0)return false;
   }
   return true;
}
bool checkmin(int x){
  int tmp=k;
  for(int i=1;i<=n;++i){
    if(a[i]<x)tmp-=(x-a[i]);
    if(tmp<0)return false;
  }
  return true;
}
int main(){
    scanf("%d%d",&n,&k);
    LL sum=0;
    int k1,k2,x=0,y=0x7f7f7f7f;
    for(int i=1;i<=n;++i){
      scanf("%d",&a[i]);
      sum+=a[i];
      x=max(x,a[i]);
      y=min(y,a[i]);
    }
    k1=k2=sum/n;
    if(sum%n)++k2;
    int ans2,ans1,l=y,r=k1;
    while(l<=r){
       int m=(l+r)>>1;
       if(checkmin(m))ans1=m,l=m+1;
       else r=m-1;
    }
    l=k2,r=x;
    while(l<r){
        int m=(l+r)>>1;
        if(checkmax(m))r=m;
        else l=m+1;
    }
    ans2=(l+r)>>1;
    printf("%d\n",ans2-ans1);
    return 0;
}

时间: 2024-10-11 16:27:59

codeforces 671B Robin Hood 二分的相关文章

【模拟】Codeforces 671B Robin Hood

题目链接: http://codeforces.com/problemset/problem/671/B 题目大意: N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人.总共K次,问最后贫富差距. 钱被拿走是立刻结算,所以可能拿走后这个人变最穷的人再还回去. 最富或最穷的人可能有多个,随机选择,并且不会影响最终答案. (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) 题目思路: [模拟] 直接排序离散化数据,之后模拟就行. 细节挺多的要处理清楚. 1

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 672D Robin Hood

思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大会是多少,以及将最大的几个数总共减$k$次,最大值最小可能是多少.两者相减就是答案. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring>

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 (二分) - xgtao -

题目链接 有n(<=500000)个人有各自的财富,Robin Hood要劫富济贫,每次把最富有的那个人的财富抢去1,加在最穷的那个人的财富里,但是一共只能抢有限次数,问最后最富有的那个人和最穷的那个人财富差的最小值. 要让最富有与最穷的差值最小,也就是要让一个最大值最小,最小值最大,用最大值最小-最小值最大那么就是最小,那么求这两个值就可以用二分. #include <iostream> #include <cstdio> #include <algorithm>

cf671B 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 will take exactl

Curious Robin Hood(树状数组+线段树)

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri

LightOj 1112 Curious Robin Hood(线段树||树状数组)

Curious Robin Hood Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.

Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.