Submission #237766

#TimeUsernameProblemLanguageResultExecution timeMemory
237766VimmerKrov (COCI17_krov)C++14
140 / 140
760 ms8292 KiB
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("fast-math") //#pragma GCC optimize("no-stack-protector") #define F first #define S second #define sz(x) int(x.size()) #define pb push_back #define N 100001 #define M ll(1e9 + 7) #define inf 1e9 + 1e9 using namespace std; //using namespace __gnu_pbds; typedef long double ld; typedef long long ll; typedef short int si; typedef array <int, 2> a2; //typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; struct tree { vector <ll> v, fkol, fsm, c; ll s, kol; tree(): v(0), fkol(0), fsm(0), c(0), s(0), kol(0) {}; void init() { c = v; sort(c.begin(), c.end()); fkol.assign(sz(c), 0); fsm.assign(sz(c), 0); } ll idr(ll val) {return upper_bound(c.begin(), c.end(), val) - c.begin() - 1;} void upd_kol(ll v, ll add) {for (; v < sz(fkol); v = (v | (v + 1))) fkol[v] += add;} void upd_sm(ll v, ll add) {for (; v < sz(fsm); v = (v | (v + 1))) fsm[v] += add;} ll get_sm(ll v) {ll res = 0; for (; v >= 0; v = (v & (v + 1)) - 1) res += fsm[v]; return res;} ll get_kol(ll v) {ll res = 0; for (; v >= 0; v = (v & (v + 1)) - 1) res += fkol[v]; return res;} void add(ll i, ll val) { ll id = idr(v[i]); upd_kol(id, 1); upd_sm(id, val); kol++; s += val; } void del(ll i, ll val) { ll id = idr(v[i]); upd_kol(id, -1); upd_sm(id, -val); kol--; s -= val; } ll calc(ll H) { ll id = idr(H), ans = 0; if (id < 0) return s - kol * H; if (id >= sz(c)) return kol * H - s; ll sum = get_sm(id); ll koler = get_kol(id); ans += koler * H - sum + ((s - sum) - (kol - koler) * H); return ans; } }; ll n, a[N], ans = 1e18; int main() { //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout); ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; tree lt, rt; for (ll i = 0; i < n; i++) { cin >> a[i]; lt.v.pb(a[i] - i); rt.v.pb(a[i] + i); } lt.init(); rt.init(); for (ll i = 0; i < n; i++) rt.add(i, i + a[i]); for (ll i = 0; i < n; i++) { rt.del(i, i + a[i]); lt.add(i, a[i] - i); ll l = max(i + 1, n - i), r = 1e9; while (l < r) { ll lft = (r + l) >> 1; ll rgt = lft + 1; ll sml = lt.calc(lft - i) + rt.calc(lft + i), smr = lt.calc(rgt - i) + rt.calc(rgt + i); if (sml == smr) l = r = lft; else { if (sml < smr) r = lft; if (sml > smr) l = rgt; } } ll smr = lt.calc(l - i); smr += rt.calc(l + i); ans = min(ans, smr); } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...