Submission #677703

# Submission time Handle Problem Language Result Execution time Memory
677703 2023-01-04T07:57:58 Z bashkort Ancient Books (IOI17_books) C++17
50 / 100
177 ms 30804 KB
#include <bits/stdc++.h>
#include "books.h"

using namespace std;
using ll = long long;

long long minimum_walk(std::vector<int> p, int s) {
    if (is_sorted(p.begin(), p.end())) {
        return 0;
    }

    int n = p.size();

    ll ans = 0;

    vector<int> inv(n);
    for (int i = 0; i < n; ++i) {
        inv[p[i]] = i;

        ans += abs(p[i] - i);
    }

    vector<int> lx(n, -1), rx(n, -1);
    for (int i = 0; i < n; ++i) {
        if (lx[i] == -1) {
            int v = i, mn = n, mx = 0;

            vector<int> cycle;
            do {
                mn = min(mn, v);
                mx = max(mx, v);

                lx[v] = 228;
                cycle.push_back(v);

                v = p[v];
            } while (lx[v] == -1);

            for (int x: cycle) {
                lx[x] = mn, rx[x] = mx;
            }
        }
    }

    auto extend = [&](int &l, int &r) {
        int mn = l, mx = r;
        for (int i = l; i <= r; ++i) {
            mn = min(mn, lx[i]);
            mx = max(mx, rx[i]);
        }
        l = mn, r = mx;
    };

    int trivL = 0, trivR = n - 1;
    while (p[trivL] == trivL) {
        trivL += 1;
    }
    while (p[trivR] == trivR) {
        trivR -= 1;
    }

    int l = lx[s], r = rx[s];
    extend(l, r);
    while (l >= 0 && r < n) {
        int cl = l, cr = r;

        int ansL = 0, ansR = 0;
        int mn = n;

        bool found = false;
        while (cl > 0) {
            cl -= 1;
            if (mn > cl) {
                ansL += 1;
            }
            mn = min(mn, p[cl]);
            if (rx[cl] > r) {
                found = true;
                break;
            }
        }

        int mx = 0;

        while (cr < n - 1) {
            cr += 1;
            if (mx < cr) {
                ansR += 1;
            }
            mx = max(mx, p[cr]);
            if (lx[cr] < l) {
                found = true;
                break;
            }
        }

        if (!found) {
            if (trivL < l) {
                ans += (ansL - trivL) * 2;
            }
            if (trivR > r) {
                ans += (ansR - (n - trivR - 1)) * 2;
            }
            break;
        }

        ans += min(ansL, ansR);
        l = min(lx[cl], lx[cr]);
        r = max(rx[cr], rx[cr]);
        extend(l, r);
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 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 1 ms 212 KB Output is correct
14 Correct 1 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 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 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 1 ms 212 KB Output is correct
14 Correct 1 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 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 0 ms 300 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 300 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 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 1 ms 212 KB Output is correct
14 Correct 1 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 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 0 ms 300 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 300 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 177 ms 30804 KB Output is correct
31 Correct 160 ms 28784 KB Output is correct
32 Correct 86 ms 14816 KB Output is correct
33 Correct 126 ms 26612 KB Output is correct
34 Correct 140 ms 26608 KB Output is correct
35 Correct 143 ms 26728 KB Output is correct
36 Correct 120 ms 26612 KB Output is correct
37 Correct 126 ms 26628 KB Output is correct
38 Correct 106 ms 26584 KB Output is correct
39 Correct 103 ms 26676 KB Output is correct
40 Correct 105 ms 26920 KB Output is correct
41 Correct 134 ms 29600 KB Output is correct
42 Correct 124 ms 28176 KB Output is correct
43 Correct 123 ms 26600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3024'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 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 1 ms 212 KB Output is correct
14 Correct 1 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 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 0 ms 300 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 300 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 177 ms 30804 KB Output is correct
31 Correct 160 ms 28784 KB Output is correct
32 Correct 86 ms 14816 KB Output is correct
33 Correct 126 ms 26612 KB Output is correct
34 Correct 140 ms 26608 KB Output is correct
35 Correct 143 ms 26728 KB Output is correct
36 Correct 120 ms 26612 KB Output is correct
37 Correct 126 ms 26628 KB Output is correct
38 Correct 106 ms 26584 KB Output is correct
39 Correct 103 ms 26676 KB Output is correct
40 Correct 105 ms 26920 KB Output is correct
41 Correct 134 ms 29600 KB Output is correct
42 Correct 124 ms 28176 KB Output is correct
43 Correct 123 ms 26600 KB Output is correct
44 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3024'
45 Halted 0 ms 0 KB -