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<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int n;
ll a[100010];
double f(double x){
double ret=0;
for(int i=0;i<n;i++)ret+=fabs(1.0*a[i]-x*i);
return ret;
}
ll f(ll x){
ll ret=0;
for(ll i=0;i<n;i++)ret+=abs((ll)a[i]-x*i);
return ret;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%lld",&a[i]);
double lo=0,hi=1e9,z0,z1;
for(int i=0;i<100;i++){
z0=(lo*2+hi)/3.0,z1=(lo+hi*2)/3.0;
if(f(z0)<f(z1))hi=z1;
else lo=z0;
}
ll x=hi;
printf("%lld",min(f(x),f(x+1)));
}
# | 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... |