Submission #431691

# Submission time Handle Problem Language Result Execution time Memory
431691 2021-06-17T14:31:24 Z rainboy Ancient Books (IOI17_books) C++
50 / 100
170 ms 18756 KB
#include "books.h"
#include <string.h>

using namespace std;

const int N = 1000000;
const int SMALL = 1000;

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

typedef vector<int> vi;

int dd[N];
int dp[SMALL][SMALL];

long long minimum_walk(vi pp, int s) {
	int n = pp.size(), i, j;
	long long ans;

	ans = 0;
	for (i = 0; i < n; i++)
		if (i < pp[i])
			ans += pp[i] - i, dd[i]++, dd[pp[i]]--;
		else if (i > pp[i])
			ans += i - pp[i], dd[pp[i]]++, dd[i]--;
	if (s == 0) {
		for (i = 1; i < n - 1; i++)
			dd[i] += dd[i - 1];
		i = n - 1;
		while (i >= 0 && pp[i] == i)
			i--;
		for (i--; i >= 0; i--)
			if (dd[i] == 0)
				ans += 2;
	} else {
		int l_, r_, l, r;

		for (i = 0; i < n; i++)
			memset(dp[i], 0x3f, n * sizeof *dp[i]);
		dp[s][s] = 0;
		l_ = r_ = pp[s];
		for (i = s; i >= 0; i--) {
			l_ = min(l_, pp[i]), r_ = max(r_, pp[i]);
			l = l_, r = r_;
			for (j = s; j < n; j++) {
				l = min(l, pp[j]), r = max(r, pp[j]);
				if (i > 0)
					dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] + (l >= i));
				if (j + 1 < n)
					dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (r <= j));
			}
		}
		ans += dp[0][n - 1] * 2;
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 288 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 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 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 288 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 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 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 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 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 284 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 288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 288 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 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 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 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 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 284 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 288 KB Output is correct
30 Correct 147 ms 18636 KB Output is correct
31 Correct 160 ms 18644 KB Output is correct
32 Correct 139 ms 18636 KB Output is correct
33 Correct 134 ms 18684 KB Output is correct
34 Correct 142 ms 18664 KB Output is correct
35 Correct 170 ms 18632 KB Output is correct
36 Correct 143 ms 18636 KB Output is correct
37 Correct 153 ms 18640 KB Output is correct
38 Correct 152 ms 18640 KB Output is correct
39 Correct 147 ms 18636 KB Output is correct
40 Correct 135 ms 18640 KB Output is correct
41 Correct 145 ms 18756 KB Output is correct
42 Correct 155 ms 18724 KB Output is correct
43 Correct 141 ms 18720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4172 KB Output is correct
2 Correct 3 ms 4172 KB Output is correct
3 Incorrect 4 ms 4172 KB 3rd lines differ - on the 1st token, expected: '5920', found: '5922'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 288 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 288 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 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 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 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 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 284 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 288 KB Output is correct
30 Correct 147 ms 18636 KB Output is correct
31 Correct 160 ms 18644 KB Output is correct
32 Correct 139 ms 18636 KB Output is correct
33 Correct 134 ms 18684 KB Output is correct
34 Correct 142 ms 18664 KB Output is correct
35 Correct 170 ms 18632 KB Output is correct
36 Correct 143 ms 18636 KB Output is correct
37 Correct 153 ms 18640 KB Output is correct
38 Correct 152 ms 18640 KB Output is correct
39 Correct 147 ms 18636 KB Output is correct
40 Correct 135 ms 18640 KB Output is correct
41 Correct 145 ms 18756 KB Output is correct
42 Correct 155 ms 18724 KB Output is correct
43 Correct 141 ms 18720 KB Output is correct
44 Correct 4 ms 4172 KB Output is correct
45 Correct 3 ms 4172 KB Output is correct
46 Incorrect 4 ms 4172 KB 3rd lines differ - on the 1st token, expected: '5920', found: '5922'
47 Halted 0 ms 0 KB -