Submission #421976

# Submission time Handle Problem Language Result Execution time Memory
421976 2021-06-09T14:22:36 Z MarcoMeijer Ancient Books (IOI17_books) C++11
100 / 100
336 ms 54164 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e9
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define sz size()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }

ll minimum_walk(vi p, int s) {
    int n = p.size();
    ll ans = 0;
    vi visited; visited.assign(n,0);
    vi left; left.assign(n,0);
    vi right; right.assign(n,0);
    vi rightInt, leftInt; rightInt.assign(n,0); leftInt.assign(n,0);
    vi rightEmpty, leftEmpty; rightEmpty.assign(n,1); leftEmpty.assign(n,1);

    RE(i,n)
        ans += (ll)abs(i-p[i]);

    RE(i,n) {
        if(visited[i]) continue;
        left[i] = i, right[i] = i;
        if(i == p[i]) continue;
        int u = p[i];
        while(u != i) {
            visited[u] = 1;
            left [i] = min(left [i], u);
            right[i] = max(right[i], u);
            u = p[u];
        }
        u = p[i];
        while(u != i) {
            left [u] = left [i];
            right[u] = right[i];
            u = p[u];
        }
    }

    REV(i,s,n) {
        if(i != n-1)
            rightInt[i] = rightInt[i+1];
        if(i != n-1)
            rightEmpty[i] = rightEmpty[i+1];
        if(left[i] <= s)
            rightInt[i] = 1;
        if(left[i] != right[i])
            rightEmpty[i] = 0;
    }
    REP(i,0,s+1) {
        if(i != 0)
            leftInt[i] = leftInt[i-1];
        if(i != 0)
            leftEmpty[i] = leftEmpty[i-1];
        if(right[i] >= s)
            leftInt[i] = 1;
        if(left[i] != right[i])
            leftEmpty[i] = 0;
    }

    function<int(int)> getLeftSize  = [&](int x) {
        static vi mem(n,-1);
        if(mem[x] != -1)
            return mem[x];

        int i = x;
        int res = 0;
        int leftSide = x;
        int firstInt = INF;
        while(i-- > 0) {
            if(!leftInt[i])
                return mem[x] = leftEmpty[i] ? int(INF) : 1;
            if(i < leftSide)
                res++;
            if(right[i] >= s)
                return mem[x] = res;
            if(firstInt == INF && left[i] != right[i]) {
                firstInt = res;
            }
            leftSide = min(leftSide, left[i]);
        }
        return mem[x] = firstInt;
    };
    function<int(int)> getRightSize = [&](int x) {
        static vi mem(n,-1);
        if(mem[x] != -1)
            return mem[x];

        int i = x;
        int res = 0;
        int rightSide = i;
        int firstInt = INF;
        while(i++ < n-1) {
            if(!rightInt[i])
                return mem[x] = rightEmpty[i] ? int(INF) : 1;
            if(i > rightSide)
                res++;
            if(left[i] <= s)
                return mem[x] = res;
            if(firstInt == INF && left[i] != right[i]) {
                firstInt = res;
            }
            rightSide = max(rightSide, right[i]);
        }
        return mem[x] = firstInt;
    };

    int ls=s, rs=s, nls=s, nrs=s;
    queue<int> q; q.push(s);

    while(true) {
        while(!q.empty()) {
            int i = q.front(); q.pop();
            while(ls > left [i]) q.push(--ls);
            while(rs < right[i]) q.push(++rs);
        }

        if(nls < ls || nrs > rs) {
            while(ls > nls) {
                q.push(--ls);
                ans += 2ll;
                if(left[ls] != right[ls])
                    break;
            }
            while(rs < nrs) {
                q.push(++rs);
                ans += 2ll;
                if(left[rs] != right[rs])
                    break;
            }
            continue;
        }

        int leftSize = getLeftSize(ls), rightSize = getRightSize(rs);

        if(leftSize == INF && rightSize == INF)
            break;
        if(leftSize < rightSize) {
            nls = ls - leftSize;
        } else {
            nrs = rs + rightSize;
        }
    }

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 336 ms 43496 KB Output is correct
31 Correct 322 ms 47172 KB Output is correct
32 Correct 181 ms 47044 KB Output is correct
33 Correct 182 ms 47000 KB Output is correct
34 Correct 195 ms 47044 KB Output is correct
35 Correct 185 ms 47060 KB Output is correct
36 Correct 217 ms 47068 KB Output is correct
37 Correct 204 ms 46948 KB Output is correct
38 Correct 173 ms 47044 KB Output is correct
39 Correct 171 ms 47056 KB Output is correct
40 Correct 188 ms 47172 KB Output is correct
41 Correct 224 ms 49096 KB Output is correct
42 Correct 217 ms 48664 KB Output is correct
43 Correct 176 ms 47192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 216 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 336 ms 43496 KB Output is correct
31 Correct 322 ms 47172 KB Output is correct
32 Correct 181 ms 47044 KB Output is correct
33 Correct 182 ms 47000 KB Output is correct
34 Correct 195 ms 47044 KB Output is correct
35 Correct 185 ms 47060 KB Output is correct
36 Correct 217 ms 47068 KB Output is correct
37 Correct 204 ms 46948 KB Output is correct
38 Correct 173 ms 47044 KB Output is correct
39 Correct 171 ms 47056 KB Output is correct
40 Correct 188 ms 47172 KB Output is correct
41 Correct 224 ms 49096 KB Output is correct
42 Correct 217 ms 48664 KB Output is correct
43 Correct 176 ms 47192 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 216 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 1 ms 332 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 196 ms 53740 KB Output is correct
65 Correct 169 ms 53608 KB Output is correct
66 Correct 185 ms 52108 KB Output is correct
67 Correct 177 ms 51844 KB Output is correct
68 Correct 17 ms 5500 KB Output is correct
69 Correct 20 ms 5376 KB Output is correct
70 Correct 16 ms 5532 KB Output is correct
71 Correct 17 ms 5568 KB Output is correct
72 Correct 21 ms 5320 KB Output is correct
73 Correct 18 ms 5372 KB Output is correct
74 Correct 227 ms 50012 KB Output is correct
75 Correct 175 ms 50024 KB Output is correct
76 Correct 167 ms 54120 KB Output is correct
77 Correct 174 ms 54164 KB Output is correct
78 Correct 172 ms 51292 KB Output is correct
79 Correct 172 ms 51144 KB Output is correct
80 Correct 160 ms 50024 KB Output is correct
81 Correct 176 ms 50916 KB Output is correct
82 Correct 179 ms 51020 KB Output is correct
83 Correct 171 ms 50012 KB Output is correct
84 Correct 176 ms 50012 KB Output is correct
85 Correct 175 ms 50092 KB Output is correct
86 Correct 171 ms 50024 KB Output is correct
87 Correct 192 ms 50140 KB Output is correct
88 Correct 172 ms 50016 KB Output is correct
89 Correct 172 ms 50040 KB Output is correct
90 Correct 179 ms 50028 KB Output is correct
91 Correct 178 ms 50020 KB Output is correct
92 Correct 193 ms 50280 KB Output is correct