F-Honk`s pool
Topic link: https: //nanti.jisuanke.com/t/41406
Title effect: honk pool has n, i-th tank has a [i ] liters per day will do the following operations honk
1. Take one liter of water up from the water pool (if there are a plurality of most pool water, an arbitrary choice) from 1L of
2. Find the least a water pool (if there are a plurality of minimum water pool, an arbitrary choice), the liters of water that was poured wherein
After you ask k days, the pool water is the number of poor
Problem-solving ideas:
1. simulated violence, as to why this can too, I do not know, certainly the cause of water data, look at the code
2. dichotomy
During this operation, all of the average pool will be certainly moved, first requires the average value Ave, the n in the pond
The transfer amount of water is constant is k, we first separated the maximum amount of water is greater than two in the k ave days can be transferred, and then half of the pool is less than ave
Who can get the maximum amount you can subtract final
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+10; typedef long long ll; ll a[maxn]; ll n,k; bool check(ll mid,bool flag=false) { ll sum=0; for(int i=1;i<=n;i++) { if(a[i]>=mid&&flag==false) sum+=a[i]-mid; else if(a[i]<=mid&&flag==true) sum+=mid-a[i]; } if(sum<=k) return true; else return false; } int main() { std::ios::sync_with_stdio(false); while(std::cin>>n>>k) { ll top,low,sum=0; for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i]; sort(a+1,a+1+n); ll maxx=a[n]; ll minn=a[1]; if(sum%n==0) { top=low=sum/n; } else { low=sum/n+1; top=sum/n; } ll ans=0; ll ls=low,rs=maxx; while(ls<=rs) { ll mid=(ls+rs)>>1; if(check(mid)) { ans=mid; rs=mid-1; } else ls=mid+1; } ll ans1=0; ls=minn,rs=top; while(ls<=rs) { ll mid=(ls+rs)>>1; if(check(mid,true)) { ans1=mid; ls=mid+1; } else rs=mid-1; } cout<<ans-ans1<<endl; } return 0; }