• 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
    ");
            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
    ");
            else { a[n]=tt; work(); }
        }
    
        return 0;
    }
  • 相关阅读:
    谷歌 colab调用 Kaggle 数据集
    TensorFlow/Keras binary_crossentropy损失函数
    R语言 pivot_longer 图表变换
    R语言 ggplot2 柱状图
    R语言 ggplot2 笔记
    Bash 批量删除指定后缀的文件
    MacBook 风扇控制软件 Macs Fan Control
    R语言 dplyr selec 辅助函数
    R语言一次性更新全部packages
    R语言 glue 版本冲突
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5873695.html
Copyright © 2020-2023  润新知