Submission #767219

# Submission time Handle Problem Language Result Execution time Memory
767219 2023-06-26T14:13:19 Z danikoynov Ancient Books (IOI17_books) C++14
100 / 100
153 ms 65692 KB
#include "books.h"
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;


const int maxn = 1e6 + 10;
const ll inf = 1e18;
ll used[maxn];
int  lb[maxn], rb[maxn], n;
int bef[maxn], aft[maxn];
vector < int > st[maxn];

int l, r, cnt;
void extend(int left, int right)
{
    while(l > left || r < right)
    {
        if (l > left)
        {
            l --;
            if (used[l] != 0)
            {
            left = min(left, lb[used[l]]);
            right = max(right, rb[used[l]]);
            }
        }
        if (r < right)
        {
            r ++;
            if (used[r] != 0)
            {
            left = min(left, lb[used[r]]);
            right = max(right, rb[used[r]]);
            }
        }
    }
}

ll left_cost(int to)
{
    int gt = l, cost = 0;
    for (int i = l - 1; i >= to; i --)
    {
        if (used[i] == 0)
            continue;
        if (gt > i)
        cost = cost + (gt - i) * 2;
        gt = min(gt, lb[used[i]]);
    }
    return cost;
}

ll right_cost(int to)
{
    int gt = r, cost = 0;
    for (int i = r + 1; i <= to; i ++)
    {
        if (used[i] == 0)
            continue;
        if (gt < i)
            cost = cost + (i - gt) * 2;
        gt = max(gt, rb[used[i]]);
    }
    return cost;
}
long long minimum_walk(vector<int> p, int s)
{
    n = p.size();

    int cnt = 0;
    ll ans = 0;
    for (int i = 0; i < n; i ++)
    {
        ans = ans + abs(p[i] - i);
        if (p[i] == i)
            continue;
        if (used[i])
            continue;

        cnt ++;
        used[i] = cnt;
        int v = p[i];
        lb[cnt] = i;
        rb[cnt] = i;
        st[cnt].push_back(i);
        while(v != i)
        {
            st[cnt].push_back(v);
            used[v] = cnt;
            rb[cnt] = max(rb[cnt], v);
            v = p[v];
        }
    }
    l = s + 1;
    r = s;
    extend(s, s);
    while(true)
    {
        ///cout << l << " " << r << " " << ans << endl;
        int bf = l - 1, af = r + 1;
        while(bf >= 0)
        {
            if (used[bf] == 0)
            {
                bf --;
                continue;
            }
            if (lb[used[bf]] < l && rb[used[bf]] > r)
                break;
            bf --;
        }
        while(af < n)
        {
            if (used[af] == 0)
            {
                af ++;
                continue;
            }
            if (lb[used[af]] < l && rb[used[af]] > r)
                break;
            af ++;
        }

        if (bf != -1)
        {
            ll lc = left_cost(bf);
            ll rc = right_cost(af);
            ans = ans + min(lc, rc);
            extend(bf, af);
        }
        else
        {
                        ll lc = left_cost(0);
            ll rc = right_cost(n - 1);
            ///scout << lc << " " << rc << endl;
            ans = ans + lc + rc;
            break;
        }
    }

    ///cout << dp[lf][rf] << endl;
    ///cout << lf << " " << rf << endl;
    return ans;


}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 12 ms 23808 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23800 KB Output is correct
6 Correct 14 ms 23784 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23788 KB Output is correct
10 Correct 12 ms 23720 KB Output is correct
11 Correct 12 ms 23788 KB Output is correct
12 Correct 12 ms 23824 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23792 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23772 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 12 ms 23808 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23800 KB Output is correct
6 Correct 14 ms 23784 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23788 KB Output is correct
10 Correct 12 ms 23720 KB Output is correct
11 Correct 12 ms 23788 KB Output is correct
12 Correct 12 ms 23824 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23792 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23772 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 12 ms 23788 KB Output is correct
20 Correct 14 ms 23788 KB Output is correct
21 Correct 12 ms 23776 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23808 KB Output is correct
24 Correct 14 ms 23792 KB Output is correct
25 Correct 12 ms 23724 KB Output is correct
26 Correct 13 ms 23796 KB Output is correct
27 Correct 13 ms 23788 KB Output is correct
28 Correct 12 ms 23764 KB Output is correct
29 Correct 13 ms 23768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 12 ms 23808 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23800 KB Output is correct
6 Correct 14 ms 23784 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23788 KB Output is correct
10 Correct 12 ms 23720 KB Output is correct
11 Correct 12 ms 23788 KB Output is correct
12 Correct 12 ms 23824 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23792 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23772 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 12 ms 23788 KB Output is correct
20 Correct 14 ms 23788 KB Output is correct
21 Correct 12 ms 23776 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23808 KB Output is correct
24 Correct 14 ms 23792 KB Output is correct
25 Correct 12 ms 23724 KB Output is correct
26 Correct 13 ms 23796 KB Output is correct
27 Correct 13 ms 23788 KB Output is correct
28 Correct 12 ms 23764 KB Output is correct
29 Correct 13 ms 23768 KB Output is correct
30 Correct 153 ms 45312 KB Output is correct
31 Correct 131 ms 45176 KB Output is correct
32 Correct 117 ms 32420 KB Output is correct
33 Correct 119 ms 48088 KB Output is correct
34 Correct 123 ms 48076 KB Output is correct
35 Correct 127 ms 49320 KB Output is correct
36 Correct 120 ms 48916 KB Output is correct
37 Correct 116 ms 46496 KB Output is correct
38 Correct 104 ms 46028 KB Output is correct
39 Correct 106 ms 46012 KB Output is correct
40 Correct 108 ms 45136 KB Output is correct
41 Correct 126 ms 44992 KB Output is correct
42 Correct 128 ms 44756 KB Output is correct
43 Correct 120 ms 50060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23760 KB Output is correct
2 Correct 13 ms 23708 KB Output is correct
3 Correct 13 ms 23792 KB Output is correct
4 Correct 12 ms 23796 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23808 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 14 ms 23796 KB Output is correct
10 Correct 12 ms 23780 KB Output is correct
11 Correct 13 ms 23788 KB Output is correct
12 Correct 12 ms 23848 KB Output is correct
13 Correct 12 ms 23820 KB Output is correct
14 Correct 12 ms 23792 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23828 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23780 KB Output is correct
19 Correct 12 ms 23792 KB Output is correct
20 Correct 12 ms 23740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 12 ms 23808 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23800 KB Output is correct
6 Correct 14 ms 23784 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23788 KB Output is correct
10 Correct 12 ms 23720 KB Output is correct
11 Correct 12 ms 23788 KB Output is correct
12 Correct 12 ms 23824 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23792 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23772 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 12 ms 23788 KB Output is correct
20 Correct 14 ms 23788 KB Output is correct
21 Correct 12 ms 23776 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23808 KB Output is correct
24 Correct 14 ms 23792 KB Output is correct
25 Correct 12 ms 23724 KB Output is correct
26 Correct 13 ms 23796 KB Output is correct
27 Correct 13 ms 23788 KB Output is correct
28 Correct 12 ms 23764 KB Output is correct
29 Correct 13 ms 23768 KB Output is correct
30 Correct 153 ms 45312 KB Output is correct
31 Correct 131 ms 45176 KB Output is correct
32 Correct 117 ms 32420 KB Output is correct
33 Correct 119 ms 48088 KB Output is correct
34 Correct 123 ms 48076 KB Output is correct
35 Correct 127 ms 49320 KB Output is correct
36 Correct 120 ms 48916 KB Output is correct
37 Correct 116 ms 46496 KB Output is correct
38 Correct 104 ms 46028 KB Output is correct
39 Correct 106 ms 46012 KB Output is correct
40 Correct 108 ms 45136 KB Output is correct
41 Correct 126 ms 44992 KB Output is correct
42 Correct 128 ms 44756 KB Output is correct
43 Correct 120 ms 50060 KB Output is correct
44 Correct 12 ms 23760 KB Output is correct
45 Correct 13 ms 23708 KB Output is correct
46 Correct 13 ms 23792 KB Output is correct
47 Correct 12 ms 23796 KB Output is correct
48 Correct 17 ms 23808 KB Output is correct
49 Correct 12 ms 23764 KB Output is correct
50 Correct 12 ms 23808 KB Output is correct
51 Correct 12 ms 23764 KB Output is correct
52 Correct 14 ms 23796 KB Output is correct
53 Correct 12 ms 23780 KB Output is correct
54 Correct 13 ms 23788 KB Output is correct
55 Correct 12 ms 23848 KB Output is correct
56 Correct 12 ms 23820 KB Output is correct
57 Correct 12 ms 23792 KB Output is correct
58 Correct 12 ms 23764 KB Output is correct
59 Correct 12 ms 23828 KB Output is correct
60 Correct 12 ms 23764 KB Output is correct
61 Correct 12 ms 23780 KB Output is correct
62 Correct 12 ms 23792 KB Output is correct
63 Correct 12 ms 23740 KB Output is correct
64 Correct 110 ms 50848 KB Output is correct
65 Correct 114 ms 50092 KB Output is correct
66 Correct 106 ms 52132 KB Output is correct
67 Correct 110 ms 51984 KB Output is correct
68 Correct 22 ms 26708 KB Output is correct
69 Correct 27 ms 26624 KB Output is correct
70 Correct 27 ms 26488 KB Output is correct
71 Correct 23 ms 26552 KB Output is correct
72 Correct 23 ms 26352 KB Output is correct
73 Correct 21 ms 26480 KB Output is correct
74 Correct 119 ms 53876 KB Output is correct
75 Correct 136 ms 53792 KB Output is correct
76 Correct 117 ms 55716 KB Output is correct
77 Correct 121 ms 55840 KB Output is correct
78 Correct 125 ms 60452 KB Output is correct
79 Correct 141 ms 60668 KB Output is correct
80 Correct 90 ms 38300 KB Output is correct
81 Correct 132 ms 65692 KB Output is correct
82 Correct 131 ms 65624 KB Output is correct
83 Correct 124 ms 56732 KB Output is correct
84 Correct 122 ms 56148 KB Output is correct
85 Correct 129 ms 53768 KB Output is correct
86 Correct 111 ms 52724 KB Output is correct
87 Correct 111 ms 52084 KB Output is correct
88 Correct 129 ms 57860 KB Output is correct
89 Correct 128 ms 57548 KB Output is correct
90 Correct 104 ms 52256 KB Output is correct
91 Correct 107 ms 51684 KB Output is correct
92 Correct 110 ms 51232 KB Output is correct