Codeforces Round #352 (Div. 2) D. Robin Hood (dichotomy + judging equilibrium state)

Source: Internet
Author: User

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)

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.