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;
const int N = 1e5 + 5;
int n, a[N];
long long check (int mid) {
long long sum = 0;
int Min = 2e9 + 2277;
for (int i = 1; i <= n; i++) {
int need = a[mid] - abs(i - mid);
sum += abs(need - a[i]);
Min = min(Min, need);
}
if (Min <= 0) {
sum = 0;
for (int i = 1; i <= n; i++) {
int need = a[mid] + 1 - Min - abs(i - mid);
sum += abs(need - a[i]);
}
}
return sum;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
//for (int i = 1; i <= n; i++) cout << check(i) << " ";
//return 0;
int l = 1, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid) < check(mid + 1)) r = mid - 1;
else l = mid + 1;
}
cout << check(l);
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... |
# | 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... |