Submission #236827

# Submission time Handle Problem Language Result Execution time Memory
236827 2020-06-03T13:48:01 Z ne4eHbKa Krov (COCI17_krov) C++17
140 / 140
721 ms 5364 KB
#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 re return
#define pb push_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 vector<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; vector<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 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:75:5: note: in expansion of macro 'fo'
     fo(n) le.v.pb(a[i] - i); le.init();
     ^~
krov.cpp:75: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:76:5: note: in expansion of macro 'fo'
     fo(n) ri.v.pb(a[i] + i); ri.init();
     ^~
krov.cpp:76: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();
                              ^~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 384 KB Output is correct
2 Correct 9 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 384 KB Output is correct
2 Correct 9 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 12 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 512 KB Output is correct
2 Correct 16 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 512 KB Output is correct
2 Correct 25 ms 544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 640 KB Output is correct
2 Correct 28 ms 760 KB Output is correct
3 Correct 16 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 1664 KB Output is correct
2 Correct 164 ms 1728 KB Output is correct
3 Correct 170 ms 1664 KB Output is correct
4 Correct 206 ms 1924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 301 ms 2428 KB Output is correct
2 Correct 305 ms 2684 KB Output is correct
3 Correct 145 ms 2428 KB Output is correct
4 Correct 237 ms 2548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 578 ms 3836 KB Output is correct
2 Correct 462 ms 4080 KB Output is correct
3 Correct 259 ms 2300 KB Output is correct
4 Correct 575 ms 4084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 5364 KB Output is correct
2 Correct 606 ms 5108 KB Output is correct
3 Correct 538 ms 4084 KB Output is correct
4 Correct 721 ms 5024 KB Output is correct
5 Correct 127 ms 1784 KB Output is correct