Submission #206638

# Submission time Handle Problem Language Result Execution time Memory
206638 2020-03-04T09:58:37 Z anonymous Ancient Books (IOI17_books) C++14
100 / 100
262 ms 31740 KB
#include "books.h"
#include<iostream>
#define MAXN 1000005
#define LL long long
using namespace std;
int N, S, cyc = 1, eL, eR, Lcur, Rcur;
int L[MAXN], R[MAXN], P[MAXN], C[MAXN];
bool vis[MAXN];
LL ans;
LL minimum_walk(vector<int> p, int s) {
	N = p.size(), eL = N+1, S = s+1, Lcur = S, Rcur = S;
	for (int i=0; i<N; i++) {
        P[i+1]=p[i]+1; //1 index
        ans += (LL) max(p[i] - i, i - p[i]);
	}
	for (int i=1; i<=N; i++) {
        int cur = P[i];
        L[cyc] = 1<<30, R[cyc] = -1<<30;
        while (!vis[cur]) {
            L[cyc] = min(L[cyc], cur);
            R[cyc] = max(R[cyc], cur);
            C[cur] = cyc;
            vis[cur] = true;
            cur = P[cur];
        }
        cyc++;
	}
	for (int i=1; i<=N; i++) {
        if (P[i] != i) {
            eL = i;
            break;
        }
	}
	for (int i=N; i>=1; i--) {
        if (P[i] != i) {
            eR = i;
            break;
        }
	}
	while (Lcur > eL || Rcur < eR) {
        //expand left and right as much as frontier allows
        int pt1 = Lcur, pt2 = Rcur; //[Lcur, Rcur] can be reached
        while (pt1 >= Lcur || pt2 <= Rcur) {
            if (pt1 >= Lcur) {
                Lcur = min(Lcur, L[C[pt1]]);
                Rcur = max(Rcur, R[C[pt1]]);
                pt1--;
            }
            if (pt2 <= Rcur) {
                Lcur = min(Lcur, L[C[pt2]]);
                Rcur = max(Rcur, R[C[pt2]]);
                pt2++;
            }
        }
        //if one boundary is reached
        if (Lcur <= eL && Rcur >= eR) {
            break;
        } else if (Lcur <= eL) {
            ans+=2;
            Rcur++;
        } else if (Rcur >= eR) {
            ans+=2;
            Lcur--;
        } else {
            //test expand right Cost to go beyond
            int tmpL = Lcur, tmpR = Rcur, ptL = Lcur, ptR = Rcur;
            int Lcost = 0, Rcost = 0;
            bool root = true;
            //check R cost:
            while (ptR <= eR) {
                if (ptR > tmpR) {tmpR = ptR, Rcost++;}
                tmpR = max(tmpR, R[C[ptR]]);
                if (L[C[ptR]] < Lcur) {
                    root = false;
                    break;
                }
                ptR++;
            }
            //check L cost:
            while (ptL >= eL) {
                if (ptL < tmpL) {tmpL = ptL, Lcost++;}
                tmpL = min(tmpL, L[C[ptL]]);
                if (R[C[ptL]] > Rcur) {
                    root = false;
                    break;
                }
                ptL--;
            }
            if (root) {
                ans+=2*(Lcost + Rcost);
                break;
            } else if (Lcost > Rcost) {
                Rcur = ptR, Lcur = L[C[ptR]];
                ans+=2*Rcost;
            } else {
                Lcur = ptL, Rcur = R[C[ptL]];
                ans+=2*Lcost;
            }
        }
	}
	return(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 252 ms 24824 KB Output is correct
31 Correct 262 ms 24952 KB Output is correct
32 Correct 156 ms 24824 KB Output is correct
33 Correct 160 ms 24824 KB Output is correct
34 Correct 163 ms 24824 KB Output is correct
35 Correct 170 ms 24824 KB Output is correct
36 Correct 169 ms 24824 KB Output is correct
37 Correct 159 ms 24764 KB Output is correct
38 Correct 163 ms 24824 KB Output is correct
39 Correct 164 ms 24824 KB Output is correct
40 Correct 171 ms 24824 KB Output is correct
41 Correct 198 ms 24824 KB Output is correct
42 Correct 183 ms 24824 KB Output is correct
43 Correct 160 ms 24824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 380 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 380 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 380 KB Output is correct
13 Correct 5 ms 380 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 6 ms 504 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 252 ms 24824 KB Output is correct
31 Correct 262 ms 24952 KB Output is correct
32 Correct 156 ms 24824 KB Output is correct
33 Correct 160 ms 24824 KB Output is correct
34 Correct 163 ms 24824 KB Output is correct
35 Correct 170 ms 24824 KB Output is correct
36 Correct 169 ms 24824 KB Output is correct
37 Correct 159 ms 24764 KB Output is correct
38 Correct 163 ms 24824 KB Output is correct
39 Correct 164 ms 24824 KB Output is correct
40 Correct 171 ms 24824 KB Output is correct
41 Correct 198 ms 24824 KB Output is correct
42 Correct 183 ms 24824 KB Output is correct
43 Correct 160 ms 24824 KB Output is correct
44 Correct 5 ms 376 KB Output is correct
45 Correct 5 ms 376 KB Output is correct
46 Correct 5 ms 380 KB Output is correct
47 Correct 5 ms 376 KB Output is correct
48 Correct 5 ms 376 KB Output is correct
49 Correct 5 ms 376 KB Output is correct
50 Correct 5 ms 376 KB Output is correct
51 Correct 5 ms 376 KB Output is correct
52 Correct 5 ms 380 KB Output is correct
53 Correct 5 ms 376 KB Output is correct
54 Correct 5 ms 376 KB Output is correct
55 Correct 5 ms 380 KB Output is correct
56 Correct 5 ms 380 KB Output is correct
57 Correct 5 ms 376 KB Output is correct
58 Correct 6 ms 504 KB Output is correct
59 Correct 5 ms 376 KB Output is correct
60 Correct 5 ms 376 KB Output is correct
61 Correct 5 ms 376 KB Output is correct
62 Correct 5 ms 376 KB Output is correct
63 Correct 5 ms 376 KB Output is correct
64 Correct 160 ms 31480 KB Output is correct
65 Correct 162 ms 31480 KB Output is correct
66 Correct 172 ms 31480 KB Output is correct
67 Correct 162 ms 31480 KB Output is correct
68 Correct 20 ms 3320 KB Output is correct
69 Correct 19 ms 3448 KB Output is correct
70 Correct 20 ms 3320 KB Output is correct
71 Correct 20 ms 3328 KB Output is correct
72 Correct 20 ms 3320 KB Output is correct
73 Correct 21 ms 3448 KB Output is correct
74 Correct 162 ms 31480 KB Output is correct
75 Correct 180 ms 31608 KB Output is correct
76 Correct 160 ms 31480 KB Output is correct
77 Correct 177 ms 31480 KB Output is correct
78 Correct 170 ms 31480 KB Output is correct
79 Correct 173 ms 31480 KB Output is correct
80 Correct 156 ms 31480 KB Output is correct
81 Correct 161 ms 31480 KB Output is correct
82 Correct 165 ms 31736 KB Output is correct
83 Correct 163 ms 31480 KB Output is correct
84 Correct 162 ms 31480 KB Output is correct
85 Correct 161 ms 31740 KB Output is correct
86 Correct 159 ms 31480 KB Output is correct
87 Correct 174 ms 31480 KB Output is correct
88 Correct 168 ms 31608 KB Output is correct
89 Correct 167 ms 31608 KB Output is correct
90 Correct 167 ms 31736 KB Output is correct
91 Correct 169 ms 31480 KB Output is correct
92 Correct 170 ms 31480 KB Output is correct