Submission #337703

# Submission time Handle Problem Language Result Execution time Memory
337703 2020-12-21T12:20:58 Z pit4h Ancient Books (IOI17_books) C++14
50 / 100
183 ms 35948 KB
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
ll solve(vector<int> p, int force) {
	int n = p.size(), s = 0, t = n-1;

	while(s+1<=force && p[s]==s) s++;
	while(t-1>=force && p[t]==t) t--;

	ll ans = 0;

	vector<int> cnt(n), L(n), R(n);
	for(int i=s; i<=t; ++i) {
		if(p[i] > i) cnt[i]++, cnt[p[i]]--;
	}
	int delta = 0;
	for(int i=s; i<=t; ++i) {
		delta += cnt[i];
		L[i] = delta;
	}

	cnt = vector<int>(n, 0);
	for(int i=s; i<=t; ++i) {
		if(p[i] < i) {
			cnt[i-1]++;
			if(p[i]-1>=0) cnt[p[i]-1]--;
		}
	}
	delta = 0;
	for(int i=t; i>=s; --i) {
		delta += cnt[i];
		R[i] = delta;
	}
	
	for(int i=s; i<t; ++i) {
		ans += max(2, max(L[i], R[i])*2);
	}
	return ans;
}
ll minimum_walk(vector<int> p, int s) {
	int n = p.size();
	vector<int> pref(n), suf(n);
	pref[0] = p[0], suf[n-1] = p[n-1];
	for(int i=1; i<n; ++i) {
		pref[i] = max(pref[i-1], p[i]);
	}
	for(int i=n-2; i>=0; --i) {
		suf[i] = min(suf[i+1], p[i]);
	}
	if(s==0 || p[s]!=s || (pref[s-1]<s && suf[s-1]>s)) {
		return solve(p, s);
	}
	ll ans = 1e18+1;
	vector<int> q = p;
	for(int i=0; i<n; ++i) {
		if(i==s) continue;
		q[i] = s;
		q[s] = p[i];
		ans = min(ans, solve(q, s));
		q[i] = p[i];
		q[s] = p[s];
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 166 ms 35820 KB Output is correct
31 Correct 183 ms 35820 KB Output is correct
32 Correct 169 ms 35820 KB Output is correct
33 Correct 166 ms 35820 KB Output is correct
34 Correct 160 ms 35820 KB Output is correct
35 Correct 163 ms 35820 KB Output is correct
36 Correct 167 ms 35820 KB Output is correct
37 Correct 164 ms 35820 KB Output is correct
38 Correct 172 ms 35820 KB Output is correct
39 Correct 168 ms 35948 KB Output is correct
40 Correct 165 ms 35820 KB Output is correct
41 Correct 164 ms 35820 KB Output is correct
42 Correct 166 ms 35820 KB Output is correct
43 Correct 162 ms 35744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 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 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 166 ms 35820 KB Output is correct
31 Correct 183 ms 35820 KB Output is correct
32 Correct 169 ms 35820 KB Output is correct
33 Correct 166 ms 35820 KB Output is correct
34 Correct 160 ms 35820 KB Output is correct
35 Correct 163 ms 35820 KB Output is correct
36 Correct 167 ms 35820 KB Output is correct
37 Correct 164 ms 35820 KB Output is correct
38 Correct 172 ms 35820 KB Output is correct
39 Correct 168 ms 35948 KB Output is correct
40 Correct 165 ms 35820 KB Output is correct
41 Correct 164 ms 35820 KB Output is correct
42 Correct 166 ms 35820 KB Output is correct
43 Correct 162 ms 35744 KB Output is correct
44 Incorrect 1 ms 364 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -