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 ll
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3SJjfN1 ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
signed main(){
_3SJjfN1;
int n;
cin>>n;
vi a(n);
rep(i,n){
cin>>a[i];
}
auto af=[&](int pvt,int x)->int{
int res=0;
per(i,pvt+1){
int need=x-(pvt-i);
res+=abs(need-a[i]);
}
rng(i,pvt+1,n){
int need=x-(i-pvt);
res+=abs(need-a[i]);
}
return res;
};
const int inf=1e18;
int ans=inf;
rep(i,n){
int l=max(i+1,n-i),r=1e10;
while(r-l>3){
int ml=(2*l+r)/3;
int mr=(l+2*r)/3;
int fl=af(i,ml);
int fr=af(i,mr);
if(fl<fr){
r=mr;
}else{
l=ml;
}
}
rng(j,l,r+1){
ans=min(ans,af(i,j));
}
}
print(ans);
}
# | 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... |