This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int besthub(int n, int L, int x[], long long b)
{
int l=0, r=n-1;
int prefix_sum[n+1];
prefix_sum[0]=0;
for(int i=1; i<=n; i++)
{
prefix_sum[i]=x[i-1]+prefix_sum[i-1];
}
while(r-l>=1)
{
int mid=(r+l)/2;
long long min_dist=LLONG_MAX;
for(int i=0; i<=n-mid-1; i++)
{
int polovina=(i+i+mid)/2, kraj=i+mid;
long long dist=0;
/*for(int j=i; j<polovina; j++)
{
dist+=abs(x[j]-x[polovina]);
}*/
//levo
dist+=(polovina-i)*x[polovina]-(prefix_sum[polovina]-prefix_sum[i]);
//dist+=prefix_sum[polovina]-(prefix_sum[i-1]-(polovina-i)*x[polovina]);
//desno
dist+=(prefix_sum[kraj+1]-prefix_sum[polovina+1])-(kraj-polovina)*x[polovina];
//dist+=(kraj-polovina)*x[polovina]+prefix_sum[kraj+1]-prefix_sum[polovina+1];
/*for(int j=polovina+1; j<=kraj; j++)
{
dist+=abs(x[j]-x[polovina]);
}*/
min_dist=min(dist, min_dist);
}
if(min_dist<=b)
{
l=mid+1;
}
else
{
r=mid;
}
}
return l;
}
/*int main()
{
int r, l;
long long b;
cin>>r>>l;
int x[r];
for(int i=0; i<r; i++)
cin>>x[i];
cin>>b;
cout<<besthub(r, l, x, b);
return 0;
}*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |