Submission #832271

# Submission time Handle Problem Language Result Execution time Memory
832271 2023-08-21T08:22:19 Z MadokaMagicaFan Ancient Books (IOI17_books) C++14
12 / 100
1 ms 312 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);
	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 = p[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 += rn[i][0] - newr.back()[1];
			newr.pb(rn[i]);
		} else {
			newr.back()[1] = max(newr.back()[1], rn[i][1]);
		}
	}


	return ans + 2ll * newr.front()[0];
}


#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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 308 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 308 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 308 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 308 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '2082', found: '1435'
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 308 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 308 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '2082', found: '1435'
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 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 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 308 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 308 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '2082', found: '1435'
23 Halted 0 ms 0 KB -