Submission #423870

# Submission time Handle Problem Language Result Execution time Memory
423870 2021-06-11T13:33:49 Z Rudy420 Ancient Books (IOI17_books) C++17
100 / 100
216 ms 24656 KB
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define lg length()
#define pb push_back
#define INF 2000000005
#define LINF 1000000000000000005
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(rng);}
#include "books.h"

long long minimum_walk(vector<int> p, int s) {
	long long sum = 0;
	int L = -1,R = -1;
	int n = sz(p);
	for(int i=0;i<n;i++) sum += abs(i - p[i]);
	for(int i=0;i<n;i++){
		if(p[i]!=i){
			L = i;
			break;
		}
	}
	if(L == -1) return 0;
	for(int i=n-1;i>=0;i--){
		if(p[i]!=i){
			R = i;
			break;
		}
	}
	L = min(L, s);
	R = max(R, s);
	vector<int> v(n, 0);
	int l=s,r=s;
	queue<int> q;
	q.push(s);
	long long ans = 0;
	while(l != L || r != R){
		while(q.size()){
			int i = q.front();
			q.pop();
			if(!v[i]){
				int ln=i,rn=i;
				v[i] = 1;
				int j = p[i];
				while(j != i){
					v[j] = 1;
					ln = min(ln, j);
					rn = max(rn, j);
					j = p[j];
				}
				for(int j=ln;j<l;j++) q.push(j);
				for(int j=r+1;j<=rn;j++) q.push(j);
				l = min(l, ln); r = max(r, rn);
			}
		}

		long long cl = 0, cr = 0;
		vector<int> qL, qR;
		int sl = -1, sr = -1;
		int oldl = l, oldr = r;
		while(l != L){
			l--;
			q.push(l);
			cl += 2;
			while(q.size()){
				int i = q.front();
				q.pop();
				if(!v[i]){
					int ln=i,rn=i;
					v[i] = 1;
					qL.push_back(i);
					int j = p[i];
					while(j != i){
						v[j] = 1;
						qL.push_back(j);
						ln = min(ln, j);
						rn = max(rn, j);
						j = p[j];
					}

					if(rn > r){
						sl = i;
						break;
					}

					for(int j=ln;j<l;j++) q.push(j);
					for(int j=r+1;j<=rn;j++) q.push(j);
					l = min(l, ln); r = max(r, rn);
				}
			}
			if(sl != -1) break;
		}

		for(int i : qL) v[i] = 0;
		while(sz(q)) q.pop();
		l = oldl; r = oldr;
		while(r != R){
			r++;
			q.push(r);
			cr += 2;
			while(q.size()){
				int i = q.front();
				q.pop();
				if(!v[i]){
					int ln=i,rn=i;
					v[i] = 1;
					qR.push_back(i);
					int j = p[i];
					while(j != i){
						v[j] = 1;
						qR.push_back(j);
						ln = min(ln, j);
						rn = max(rn, j);
						j = p[j];
					}

					if(ln < l){
						sr = i;
						break;
					}

					for(int j=ln;j<l;j++) q.push(j);
					for(int j=r+1;j<=rn;j++) q.push(j);
					l = min(l, ln); r = max(r, rn);
				}
			}
			if(sr != -1) break;
		}

		for(int i : qR) v[i] = 0;
		while(sz(q)) q.pop();
		l = oldl; r = oldr;

		if(sl == -1){
			ans += cl + cr;
			break;
		}
		else{
			if(cl < cr) ans += cl, q.push(sl);
			else ans += cr, q.push(sr);
		}

	}

	return ans + sum;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 195 ms 16888 KB Output is correct
31 Correct 200 ms 16908 KB Output is correct
32 Correct 137 ms 8772 KB Output is correct
33 Correct 144 ms 16984 KB Output is correct
34 Correct 146 ms 16984 KB Output is correct
35 Correct 148 ms 16948 KB Output is correct
36 Correct 157 ms 17004 KB Output is correct
37 Correct 138 ms 16948 KB Output is correct
38 Correct 142 ms 17000 KB Output is correct
39 Correct 144 ms 16956 KB Output is correct
40 Correct 147 ms 17080 KB Output is correct
41 Correct 161 ms 18996 KB Output is correct
42 Correct 164 ms 18512 KB Output is correct
43 Correct 137 ms 16924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 195 ms 16888 KB Output is correct
31 Correct 200 ms 16908 KB Output is correct
32 Correct 137 ms 8772 KB Output is correct
33 Correct 144 ms 16984 KB Output is correct
34 Correct 146 ms 16984 KB Output is correct
35 Correct 148 ms 16948 KB Output is correct
36 Correct 157 ms 17004 KB Output is correct
37 Correct 138 ms 16948 KB Output is correct
38 Correct 142 ms 17000 KB Output is correct
39 Correct 144 ms 16956 KB Output is correct
40 Correct 147 ms 17080 KB Output is correct
41 Correct 161 ms 18996 KB Output is correct
42 Correct 164 ms 18512 KB Output is correct
43 Correct 137 ms 16924 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 296 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 296 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 163 ms 23380 KB Output is correct
65 Correct 184 ms 23140 KB Output is correct
66 Correct 156 ms 23980 KB Output is correct
67 Correct 156 ms 24384 KB Output is correct
68 Correct 16 ms 2632 KB Output is correct
69 Correct 15 ms 2632 KB Output is correct
70 Correct 15 ms 2680 KB Output is correct
71 Correct 17 ms 2632 KB Output is correct
72 Correct 17 ms 2668 KB Output is correct
73 Correct 16 ms 2632 KB Output is correct
74 Correct 149 ms 24656 KB Output is correct
75 Correct 147 ms 23732 KB Output is correct
76 Correct 143 ms 22852 KB Output is correct
77 Correct 137 ms 22744 KB Output is correct
78 Correct 173 ms 22800 KB Output is correct
79 Correct 163 ms 21860 KB Output is correct
80 Correct 122 ms 14788 KB Output is correct
81 Correct 216 ms 20304 KB Output is correct
82 Correct 201 ms 21048 KB Output is correct
83 Correct 177 ms 18756 KB Output is correct
84 Correct 172 ms 18648 KB Output is correct
85 Correct 183 ms 18644 KB Output is correct
86 Correct 152 ms 18756 KB Output is correct
87 Correct 189 ms 18764 KB Output is correct
88 Correct 178 ms 18760 KB Output is correct
89 Correct 181 ms 18664 KB Output is correct
90 Correct 149 ms 18760 KB Output is correct
91 Correct 151 ms 18844 KB Output is correct
92 Correct 206 ms 19252 KB Output is correct