답안 #832293

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832293 2023-08-21T08:30:43 Z MadokaMagicaFan 고대 책들 (IOI17_books) C++14
0 / 100
1 ms 468 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 (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 Incorrect 0 ms 468 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 468 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 468 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 468 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 468 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -