Submission #949609

# Submission time Handle Problem Language Result Execution time Memory
949609 2024-03-19T12:28:02 Z qin Ancient Books (IOI17_books) C++17
50 / 100
88 ms 14196 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n")
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define vv vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int inf = 2e09; ll infll = 2e18; int mod = 119<<23|1;
ll minimum_walk(vv<int> p, int s){
		int n = ssize(p);
		vv<int> vis(n, 0);
		int l = 0;
		ll result = 0;
		vv<pii> st;
		for(int i = 0; i < n; ++i) if(!vis[i]){
				//~ printf("%d\n", i);
				int mx = i; vis[i] = ++l; result += abs(i-p[i]);
				for(int u = p[i]; u != i; u = p[u]) vis[u] = l, mx = max(mx, u), result += abs(u-p[u]);
				if(st.empty()) {if(mx != i) st.emplace_back(i, mx);}
				else{
						if(i < st.back().se) st.back().se = max(st.back().se, mx);
						else if(mx != i) st.emplace_back(i, mx);
				}
				//~ printf("%d %lld\n", i, result);
		}
		//~ printf("a\n");
		for(int i = 1; i < ssize(st); ++i) result += 2*(st[i].fi-st[i-1].se);
		int mn = inf;
		for(int i = s; i < n; ++i) if(i != p[i]){ mn = i-s; break; }
		for(int i = s; ~i; --i) if(i != p[i]){ mn = min(s-i, mn); break; }
		if(mn != inf) result += 2*mn;
		return result;
}
#ifdef LOCAL
int main(){
		int T = 1;
		for(++T; --T; ){
				int n, s; scanf("%d%d", &n, &s);
				vv<int> p(n);
				for(int i = 0; i < n; ++i) scanf("%d", &p[i]);
				ll result = minimum_walk(p, s);
				printf("%lld\n", result);
		}
		return 0;
}
#endif
# 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 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 1 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 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 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 1 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 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 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 1 ms 348 KB Output is correct
28 Correct 1 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 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 1 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 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 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 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 88 ms 12120 KB Output is correct
31 Correct 88 ms 12116 KB Output is correct
32 Correct 75 ms 12116 KB Output is correct
33 Correct 78 ms 14196 KB Output is correct
34 Correct 78 ms 14176 KB Output is correct
35 Correct 79 ms 14140 KB Output is correct
36 Correct 83 ms 13176 KB Output is correct
37 Correct 74 ms 12116 KB Output is correct
38 Correct 74 ms 12116 KB Output is correct
39 Correct 73 ms 12112 KB Output is correct
40 Correct 76 ms 12012 KB Output is correct
41 Correct 84 ms 12116 KB Output is correct
42 Correct 82 ms 12028 KB Output is correct
43 Correct 78 ms 12120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 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 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 1 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 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 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 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 88 ms 12120 KB Output is correct
31 Correct 88 ms 12116 KB Output is correct
32 Correct 75 ms 12116 KB Output is correct
33 Correct 78 ms 14196 KB Output is correct
34 Correct 78 ms 14176 KB Output is correct
35 Correct 79 ms 14140 KB Output is correct
36 Correct 83 ms 13176 KB Output is correct
37 Correct 74 ms 12116 KB Output is correct
38 Correct 74 ms 12116 KB Output is correct
39 Correct 73 ms 12112 KB Output is correct
40 Correct 76 ms 12012 KB Output is correct
41 Correct 84 ms 12116 KB Output is correct
42 Correct 82 ms 12028 KB Output is correct
43 Correct 78 ms 12120 KB Output is correct
44 Incorrect 1 ms 348 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -