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;
#define int long long
int maOf (int a, int b)
{
return (a>b) ? a : b;
}
int miOf (int a, int b)
{
return (a<b) ? a : b;
}
int tong(int a[], int n,int i, int m){
int y = m;
int d = 0;
d += abs(a[i] - y);
y--;
for(int j = i - 1; j >= 1; j--){
d += abs(a[j] - y);
y--;
}
y = m - 1;
for(int j = i + 1; j <= n; j++){
d += abs(a[j] - y);
y--;
}
return d;
}
int roof(int *a, int &n,int &i){
int t, p= 1e9,average,tog,tong2,tong3,tong4,tong5;
if(n&1) t = maOf(i, n - i);
else t = maOf(i, n - i) + 1;
while(p > t + 1){
average = (t + p)/2;
tog = tong(a,n,i, t);
tong2 = tong(a,n,i, average); tong3 = tong(a,n,i, p);
if(tog > tong2 && tong2 > tong3){
t = average;
}else {
if(tog < tong2 && tong2 < tong3){
p = average;
}
else {
tong4 = tong(a,n,i, average - 1);tong5 = tong(a,n,i, average + 1);
if(tong4 > tong2 && tong2 > tong5) t = average;
else p = average;
}
}
}
return miOf(tong(a,n,i, t), tong(a,n,i, p));
}
main (){
int n,kq=1e12,y;
cin>>(n); int a[n+1];
for(int i = 1; i <= n; i++)
cin>>(a[i]);
for(int i = 1; i <= n; i++){
y = roof(a,n,i);
if(kq > y) {
kq = y;
}
}
cout<<(kq);
}
Compilation message (stderr)
krov.cpp:54:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
54 | main (){
| ^
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |