답안 #832296

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832296 2023-08-21T08:33:07 Z MadokaMagicaFan 고대 책들 (IOI17_books) C++14
50 / 100
116 ms 17896 KB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define sz(v) ((int)v.size())
#define pb push_back

const int N = 1e6;
bitset<N> v;

ll minimum_walk(vector<int> p, int s) {
	/* assumes s = 0 */
	ll ans = 0;
	int n = sz(p);
	v = 0;
	for (int i = 0; i < n; ++i)
		ans += (ll)abs(i - p[i]);

	vector<array<ll,2>> rn, newr;
	for (int i = 0; i < n; ++i) {
		int l, r, x;
		l = r = i;
		if (v[i]) continue;
		x = i;
		while (!v[x]) {
			v[x] = 1;
			l = min(l, x);
			r = max(r, x);
			x = p[x];
		}
		if (l != r)
			rn.pb({l,r});
	}

	if (sz(rn) == 0) return 0;

	sort(rn.begin(), rn.end());
	newr.pb(rn.front());
	for (int i = 1; i < sz(rn); ++i) {
		if (rn[i][0] > newr.back()[1]) {
			ans += 2ll*(rn[i][0] - newr.back()[1]);
			newr.pb(rn[i]);
		} else {
			newr.back()[1] = max(newr.back()[1], rn[i][1]);
		}
	}

	int j = 0;
	for (auto u : newr) {
		if (u[0] <= s && u[1] >= s)
			j = 1;
	}
	if (s < newr[0][0] || s > newr.back()[1])
		j = 1;

	if (j^1) return ans;

	ll mv = n;
	for (int i = 0; i < n; ++i) {
		if (i != p[i])
			mv = min(mv, (ll) abs(i-s));
	}


	return ans + 2ll * mv;
}


#ifdef ONPC
int main() {
	int n, s;
	cin >> n >> s;
	vector<int> p(n);
	for(int i = 0; i < n; ++i)
		cin >> p[i];

	cout << minimum_walk(p, s) << endl;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 544 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 0 ms 468 KB Output is correct
28 Correct 0 ms 468 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 544 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 0 ms 468 KB Output is correct
28 Correct 0 ms 468 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 91 ms 8364 KB Output is correct
31 Correct 93 ms 8312 KB Output is correct
32 Correct 76 ms 8320 KB Output is correct
33 Correct 110 ms 17516 KB Output is correct
34 Correct 97 ms 17464 KB Output is correct
35 Correct 116 ms 17896 KB Output is correct
36 Correct 94 ms 14176 KB Output is correct
37 Correct 79 ms 8948 KB Output is correct
38 Correct 78 ms 8576 KB Output is correct
39 Correct 98 ms 8560 KB Output is correct
40 Correct 79 ms 8308 KB Output is correct
41 Correct 86 ms 8308 KB Output is correct
42 Correct 85 ms 8320 KB Output is correct
43 Correct 99 ms 12512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 468 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 544 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 0 ms 468 KB Output is correct
28 Correct 0 ms 468 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 91 ms 8364 KB Output is correct
31 Correct 93 ms 8312 KB Output is correct
32 Correct 76 ms 8320 KB Output is correct
33 Correct 110 ms 17516 KB Output is correct
34 Correct 97 ms 17464 KB Output is correct
35 Correct 116 ms 17896 KB Output is correct
36 Correct 94 ms 14176 KB Output is correct
37 Correct 79 ms 8948 KB Output is correct
38 Correct 78 ms 8576 KB Output is correct
39 Correct 98 ms 8560 KB Output is correct
40 Correct 79 ms 8308 KB Output is correct
41 Correct 86 ms 8308 KB Output is correct
42 Correct 85 ms 8320 KB Output is correct
43 Correct 99 ms 12512 KB Output is correct
44 Incorrect 0 ms 468 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -