继矿工配餐之后又一IOI水题
直接二分最大的距离然后判断即可
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
long long p;
int pos[100003];
bool check(int len){
int i,l,r,mid;
long long now=0;
l=1,r=len,mid=l+r>>1;
for(i=l;i<mid;i++){
now+=(pos[mid]-pos[i]);
}
for(i=mid+1;i<=r;i++){
now+=(pos[i]-pos[mid]);
}
if(now<=p){
return true;
}
for(;r<n;){
now-=(pos[mid]-pos[l++]);
now+=(pos[++r]-pos[mid]);
mid=l+r>>1;
now+=(pos[mid]-pos[mid-1])*(mid-l);
now-=(pos[mid]-pos[mid-1])*(r-mid+1);
if(now<=p)return true;
}
return false;
}
int main(){
cin>>n>>m>>p;
for(int i=1;i<=n;i++){
cin>>pos[i];
}
int l=1,r=n,mid,ans=0;
while(l<r){
if(r-l<=3){
for(int i=l;i<=r;i++){
if(check(i)){
ans=i;
}
}
break;
}
mid=l+r>>1;
if(check(mid)){
l=mid;
}else{
r=mid-1;
}
}
cout<<ans<<endl;
return 0;
}
/*
in:
5 20 6
1
2
10
12
14
out:
3
*/