Submission #1061903

# Submission time Handle Problem Language Result Execution time Memory
1061903 2024-08-16T15:05:06 Z _8_8_ Ancient Books (IOI17_books) C++17
100 / 100
112 ms 14968 KB
    #include "books.h"
    #include <bits/stdc++.h>
        
    using namespace std;
    typedef long long ll;
    const int N = 1e6 + 12;
    int n;
     
    long long minimum_walk(vector<int> p, int s) {
        n = (int)p.size();
        int it = 0;
        ll res = 0;
        for(int i = 0;i < n;i++) {
            res += abs(i - p[i]);
        }
        if(s == 0) {
            int nn = -1;
            for(int i = n - 1;i >= 0;i--) {
                if(p[i] != i) {
                    nn = i;
                    break;
                }
            }
            int mx = -1;
            for(int i = 0;i < nn;i++) {
                mx = max(mx,p[i]);
                if(mx == i) {
                    res += 2;
                }
            }
            return res;
        }
        int l = s,r = s,R = s,L = s;
        int tl,tr;
        for(int i = 0;i < n;i++) {
            if(p[i] != i) {
                tl = i;
                break;
            }
        }
        for(int i = n - 1;i >= 0;i--) {
            if(i != p[i]) {
                tr = i;
                break;
            }
        }
        if(!res) {
            return res;
        }
        while(true) {
            R = max({R,p[r],p[l]});
            L = min({L,p[l],p[r]});
            if(l <= tl && r >= tr) {
                break;
            }
            if(r < R) {
                assert(R <= tr);
                r++;
            } else if(L < l) {
                assert(L >= tl);
                l--;
            } else {
                int l_ = -1e9,r_ = (int)1e9;
                for(int i = l - 1;i >= tl;i--) {
                    if(p[i] > r) {
                        l_ = i;
                        break;
                    }
                }
                for(int i = r + 1;i <= tr;i++) {
                    if(p[i] < l) {
                        r_ = i;
                        break;
                    }
                }
                if(l_ == (int)-1e9) {
                    assert(r_ == (int)1e9);
                    if(l > tl) {
                        res += 2;
                        l--;
                        int mn = (int)1e9;
                        for(int j = l;j > tl;--j) {
                            assert(p[j] <= l && p[j] >= tl);
                            mn = min(mn,p[j]);
                            if(j == mn) {
                                res += 2;
                            }
                        }
                    }
                    if(r < tr) {
                        res += 2;
                        r++;
                        int mx = -1;
                        for(int j = r;j < tr;j++) {
                            assert(p[j] >= r && p[j] <= tr);
                            mx = max(mx,p[j]);
                            if(mx == j) {
                                res += 2;
                            }
                        }
                    }
                    return res;
                } else {
                    ll a = 2,b = 2;
                    int mn = (int)1e9;
                    for(int j = l - 1;j > l_;j--) {
                        mn = min(mn,p[j]);
                        if(mn == j) {
                            a += 2;
                        }
                    }
                    int mx= -1;
                    for(int j = r + 1;j < r_;j++) {
                        mx = max(mx,p[j]);
                        if(mx == j) b += 2;
                    }
                    if(a < b) {
                        res += a;
                        for(int j = l - 1;j > l_;j--) {
                            L = min(L,p[j]);
                            R = max(R,p[j]);
                        }
                        l = l_;
                    } else {
                        res += b;
                        for(int j = r + 1;j < r_;j++) {
                            L = min(L,p[j]);
                            R = max(R,p[j]);
                        }
                        r = r_;
                    }
                }
            }
        }
        return res;
    }

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:11:13: warning: unused variable 'it' [-Wunused-variable]
   11 |         int it = 0;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 360 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 360 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 416 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 360 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 416 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 64 ms 8192 KB Output is correct
31 Correct 70 ms 8020 KB Output is correct
32 Correct 65 ms 8172 KB Output is correct
33 Correct 77 ms 8192 KB Output is correct
34 Correct 76 ms 8016 KB Output is correct
35 Correct 62 ms 8016 KB Output is correct
36 Correct 74 ms 8020 KB Output is correct
37 Correct 70 ms 8016 KB Output is correct
38 Correct 62 ms 8020 KB Output is correct
39 Correct 67 ms 8020 KB Output is correct
40 Correct 63 ms 8188 KB Output is correct
41 Correct 63 ms 8016 KB Output is correct
42 Correct 64 ms 8020 KB Output is correct
43 Correct 70 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 360 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 416 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 64 ms 8192 KB Output is correct
31 Correct 70 ms 8020 KB Output is correct
32 Correct 65 ms 8172 KB Output is correct
33 Correct 77 ms 8192 KB Output is correct
34 Correct 76 ms 8016 KB Output is correct
35 Correct 62 ms 8016 KB Output is correct
36 Correct 74 ms 8020 KB Output is correct
37 Correct 70 ms 8016 KB Output is correct
38 Correct 62 ms 8020 KB Output is correct
39 Correct 67 ms 8020 KB Output is correct
40 Correct 63 ms 8188 KB Output is correct
41 Correct 63 ms 8016 KB Output is correct
42 Correct 64 ms 8020 KB Output is correct
43 Correct 70 ms 8192 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 344 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 77 ms 14928 KB Output is correct
65 Correct 67 ms 14928 KB Output is correct
66 Correct 67 ms 14928 KB Output is correct
67 Correct 69 ms 14844 KB Output is correct
68 Correct 7 ms 1624 KB Output is correct
69 Correct 15 ms 1628 KB Output is correct
70 Correct 14 ms 1628 KB Output is correct
71 Correct 7 ms 1624 KB Output is correct
72 Correct 7 ms 1628 KB Output is correct
73 Correct 8 ms 1628 KB Output is correct
74 Correct 66 ms 14932 KB Output is correct
75 Correct 67 ms 14828 KB Output is correct
76 Correct 65 ms 14832 KB Output is correct
77 Correct 71 ms 14832 KB Output is correct
78 Correct 65 ms 14928 KB Output is correct
79 Correct 112 ms 14932 KB Output is correct
80 Correct 66 ms 14960 KB Output is correct
81 Correct 77 ms 14932 KB Output is correct
82 Correct 78 ms 14960 KB Output is correct
83 Correct 72 ms 14924 KB Output is correct
84 Correct 84 ms 14956 KB Output is correct
85 Correct 74 ms 14956 KB Output is correct
86 Correct 66 ms 14928 KB Output is correct
87 Correct 70 ms 14960 KB Output is correct
88 Correct 68 ms 14968 KB Output is correct
89 Correct 103 ms 14932 KB Output is correct
90 Correct 67 ms 14824 KB Output is correct
91 Correct 66 ms 14932 KB Output is correct
92 Correct 67 ms 14932 KB Output is correct