답안 #799522

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799522 2023-07-31T15:34:49 Z PixelCat 고대 책들 (IOI17_books) C++14
50 / 100
117 ms 35544 KB
#include "books.h"

#ifdef NYAOWO
#include "grader.cpp"
#endif

#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;

const int MAXN = 1'000'000;
const int INF = 1e18;

LL minimum_walk(vector<i32> p, i32 s) {
    assert(s >= 0);
    int n = sz(p);

    int ans = 0;
    vector<int> cid(n, -1);
    vector<pii> range;
    For(i, 0, n - 1) if(p[i] != i && cid[i] == -1) {
        int cur = i;
        int l = n + 1;
        int r = -1;
        do {
            l = min(l, cur);
            r = max(r, cur);
            ans += abs(p[cur] - cur);
            cur = p[cur];
            cid[cur] = sz(range);
        } while(cur != i);
        range.eb(l, r);
    }
    if(sz(range) == 0) return 0;

    int L = 0, R = n - 1;
    while(cid[L] == -1) L++;
    while(cid[R] == -1) R--;
    L = min(L, (int)s);
    R = max(R, (int)s);
    cerr << L << " " << R << "\n";

    vector<int> nxtl(n), nxtr(n);
    nxtl[0] = -INF;
    For(i, 1, n - 1) {
        if(cid[i - 1] != -1) nxtl[i] = i - 1;
        else nxtl[i] = nxtl[i - 1];
    }
    nxtr[n - 1] = INF;
    Forr(i, n - 2, 0) {
        if(cid[i + 1] != -1) nxtr[i] = i + 1;
        else nxtr[i] = nxtr[i + 1];
    }

    int l = s, r = s;
    queue<int> que;
    if(cid[s] != -1) que.emplace(cid[s]);
    while(l > L && r < R) {
        while(sz(que)) {
            int id = que.front(); que.pop();
            pii rg = range[id];
            while(l > rg.F) {
                l--;
                if(cid[l] != -1) que.emplace(cid[l]);
            }
            while(r < rg.S) {
                r++;
                if(cid[r] != -1) que.emplace(cid[r]);
            }
        }
        if(l > L && r < R) {
            int l2, r2;

            int cl = 0;
            l2 = l; r2 = r;
            if(l2 == L) cl = INF;
            while(r2 == r && l2 != L) {
                cl += (l2 - nxtl[l2]) * 2;
                l2 = nxtl[l2];
                int l3 = range[cid[l2]].F;
                int r3 = range[cid[l2]].S;
                while(l2 > l3 || r2 < r3) {
                    if(r2 < r3) {
                        l2 = l3; r2 = r3;
                        break;
                    }
                    l2--;
                    if(cid[l2] != -1) {
                        l3 = min(l3, range[cid[l2]].F);
                        r3 = max(r3, range[cid[l2]].S);
                    }
                }
            }
            if(r2 == r) break;
            int l8 = l2, r8 = r2;

            int cr = 0;
            l2 = l; r2 = r;
            if(r2 == R) cr = INF;
            while(l2 == l && r2 != R) {
                cr += (nxtr[r2] - r2) * 2;
                r2 = nxtr[r2];
                int l3 = range[cid[r2]].F;
                int r3 = range[cid[r2]].S;
                while(l2 > l3 || r2 < r3) {
                    if(l2 > l3) {
                        l2 = l3; r2 = r3;
                        break;
                    }
                    r2++;
                    if(cid[r2] != -1) {
                        l3 = min(l3, range[cid[r2]].F);
                        r3 = max(r3, range[cid[r2]].S);
                    }
                }
            }
            if(l2 == l) break;
            assert(l8 == l2 && r8 == r2);

            ans += min(cl, cr);
            l = l2; r = r2;
        }
    }
    {
        int mx = r;
        For(i, r, R) if(cid[i] != -1) {
            ans += 2 * max(0ll, i - mx);
            mx = max(mx, range[cid[i]].S);
        }
    }
    {
        int mn = l;
        Forr(i, l, L) if(cid[i] != -1) {
            ans += 2 * max(0ll, mn - i);
            mn = min(mn, range[cid[i]].F);
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 110 ms 31592 KB Output is correct
31 Correct 117 ms 31564 KB Output is correct
32 Correct 76 ms 15864 KB Output is correct
33 Correct 100 ms 34772 KB Output is correct
34 Correct 107 ms 34704 KB Output is correct
35 Correct 102 ms 35028 KB Output is correct
36 Correct 110 ms 34096 KB Output is correct
37 Correct 91 ms 32096 KB Output is correct
38 Correct 90 ms 31772 KB Output is correct
39 Correct 90 ms 31736 KB Output is correct
40 Correct 94 ms 31596 KB Output is correct
41 Correct 113 ms 31612 KB Output is correct
42 Correct 103 ms 31600 KB Output is correct
43 Correct 109 ms 35544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 110 ms 31592 KB Output is correct
31 Correct 117 ms 31564 KB Output is correct
32 Correct 76 ms 15864 KB Output is correct
33 Correct 100 ms 34772 KB Output is correct
34 Correct 107 ms 34704 KB Output is correct
35 Correct 102 ms 35028 KB Output is correct
36 Correct 110 ms 34096 KB Output is correct
37 Correct 91 ms 32096 KB Output is correct
38 Correct 90 ms 31772 KB Output is correct
39 Correct 90 ms 31736 KB Output is correct
40 Correct 94 ms 31596 KB Output is correct
41 Correct 113 ms 31612 KB Output is correct
42 Correct 103 ms 31600 KB Output is correct
43 Correct 109 ms 35544 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Runtime error 1 ms 468 KB Execution killed with signal 6
46 Halted 0 ms 0 KB -