Thinking.
When $k$ tends to be positive infinity, the answer presents two situations, not $0$ or $1$. We can first judge the answer for $1$ and $0$, the rest of the situation needs to be calculated.
What needs to be calculated is to add the minimum number of $k$ times, the minimum value of the maximum will be, and the largest number of a total of $k$ times, the maximum value of the minimum may be what. Subtracting the two is the answer.
#pragmaComment (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 namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-6;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} Template<classT>inlinevoidRead (T &x) { CharC=getchar (); x=0; while(!isdigit (c)) c=GetChar (); while(IsDigit (c)) {x=x*Ten+c-'0'; C=GetChar ();}}Const intmaxn=500010;intN; LL k,a[maxn],ans,sum; LL P[MAXN],C[MAXN]; LL Ans1,ans2;voidWork () { for(intI=1; i<=n;i++) p[i]=p[i-1]+A[i]; for(intI=1; i<=n;i++) c[i]= (i-1) *a[i]-p[i-1]; intPos for(intI=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,sizeofp); Memset (c,0,sizeofc); for(inti=n;i>=1; i--) p[i]=p[i+1]+A[i]; for(inti=n;i>=1; i--) c[i]=p[i+1]-(n-i) *A[i]; for(inti=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);}intMain () {scanf ("%d%lld",&n,&k); for(intI=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(intI=1; i<=n;i++) X=x+abs (sum-A[i]); if(k>=x/2) printf ("0\n"); ElseWork (); } Else{LL TT=A[n]; A[n]=a[n]-sum%N; LL x=0; sum=sum/N; for(intI=1; i<=n;i++) X=x+abs (sum-A[i]); if(k>=x/2) printf ("1\n"); Else{a[n]=TT; work ();} } return 0;}
Codeforces 672D Robin Hood