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<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct A
{
double val;
int cnt;
};
A m[100002];
int cmp(const A &a, const A &b)
{
return a.val<b.val;
}
long long min(long long a, long long b) {return a<b ? a : b;}
int n, x[100002];
long long out;
long long sum(long long gap)
{
long long s=0;
int i;
for(i=1 ; i<n ; i++) s+=abs(gap*i-x[i]);
return s;
}
int main()
{
int i;
long long xx, c=0;
scanf("%d",&n);
for(i=0 ; i<n ; i++) scanf("%d",&x[i]);
for(i=1 ; i<n ; i++) m[i].val=(double)x[i]/i, m[i].cnt=i;
sort(m+1,m+n,cmp);
xx=(long long)n*(n-1)/2;
out=sum(m[1].val);
if(xx%2==1)
{
xx=(xx+1)/2;
i=1;
while(c<xx) c+=m[i++].cnt;
i--;
out=min(out,sum(m[i].val));
out=min(out,sum(m[i].val+1));
out=min(out,sum(m[i].val-1));
}
else
{
i=1;
xx/=2;
while(c<xx) c+=m[i++].cnt;
i--;
if(c>=xx+1)
{
out=min(out,sum(m[i].val));
out=min(out,sum(m[i].val+1));
out=min(out,sum(m[i].val-1));
}
if(c==xx)
{
out=min(out,sum(m[i].val));
out=min(out,sum(m[i].val+1));
out=min(out,sum(m[i].val-1));
out=min(out,sum(m[i+1].val));
out=min(out,sum(m[i+1].val+1));
out=min(out,sum(m[i+1].val-1));
}
}
printf("%lld",out);
return 0;
}
Compilation message (stderr)
pole.cpp: In function 'int main()':
pole.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
pole.cpp:38:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=0 ; i<n ; i++) scanf("%d",&x[i]);
~~~~~^~~~~~~~~~~~
# | 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... |