Test instructions: There are n individuals, everyone a[i] items, K operations, each time from the richest hand to take an item to the poorest people
Ask K operations, the most items of people and items with the least number of different items
Analysis: If the number of times enough, the final affirmation in the average up and down, small up to sum/n, the largest reduction to sum/n, or sum/n+1
And then it's a two-minute minimum, to see if all the small can be filled in k times.
Two-point maximum, see if all the big is in K-Times is wiped flat
Then it is the two-point notation, small and big is not the same, need to add equal sign, to avoid the cycle of death
#include <stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespaceStd;typedefLong LongLL;Const intN = 5e5+5;intA[n],k,n;BOOLCheckmax (intx) { inttmp=K; for(intI=1; i<=n;++i) { if(a[i]>x) tmp-= (a[i]-x); if(tmp<0)return false; } return true;}BOOLCheckmin (intx) { inttmp=K; for(intI=1; i<=n;++i) { if(a[i]<x) tmp-= (X-A[i]); if(tmp<0)return false; } return true;}intMain () {scanf ("%d%d",&n,&k); LL sum=0; intk1,k2,x=0, y=0x7f7f7f7f; for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); Sum+=A[i]; X=Max (x,a[i]); Y=min (y,a[i]); } K1=k2=sum/N; if(sum%n) + +K2; intAns2,ans1,l=y,r=K1; while(l<=R) { intM= (l+r) >>1; if(Checkmin (m)) ans1=m,l=m+1; Elser=m-1; } l=k2,r=x; while(l<R) { intM= (l+r) >>1; if(Checkmax (m)) r=m; Elsel=m+1; } ans2= (l+r) >>1; printf ("%d\n", ans2-ans1); return 0;}
View Code
Codeforces 671B Robin Hood two points