답안 #237766

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
237766 2020-06-08T16:32:21 Z Vimmer Krov (COCI17_krov) C++14
140 / 140
760 ms 8292 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 384 KB Output is correct
2 Correct 9 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 512 KB Output is correct
2 Correct 9 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 17 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 17 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 640 KB Output is correct
2 Correct 18 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 640 KB Output is correct
2 Correct 33 ms 768 KB Output is correct
3 Correct 16 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 2324 KB Output is correct
2 Correct 166 ms 2548 KB Output is correct
3 Correct 178 ms 2420 KB Output is correct
4 Correct 212 ms 2684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 3692 KB Output is correct
2 Correct 317 ms 4064 KB Output is correct
3 Correct 136 ms 3700 KB Output is correct
4 Correct 239 ms 3696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 571 ms 6244 KB Output is correct
2 Correct 467 ms 6496 KB Output is correct
3 Correct 263 ms 3316 KB Output is correct
4 Correct 560 ms 6500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 420 ms 8292 KB Output is correct
2 Correct 592 ms 8164 KB Output is correct
3 Correct 555 ms 6500 KB Output is correct
4 Correct 760 ms 8036 KB Output is correct
5 Correct 123 ms 2428 KB Output is correct