zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    [LeetCode#91]Decode Ways
    [LeetCode#130]Surrounded Regions
    [LeetCode#84]Largest Rectangle in Histogram
    [LeetCode#179]Largest Number
    [LeetCode#187]Repeated DNA Sequences
    [LeetCode#200]Number of Islands
    [LeetCode#268]Missing Number
    [LeetCode#44]Wildcard Matching
    [LeetCode#128]Longest Consecutive Sequence
    1如何给devexpress的gridview控件绘制全选按钮
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5873695.html
Copyright © 2011-2022 走看看