Topic Connection Please poke here
Test instructions: N Each person has a certain wealth value CI, every day Robin Hood from the richest person to take the wealth of 1 points to the poorest people (after taking the poorest,
can be returned), if there are many poorest people, choose one to give the value of wealth. Ask K days the richest man and the poorest of the poor wealth of the value of what.
1 ≤ n ≤500 000, 0≤ k ≤109
1 ≤ ci ≤109
Analysis:
Every day with the value of wealth and give, the poorest people and the richest people are dynamically updated.
The last requirement is (richest-poorest) min, so to make this equation minimum, only need to k days richest minimum and poorest
The maximum value can be. As the wealth of the rich is monotonous and diminishing, the wealth of the poor is monotonically increasing. The two values can be found by the two-minute enumeration.
Enumerates the left and right endpoints of the conservative point selection l=1,r=1e9. This complexity is probably log2 (109) about log2 (230) = 30.
When poorest (max) <richest (min), ans = richest (min)-poorest (max);
Otherwise, the poor become rich, the rich become poor, indicating there must be some day d<k the wealth value equal or difference of 1.
At this point ans = sum%n==0?0:1.
It is important to note that the intermediate process may explode int, preferably full-use Longlong
1e9 means 109. This e means that the decimal point moves several.
Constant in C, when the suffix defaults, the integer is of type int, and the type of floating-point number is double.
So when used, if the constant needs to be long long, it should be appended with the LL suffix.
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespaceStd;typedefLong Longll;ll c[500005],sum,n;ll Cal1 (ll x) {ll sum=0; for(intI=0; i<n;i++) { if(c[i]>x) Sum+ = (c[i]-x); } returnsum;} ll Cal2 (ll x) {ll sum=0; for(intI=0; i<n;i++) { if(c[i]<x) Sum+ = (xC[i]); } returnsum;}intMain () {ll k; while(~SCANF ("%i64d%i64d",&n,&k)) {sum=0; for(intI=0; i<n;i++) {scanf ("%i64d",&C[i]); Sum+=C[i]; } sort (C,c+N); ll L=1, r =1e9; ll Rich,poor,mid; while(l<r) {Mid= L + (r-l)/2; if(Cal1 (mid) <=k) {Rich=mid; R=mid; } ElseL = mid+1; } l=1, r =1e9; while(l<r) {Mid= L + (r-l)/2; if(Cal2 (mid) <=k) {Poor=mid; L= mid+1; } ElseR =mid; } if(Poor<rich) printf ("%i64d\n", rich-poor); Elseprintf"%d\n", sum%n = =0?0:1); } return 0;}
Codeforces Round #352 (Div. 2) D. Robin Hood