답안 #941527

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941527 2024-03-09T05:13:59 Z beaboss 고대 책들 (IOI17_books) C++14
50 / 100
122 ms 17164 KB
// Source: https://oj.uz/problem/view/IOI17_books
// 
#include "books.h"


#include "bits/stdc++.h"

using namespace std;

#define s second
#define f first
#define pb push_back

typedef long long ll;

typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef vector<int> vi;

#define FOR(i, a, b) for (int i = (a); i<b; i++)

bool ckmin(int& a, int b){ return b < a ? a = b, true : false; }

bool ckmax(int& a, int b){ return b > a ? a = b, true : false; }

const int N = 1e6 + 10;
int ri[N], le[N];
bool vis[N];

ll minimum_walk(vector<int> p, int s) {
	// if (s == 0) {
	ll ans = 0;
	int n = p.size();
	FOR(i, 0, n) {
		// cout << i << endl;
		if (!vis[i]) {
			int l, r;
			r = l = i;
			int cur = p[i];
			while (cur != i) {
				l = min(l, cur);r=max(r, cur);
				cur=p[cur];
			}
			cur = i;
			do {

				ans += abs(p[cur]-cur);
				le[cur] = l;
				ri[cur] = r;
				vis[cur]=true;
				cur=p[cur];
			} while (cur != i);
		}
	}

	int en = n-1;
	while (en != -1 && p[en] == en) en--;
	int st = 0;
	while (st != n && p[st] == st) st++;

	if (st > en) return 0;
	// cout << 'd' << endl;
	// cout << ans << endl;
	ans += 2 * (max(st - s, 0) + max(s - en, 0));
	s = min(max(s, st), en);
	// cout << ans << endl;

	int l = s, r = s;
	// cout << ans << endl;
	while (l > st || r < en) {
		// cout << l << r << endl;
		int right = r, left = l;

		int tr = r;
		int hr = ri[tr];
		int tl = le[tr];
		int cr = 0;
		while (tr < en && (tl >= l || tr != hr)) {
			// cout << tl << tr << endl;
			if (tr == hr) cr+=2;
			tr++;
			tl=min(tl,le[tr]);
			hr = max(hr, ri[tr]);
		}

		right=max(right, tr);
		left=min(left, tl);

		// if (tl >= s) ans += cr; // didn't get through

		tl = l;
		int hl = le[tl];
		tr = ri[tl];
		int cl = 0;
		// cout << hl << tl << endl;
		while (tl > st && (tr <= r || tl != hl)) {
			// cout << hl << tl << endl;
			if (tl == hl) cl+=2;
			tl--;
			tr=max(tr, ri[tl]);
			hl = min(hl, le[tl]);
		}

		left = min(left, tl);
		right = max(right, tr);
		// if (tr <= s) ans += cl; // didn't get through

		// cout << cl << cr << endl;
		// cout << ans << endl;
		if (tl >= l || tr <= r) ans += cr + cl;
		else ans += min(cl, cr);
		// cout << ans << endl;
		l = left, r = right;
	}
	// cout << ans << endl;

	// int r =0;
	// FOR(i, 0, en+1) {
	// 	if (r < i) {
	// 		r++;
	// 		ans+=2;
	// 	}
	// 	r = max(p[i], r);
	// }
	return ans;
	// } else assert(false);
}



// int main() {
// 	ios::sync_with_stdio(false);
// 	cin.tie(nullptr);

// 	// FOR(i, 0, 8) {
// 	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// 	int n = 100;
// 	vi here(n);
// 	FOR(i, 0, n) here[i] = i;
// 	shuffle(here.begin(), here.end(), rng);
// 	FOR(i, 0, n) cout << here[i] << ' ';
// 	cout << endl;
// 	// vi here = {0, 5, 3, 8, 4, 9, 2, 1, 6, 7 };
// 	int res = minimum_walk(here, 4);
// 	int res2 = minimum_walk2(here, 4);
// 	cout << res << ' ' << res2 << endl;
// 	// cout << res << ' ' << res2 << endl;
// 	// }
// }












# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4556 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4556 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4440 KB Output is correct
30 Correct 122 ms 16980 KB Output is correct
31 Correct 107 ms 17164 KB Output is correct
32 Correct 77 ms 17068 KB Output is correct
33 Correct 83 ms 16892 KB Output is correct
34 Correct 78 ms 16908 KB Output is correct
35 Correct 85 ms 16980 KB Output is correct
36 Correct 79 ms 16980 KB Output is correct
37 Correct 84 ms 16896 KB Output is correct
38 Correct 77 ms 16976 KB Output is correct
39 Correct 77 ms 16980 KB Output is correct
40 Correct 82 ms 16980 KB Output is correct
41 Correct 99 ms 16904 KB Output is correct
42 Correct 95 ms 16888 KB Output is correct
43 Correct 78 ms 16976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4540 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4544 KB Output is correct
8 Incorrect 1 ms 4444 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10476'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4556 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4440 KB Output is correct
30 Correct 122 ms 16980 KB Output is correct
31 Correct 107 ms 17164 KB Output is correct
32 Correct 77 ms 17068 KB Output is correct
33 Correct 83 ms 16892 KB Output is correct
34 Correct 78 ms 16908 KB Output is correct
35 Correct 85 ms 16980 KB Output is correct
36 Correct 79 ms 16980 KB Output is correct
37 Correct 84 ms 16896 KB Output is correct
38 Correct 77 ms 16976 KB Output is correct
39 Correct 77 ms 16980 KB Output is correct
40 Correct 82 ms 16980 KB Output is correct
41 Correct 99 ms 16904 KB Output is correct
42 Correct 95 ms 16888 KB Output is correct
43 Correct 78 ms 16976 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4440 KB Output is correct
46 Correct 1 ms 4540 KB Output is correct
47 Correct 1 ms 4444 KB Output is correct
48 Correct 1 ms 4440 KB Output is correct
49 Correct 1 ms 4444 KB Output is correct
50 Correct 1 ms 4544 KB Output is correct
51 Incorrect 1 ms 4444 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10476'
52 Halted 0 ms 0 KB -