#include <bits/stdc++.h>
using namespace std;
#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)
#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back
#define sz(x) int((x).size())
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset((x), (y), sizeof (x))
typedef long long ll;
typedef ve<int> vi;
template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
const int oo = 2e9;
const ll OO = 4e18;
const int N = 1e5 + 5;
int n;
int a[N];
ll ans;
struct tree {
vi v, c, fc; ve<ll> fv; int n; ll s;
tree(): v(0), c(0), fc(0), fv(0), n(0), s(0) {};
void init() {
c = v;
sort(bnd(c));
fc.assign(sz(v) + 5, 0);
fv.assign(sz(v) + 5, 0);
}
void fcupd(int i, int v) { for(i++; i < sz(fc); i += i & -i) fc[i] += v; }
void fvupd(int i, int v) { for(i++; i < sz(fv); i += i & -i) fv[i] += v; }
int fcget(int i) { int v {}; for(i++; i; i -= i & -i) v += fc[i]; re v; }
ll fvget(int i) { ll v {}; for(i++; i; i -= i & -i) v += fv[i]; re v; }
int index(int v) { re upper_bound(bnd(c), v) - c.begin() - 1; }
void add(int i) {
int j = index(v[i]);
fcupd(j, +1);
fvupd(j, +v[i]);
++n, s += v[i];
}
void del(int i) {
int j = index(v[i]);
fcupd(j, -1);
fvupd(j, -v[i]);
--n, s -= v[i];
}
ll get(int c) {
int j = index(c);
if(j < 0) re s - ll(n) * c;
ll le = fcget(j);
ll ri = n - le;
ll sle = fvget(j);
ll sri = s - sle;
re le * c - sle +
sri - ri * c;
}
};
void solve() {
cin >> n;
fo(n) cin >> a[i];
ans = +OO;
tree le {}, ri {};
fo(n) le.v.pb(a[i] - i); le.init();
fo(n) ri.v.pb(a[i] + i); ri.init();
for(int i = 0; i < n; ++i) ri.add(i);
for(int i = 0; i < n; ++i) {
le.add(i);
ri.del(i);
ll l = max(i + 1, n - i), r = 1.5e9;
while(l < r) {
ll m0 = (l + r) >> 1;
ll m1 = m0 + 1;
ll f0 = le.get(m0 - i) + ri.get(m0 + i);
ll f1 = le.get(m1 - i) + ri.get(m1 + i);
if(f0 == f1) l = r = m0;
if(f0 < f1) r = m0;
if(f1 < f0) l = m1;
}
umin(ans, le.get(l - i) + ri.get(l + i));
}
cout << ans << endl;
}
int main() {
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
Compilation message
krov.cpp: In member function 'int tree::fcget(int)':
krov.cpp:43:71: error: 're' was not declared in this scope
int fcget(int i) { int v {}; for(i++; i; i -= i & -i) v += fc[i]; re v; }
^~
krov.cpp:43:71: note: suggested alternative: 've'
int fcget(int i) { int v {}; for(i++; i; i -= i & -i) v += fc[i]; re v; }
^~
ve
krov.cpp:43:77: warning: no return statement in function returning non-void [-Wreturn-type]
int fcget(int i) { int v {}; for(i++; i; i -= i & -i) v += fc[i]; re v; }
^
krov.cpp: In member function 'll tree::fvget(int)':
krov.cpp:44:71: error: 're' was not declared in this scope
ll fvget(int i) { ll v {}; for(i++; i; i -= i & -i) v += fv[i]; re v; }
^~
krov.cpp:44:71: note: suggested alternative: 've'
ll fvget(int i) { ll v {}; for(i++; i; i -= i & -i) v += fv[i]; re v; }
^~
ve
krov.cpp:44:77: warning: no return statement in function returning non-void [-Wreturn-type]
ll fvget(int i) { ll v {}; for(i++; i; i -= i & -i) v += fv[i]; re v; }
^
krov.cpp: In member function 'int tree::index(int)':
krov.cpp:45:24: error: 're' was not declared in this scope
int index(int v) { re upper_bound(bnd(c), v) - c.begin() - 1; }
^~
krov.cpp:45:24: note: suggested alternative: 've'
int index(int v) { re upper_bound(bnd(c), v) - c.begin() - 1; }
^~
ve
krov.cpp:45:67: warning: no return statement in function returning non-void [-Wreturn-type]
int index(int v) { re upper_bound(bnd(c), v) - c.begin() - 1; }
^
krov.cpp: In member function 'll tree::get(int)':
krov.cpp:60:19: error: 're' was not declared in this scope
if(j < 0) re s - ll(n) * c;
^~
krov.cpp:60:19: note: suggested alternative: 've'
if(j < 0) re s - ll(n) * c;
^~
ve
krov.cpp:65:9: error: 're' was not declared in this scope
re le * c - sle +
^~
krov.cpp:65:9: note: suggested alternative: 'ri'
re le * c - sle +
^~
ri
krov.cpp:62:12: warning: unused variable 'ri' [-Wunused-variable]
ll ri = n - le;
^~
krov.cpp:64:12: warning: unused variable 'sri' [-Wunused-variable]
ll sri = s - sle;
^~~
krov.cpp:67:5: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
krov.cpp: In function 'void solve()':
krov.cpp:4:18: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define fr(i, n) for(int i = 0; i < n; ++i)
^
krov.cpp:5:15: note: in expansion of macro 'fr'
#define fo(n) fr(i, n)
^~
krov.cpp:78:5: note: in expansion of macro 'fo'
fo(n) le.v.pb(a[i] - i); le.init();
^~
krov.cpp:78:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
fo(n) le.v.pb(a[i] - i); le.init();
^~
krov.cpp:4:18: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define fr(i, n) for(int i = 0; i < n; ++i)
^
krov.cpp:5:15: note: in expansion of macro 'fr'
#define fo(n) fr(i, n)
^~
krov.cpp:79:5: note: in expansion of macro 'fo'
fo(n) ri.v.pb(a[i] + i); ri.init();
^~
krov.cpp:79:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
fo(n) ri.v.pb(a[i] + i); ri.init();
^~