Problem solving ideas: by the minimum and the maximum value of the respective two points.
by l* (A[l+1]-a[l]): To fill 1~l to a[l+1] the height of how much money, if greater than the remaining K can be performed if the remaining k is the L, if the minimum value is a[l], otherwise a[l]+k/l;
by (n-r+1) * (A[r]-a[r-1]): How much is required to remove this part of the n~n-r+1, if greater than the remaining k is performed if the remaining k is n-r+1, if the maximum value is a[n-r+1]-k/(n-r+1);
If the maximum value is less than the maximum value, the direct printf
If the minimum value is greater than or equal to the maximum, it proves that the calculation has been balanced, because the rich become poor will get gold coins, so will get a stable state;
When the sum (the sum of coins) is just a multiple of n, the gold difference is 0, if no gold difference is 1;
#include <iostream>#include<algorithm>using namespaceStd;typedefLong Longll;inta[500100];intn,k;ll sum;intMain () {scanf ("%d%d",&n,&k); Sum=0; for(intI=1; i<=n;i++) scanf ("%d", &a[i]), sum+=A[i]; Sort (a+1, a+n+1); intL=1, r=N; intt[2]={k,k}; while(t[0]&&l<N) { if(t[0]>=1ll*l* (a[l+1]-A[L]) t[0]-=1ll*l* (a[l+1]-a[l]), l++; Else Break; } while(t[1]&&r>1){ if(t[1]>=1ll* (n-r+1) * (a[r]-a[r-1])) t[1]-=1ll* (n-r+1) * (a[r]-a[r-1]), r--; Else Break; } intjudge[2]={a[l]+t[0]/l,a[r]-t[1]/(n-r+1)}; if(judge[0]<judge[1]) {printf ("%d\n", judge[1]-judge[0]); }Elseprintf"%d\n",(int) (sum%n!=0)); return 0;}
Codeforces Round #352 (Div. 2) D. Robin Hood (dichotomy + judging equilibrium state)