CodeForces 672D Robin Hood

思维。

当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$。我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算。

需要计算的就是,将最小的几个数总共加$k$次,最小值最大会是多少,以及将最大的几个数总共减$k$次,最大值最小可能是多少。两者相减就是答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c=getchar(); x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=x*10+c-‘0‘; c=getchar();}
}

const int maxn=500010;
int n;
LL k,a[maxn],ans,sum;
LL p[maxn],c[maxn];
LL ans1,ans2;

void  work()
{
    for(int i=1;i<=n;i++) p[i]=p[i-1]+a[i];
    for(int i=1;i<=n;i++) c[i]=(i-1)*a[i]-p[i-1];
    int pos; for(int i=1;i<=n;i++) if(c[i]<=k) pos=i;
    LL tmp=k; tmp=tmp-c[pos]; ans1=a[pos];
    ans1=ans1+tmp/pos;

    memset(p,0,sizeof p); memset(c,0,sizeof c);
    for(int i=n;i>=1;i--) p[i]=p[i+1]+a[i];
    for(int i=n;i>=1;i--) c[i]=p[i+1]-(n-i)*a[i];
    for(int i=n;i>=1;i--) if(c[i]<=k) pos=i;
    tmp=k; tmp=tmp-c[pos];  ans2=a[pos];
    ans2=ans2-tmp/(n-pos+1);

    printf("%lld",ans2-ans1);
}

int main()
{
    scanf("%d%lld",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum=sum+a[i];
    }
    sort(a+1,a+1+n);

    if(sum%n==0)
    {
        LL x=0; sum=sum/n;
        for(int i=1;i<=n;i++) x=x+abs(sum-a[i]);
        if(k>=x/2) printf("0\n");
        else work();
    }

    else
    {
        LL tt=a[n];
        a[n]=a[n]-sum%n;
        LL x=0; sum=sum/n;
        for(int i=1;i<=n;i++) x=x+abs(sum-a[i]);
        if(k>=x/2) printf("1\n");
        else { a[n]=tt; work(); }
    }

    return 0;
}
时间: 2024-10-11 07:43:57

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

题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上下,小的最多被补到sum/n,大最多减少到sum/n,或者sum/n+1 然后就二分最小值,看所有小的是否能在k次被填满 二分最大值,看所有大的是否都在k次被抹平 然后就是二分的写法,小和大的不一样,需要加等号,避免死循环 #include <stdio.h> #include <strin

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

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.

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

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.

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.