Submission #917954

# Submission time Handle Problem Language Result Execution time Memory
917954 2024-01-29T07:42:11 Z nightfal Ancient Books (IOI17_books) C++14
100 / 100
102 ms 20048 KB
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <tuple>
#include <climits>

using namespace std;

bool isSubtask3(int s) {return s==0;}
bool isSubtask4(int n) {return n<=1000;}

pair<int,int> extend(int l, int r, vector<int>& lmost, vector<int>& rmost) {
    while(l > min(lmost[l],lmost[r]) || r < max(rmost[l],rmost[r])) {
        l=min(l,min(lmost[l],lmost[r])); 
        r=max(r,max(rmost[r],rmost[l]));
    }
    return make_pair(l,r);
} 

long long fulltask(vector<int> p, int s) {

    int n = p.size();

    int start; for(start = 0; start < s; start++) if(start!=p[start]) break;
    int end; for(end = n-1; end > s; end--) if(end!=p[end]) break;

    long long distBooks=0;
    for(int i=start; i<=end; i++) distBooks += abs(p[i]-i);

    int l = min(s,p[s]), r=max(s,p[s]); 
    vector<int> lmost(n), rmost(n); lmost[s] = min(s,p[s]); rmost[s] = max(s,p[s]);
    for(int i=s-1; i>=start; i--) {lmost[i] = min(lmost[i+1],p[i]); rmost[i] = max(rmost[i+1],p[i]);}
    for(int i=s+1; i<=end ; i++)  {lmost[i] = min(lmost[i-1],p[i]); rmost[i] = max(rmost[i-1],p[i]);}
    tie(l,r) = extend(l,r,lmost,rmost);

    long long distFree=0, rMoves=0, lMoves=0;

    while(start<l || r<end) {
        int l2, r2;
        if(r<end && (l<=start || rMoves < lMoves)) {
            r++; rMoves++;
            tie(l2,r2) = extend(l,r,lmost,rmost);
            if (l2<l) {distFree += rMoves; rMoves = lMoves = 0;}
        }
        else {
            l--; lMoves++;
            tie(l2,r2) = extend(l,r,lmost,rmost);
            if (r<r2) {distFree += lMoves; rMoves = lMoves = 0;}
        }
        l = l2; r = r2;
    }
    distFree += rMoves + lMoves;
    // printf("totalMoves=%d, jumps=%d\n",totalMoves,jumps);
    return distFree*2+distBooks;
}

long long minimum_walk(std::vector<int> p, int s) {
    // if (isSubtask3(s)) return subtask3(p);
    // if (isSubtask4(s)) return subtask4(p,s);
    return fulltask(p,s);
}
# 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 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 1 ms 344 KB Output is correct
10 Correct 1 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 348 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 1 ms 344 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 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 1 ms 344 KB Output is correct
10 Correct 1 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 348 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 1 ms 344 KB Output is correct
19 Correct 1 ms 348 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 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 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 1 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 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 1 ms 344 KB Output is correct
10 Correct 1 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 348 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 1 ms 344 KB Output is correct
19 Correct 1 ms 348 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 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 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 1 ms 348 KB Output is correct
30 Correct 88 ms 19944 KB Output is correct
31 Correct 82 ms 19936 KB Output is correct
32 Correct 76 ms 19792 KB Output is correct
33 Correct 86 ms 19796 KB Output is correct
34 Correct 92 ms 19940 KB Output is correct
35 Correct 85 ms 19944 KB Output is correct
36 Correct 82 ms 19944 KB Output is correct
37 Correct 81 ms 19940 KB Output is correct
38 Correct 80 ms 19936 KB Output is correct
39 Correct 82 ms 19792 KB Output is correct
40 Correct 81 ms 19952 KB Output is correct
41 Correct 82 ms 19796 KB Output is correct
42 Correct 82 ms 19936 KB Output is correct
43 Correct 81 ms 20048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 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 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 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 440 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 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 1 ms 344 KB Output is correct
10 Correct 1 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 348 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 1 ms 344 KB Output is correct
19 Correct 1 ms 348 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 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 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 1 ms 348 KB Output is correct
30 Correct 88 ms 19944 KB Output is correct
31 Correct 82 ms 19936 KB Output is correct
32 Correct 76 ms 19792 KB Output is correct
33 Correct 86 ms 19796 KB Output is correct
34 Correct 92 ms 19940 KB Output is correct
35 Correct 85 ms 19944 KB Output is correct
36 Correct 82 ms 19944 KB Output is correct
37 Correct 81 ms 19940 KB Output is correct
38 Correct 80 ms 19936 KB Output is correct
39 Correct 82 ms 19792 KB Output is correct
40 Correct 81 ms 19952 KB Output is correct
41 Correct 82 ms 19796 KB Output is correct
42 Correct 82 ms 19936 KB Output is correct
43 Correct 81 ms 20048 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 1 ms 348 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 1 ms 348 KB Output is correct
51 Correct 1 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 348 KB Output is correct
57 Correct 1 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 440 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 82 ms 19796 KB Output is correct
65 Correct 94 ms 19792 KB Output is correct
66 Correct 81 ms 19940 KB Output is correct
67 Correct 100 ms 19792 KB Output is correct
68 Correct 8 ms 2140 KB Output is correct
69 Correct 8 ms 2356 KB Output is correct
70 Correct 9 ms 2396 KB Output is correct
71 Correct 11 ms 2360 KB Output is correct
72 Correct 9 ms 2392 KB Output is correct
73 Correct 11 ms 2396 KB Output is correct
74 Correct 99 ms 19944 KB Output is correct
75 Correct 102 ms 19940 KB Output is correct
76 Correct 81 ms 19956 KB Output is correct
77 Correct 82 ms 19956 KB Output is correct
78 Correct 80 ms 19796 KB Output is correct
79 Correct 80 ms 19988 KB Output is correct
80 Correct 93 ms 19956 KB Output is correct
81 Correct 82 ms 19956 KB Output is correct
82 Correct 84 ms 19952 KB Output is correct
83 Correct 82 ms 19936 KB Output is correct
84 Correct 95 ms 19940 KB Output is correct
85 Correct 80 ms 19788 KB Output is correct
86 Correct 80 ms 19952 KB Output is correct
87 Correct 83 ms 19984 KB Output is correct
88 Correct 95 ms 19792 KB Output is correct
89 Correct 82 ms 19792 KB Output is correct
90 Correct 78 ms 19792 KB Output is correct
91 Correct 79 ms 19792 KB Output is correct
92 Correct 99 ms 19936 KB Output is correct