Codeforces 671B Robin Hood two points

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.