답안 #146653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146653 2019-08-24T23:10:58 Z tri 고대 책들 (IOI17_books) C++14
100 / 100
352 ms 81268 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


int N;
vi nxt;

vi id;
vi lM, rM;
int cL, cR;

void includeAll(int nL, int nR) {
    while (nL < cL || cR < nR) {
        while (nL < cL) {
            int cG = id[--cL];
            nL = min(nL, lM[cG]);
            nR = max(nR, rM[cG]);
        }
        while (cR < nR) {
            int cG = id[++cR];
            nL = min(nL, lM[cG]);
            nR = max(nR, rM[cG]);
        }
    }
}

ll calc(int s) {
    int cG = id[s];
    cL = lM[cG], cR = lM[cG];
    includeAll(lM[cG], rM[cG]);

    ll tExt = 0, lExt = 0, rExt = 0;
    int cSide = 0;

    while (cL != 0 || cR != N - 1) {
        cSide = !cSide;
        if (cSide == 0) {
            if (cL == 0) {
                continue;
            }

            lExt++;

            int cGroup = id[cL - 1];

            int nL = lM[cGroup];
            int nR = max(cR, rM[cGroup]);

            for (int i = cL - 1; i >= nL; i--) {
                nL = min(nL, lM[id[i]]);
                nR = max(nR, rM[id[i]]);
            }

            if (nR > cR) {
                // use this one
                tExt += lExt;
                lExt = 0, rExt = 0;
            }
            includeAll(nL, nR);
        } else {
            if (cR == N - 1) {
                continue;
            }

            rExt++;
            int cGroup = id[cR + 1];

            int nL = min(cL, lM[cGroup]);
            int nR = rM[cGroup];

            for (int i = cR + 1; i <= nR; i++) {
                nL = min(nL, lM[id[i]]);
                nR = max(nR, rM[id[i]]);
            }

            if (nL < cL) {
                tExt += rExt;
                lExt = 0, rExt = 0;
            }
            includeAll(nL, nR);
        }
    }

    tExt += lExt + rExt;
    return 2 * tExt;
}


ll minimum_walk(vi p, int s) {
    N = p.size();

    // prune
    int l = 0, r = N - 1;
    while (l < s && p[l] == l) {
        l++;
    }
    while (r > s && p[r] == r) {
        r--;
    }

    for (int i = l; i <= r; i++) {
        nxt.pb(p[i] - l);
    }
    s -= l;
    N = nxt.size();

    //ps("fin prune");

    ll sum = 0;
    for (int i = 0; i < N; i++) {
        sum += abs(nxt[i] - i);
    }

    vector<vi> groups;
    id.resize(N, -1);

    for (int i = 0; i < N; i++) {
        if (id[i] == -1) {
            vi newG;

            int cur = i;
            while (id[cur] == -1) {
                id[cur] = groups.size();
                newG.pb(cur);
                cur = nxt[cur];
            }

            groups.pb(newG);
        }
    }

    //ps("div groups");

    int numG = groups.size();

    for (int i = 0; i < numG; i++) {
        int cLM = N, cRM = 0;
        for (int x : groups[i]) {
            cLM = min(cLM, x);
            cRM = max(cRM, x);
        }
        lM.push_back(cLM);
        rM.push_back(cRM);
    }

    //ps("prep");

    return sum + calc(s);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 252 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 252 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 380 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 380 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 252 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 380 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 380 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 242 ms 22232 KB Output is correct
31 Correct 247 ms 20832 KB Output is correct
32 Correct 134 ms 8184 KB Output is correct
33 Correct 321 ms 64924 KB Output is correct
34 Correct 330 ms 64952 KB Output is correct
35 Correct 308 ms 57288 KB Output is correct
36 Correct 240 ms 34776 KB Output is correct
37 Correct 189 ms 22084 KB Output is correct
38 Correct 184 ms 20380 KB Output is correct
39 Correct 192 ms 20268 KB Output is correct
40 Correct 204 ms 20064 KB Output is correct
41 Correct 227 ms 20424 KB Output is correct
42 Correct 215 ms 20576 KB Output is correct
43 Correct 326 ms 67972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 424 KB Output is correct
4 Correct 3 ms 404 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 3 ms 380 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 252 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 380 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 380 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 242 ms 22232 KB Output is correct
31 Correct 247 ms 20832 KB Output is correct
32 Correct 134 ms 8184 KB Output is correct
33 Correct 321 ms 64924 KB Output is correct
34 Correct 330 ms 64952 KB Output is correct
35 Correct 308 ms 57288 KB Output is correct
36 Correct 240 ms 34776 KB Output is correct
37 Correct 189 ms 22084 KB Output is correct
38 Correct 184 ms 20380 KB Output is correct
39 Correct 192 ms 20268 KB Output is correct
40 Correct 204 ms 20064 KB Output is correct
41 Correct 227 ms 20424 KB Output is correct
42 Correct 215 ms 20576 KB Output is correct
43 Correct 326 ms 67972 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 3 ms 376 KB Output is correct
46 Correct 3 ms 424 KB Output is correct
47 Correct 3 ms 404 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 256 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 3 ms 380 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 3 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 3 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 316 ms 74076 KB Output is correct
65 Correct 323 ms 78808 KB Output is correct
66 Correct 192 ms 28896 KB Output is correct
67 Correct 188 ms 27668 KB Output is correct
68 Correct 24 ms 4460 KB Output is correct
69 Correct 22 ms 3696 KB Output is correct
70 Correct 26 ms 5228 KB Output is correct
71 Correct 32 ms 7656 KB Output is correct
72 Correct 34 ms 8420 KB Output is correct
73 Correct 20 ms 2912 KB Output is correct
74 Correct 322 ms 71652 KB Output is correct
75 Correct 335 ms 71732 KB Output is correct
76 Correct 335 ms 74044 KB Output is correct
77 Correct 324 ms 74380 KB Output is correct
78 Correct 313 ms 67760 KB Output is correct
79 Correct 313 ms 67756 KB Output is correct
80 Correct 134 ms 14840 KB Output is correct
81 Correct 267 ms 55424 KB Output is correct
82 Correct 266 ms 55496 KB Output is correct
83 Correct 266 ms 48588 KB Output is correct
84 Correct 254 ms 44648 KB Output is correct
85 Correct 217 ms 35168 KB Output is correct
86 Correct 194 ms 29636 KB Output is correct
87 Correct 352 ms 81268 KB Output is correct
88 Correct 329 ms 72368 KB Output is correct
89 Correct 298 ms 53220 KB Output is correct
90 Correct 188 ms 27844 KB Output is correct
91 Correct 184 ms 27064 KB Output is correct
92 Correct 187 ms 26848 KB Output is correct