답안 #69601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
69601 2018-08-21T09:43:58 Z Noam527 고대 책들 (IOI17_books) C++17
100 / 100
525 ms 226596 KB
#include "books.h"
#include <bits/stdc++.h>
#define endl '\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7, inf = 1e9 + 7;
const ll hs = 199;
const ldb eps = 1e-9, pi = acos(-1);
using namespace std;

int n;
vector<int> a, vis, to;

int cycle(int st) {
	vis[st] = 1;
	int rtn = st;
	st = a[st];
	while (!vis[st]) {
		rtn = max(rtn, st);
		vis[st] = 1;
		st = a[st];
	}
	return rtn;
}
vector<int> cycle2(int st) {
	vis[st] = 1;
	vector<int> rtn = { st };
	st = a[st];
	while (!vis[st]) {
		rtn.push_back(st);
		vis[st] = 1;
		st = a[st];
	}
	return rtn;
}

ll minimum_walk(vector<int> p, int s) {
	n = p.size();
	a = p;
	ll sum = 0;
	for (int i = 0; i < n; i++)
		sum += abs(i - a[i]);

	int l = 0, r = n - 1;
	while (l < n && a[l] == l) l++;
	if (l == n) return 0;
	while (a[r] == r) r--;



	vis.resize(n, 0);
	to.resize(n, -1);
	for (int i = l; i <= r; i++)
		if (!vis[i])
			to[i] = cycle(i);
	vector<pair<int, int>> st, range(n);
	for (int i = l; i <= r; i++)
		if (to[i] != -1) {
			if (!st.size()) st.push_back({ i, to[i] });
			else {
				if (st.back().second < i) st.push_back({ i, to[i] });
				else st.back().second = max(st.back().second, to[i]);
			}
		}

	ll rtn = sum + 2LL * ((int)st.size() - 1);
	if (s < l) return rtn + 2 * (l - s);
	if (r < s) return rtn + 2 * (s - r);

	for (auto &i : vis) i = 0;
	vector<int> tmp;
	queue<int> q;
	int optl, optr, x;
	for (const auto &i : st) {
		if (i.first <= s && s <= i.second) {
			ll add = 0;
			for (int j = i.first; j <= i.second; j++) {
				if (!vis[j]) {
					tmp = cycle2(j);
					for (const auto &ind : tmp)
						range[ind] = { j, to[j] };
				}
			}
			q.push(s);
			optl = optr = s;
			while (optl != i.first || optr != i.second) {
				while (!q.empty()) {
					x = q.front(), q.pop();
					if (range[x].first < optl || optr < range[x].second) {
						for (int j = range[x].first; j < optl; j++)
							q.push(j);
						for (int j = optr + 1; j <= range[x].second; j++)
							q.push(j);
						optl = min(optl, range[x].first);
						optr = max(optr, range[x].second);
					}
				}
				if (optl != i.first || optr != i.second) {
					add++;
					if (i.first < optl) q.push(optl - 1), optl--;
					if (optr < i.second) q.push(optr + 1), optr++;
				}
			}
			return rtn + 2 * add;
		}
	}
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:110:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 536 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 536 KB Output is correct
12 Correct 3 ms 536 KB Output is correct
13 Correct 2 ms 536 KB Output is correct
14 Correct 3 ms 544 KB Output is correct
15 Correct 3 ms 544 KB Output is correct
16 Correct 2 ms 544 KB Output is correct
17 Correct 2 ms 544 KB Output is correct
18 Correct 3 ms 544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 536 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 536 KB Output is correct
12 Correct 3 ms 536 KB Output is correct
13 Correct 2 ms 536 KB Output is correct
14 Correct 3 ms 544 KB Output is correct
15 Correct 3 ms 544 KB Output is correct
16 Correct 2 ms 544 KB Output is correct
17 Correct 2 ms 544 KB Output is correct
18 Correct 3 ms 544 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 2 ms 588 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 3 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 3 ms 624 KB Output is correct
25 Correct 2 ms 628 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 536 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 536 KB Output is correct
12 Correct 3 ms 536 KB Output is correct
13 Correct 2 ms 536 KB Output is correct
14 Correct 3 ms 544 KB Output is correct
15 Correct 3 ms 544 KB Output is correct
16 Correct 2 ms 544 KB Output is correct
17 Correct 2 ms 544 KB Output is correct
18 Correct 3 ms 544 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 2 ms 588 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 3 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 3 ms 624 KB Output is correct
25 Correct 2 ms 628 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 644 KB Output is correct
30 Correct 525 ms 39840 KB Output is correct
31 Correct 508 ms 45768 KB Output is correct
32 Correct 156 ms 45768 KB Output is correct
33 Correct 228 ms 63292 KB Output is correct
34 Correct 200 ms 70020 KB Output is correct
35 Correct 197 ms 71776 KB Output is correct
36 Correct 188 ms 71776 KB Output is correct
37 Correct 194 ms 71776 KB Output is correct
38 Correct 185 ms 71776 KB Output is correct
39 Correct 187 ms 71776 KB Output is correct
40 Correct 196 ms 71776 KB Output is correct
41 Correct 295 ms 71776 KB Output is correct
42 Correct 254 ms 71776 KB Output is correct
43 Correct 293 ms 71776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 71776 KB Output is correct
2 Correct 3 ms 71776 KB Output is correct
3 Correct 3 ms 71776 KB Output is correct
4 Correct 2 ms 71776 KB Output is correct
5 Correct 3 ms 71776 KB Output is correct
6 Correct 2 ms 71776 KB Output is correct
7 Correct 4 ms 71776 KB Output is correct
8 Correct 3 ms 71776 KB Output is correct
9 Correct 3 ms 71776 KB Output is correct
10 Correct 3 ms 71776 KB Output is correct
11 Correct 3 ms 71776 KB Output is correct
12 Correct 3 ms 71776 KB Output is correct
13 Correct 3 ms 71776 KB Output is correct
14 Correct 2 ms 71776 KB Output is correct
15 Correct 2 ms 71776 KB Output is correct
16 Correct 4 ms 71776 KB Output is correct
17 Correct 3 ms 71776 KB Output is correct
18 Correct 3 ms 71776 KB Output is correct
19 Correct 2 ms 71776 KB Output is correct
20 Correct 4 ms 71776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 536 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 3 ms 536 KB Output is correct
8 Correct 2 ms 536 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 2 ms 536 KB Output is correct
11 Correct 2 ms 536 KB Output is correct
12 Correct 3 ms 536 KB Output is correct
13 Correct 2 ms 536 KB Output is correct
14 Correct 3 ms 544 KB Output is correct
15 Correct 3 ms 544 KB Output is correct
16 Correct 2 ms 544 KB Output is correct
17 Correct 2 ms 544 KB Output is correct
18 Correct 3 ms 544 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 2 ms 588 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 3 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 3 ms 624 KB Output is correct
25 Correct 2 ms 628 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 644 KB Output is correct
30 Correct 525 ms 39840 KB Output is correct
31 Correct 508 ms 45768 KB Output is correct
32 Correct 156 ms 45768 KB Output is correct
33 Correct 228 ms 63292 KB Output is correct
34 Correct 200 ms 70020 KB Output is correct
35 Correct 197 ms 71776 KB Output is correct
36 Correct 188 ms 71776 KB Output is correct
37 Correct 194 ms 71776 KB Output is correct
38 Correct 185 ms 71776 KB Output is correct
39 Correct 187 ms 71776 KB Output is correct
40 Correct 196 ms 71776 KB Output is correct
41 Correct 295 ms 71776 KB Output is correct
42 Correct 254 ms 71776 KB Output is correct
43 Correct 293 ms 71776 KB Output is correct
44 Correct 3 ms 71776 KB Output is correct
45 Correct 3 ms 71776 KB Output is correct
46 Correct 3 ms 71776 KB Output is correct
47 Correct 2 ms 71776 KB Output is correct
48 Correct 3 ms 71776 KB Output is correct
49 Correct 2 ms 71776 KB Output is correct
50 Correct 4 ms 71776 KB Output is correct
51 Correct 3 ms 71776 KB Output is correct
52 Correct 3 ms 71776 KB Output is correct
53 Correct 3 ms 71776 KB Output is correct
54 Correct 3 ms 71776 KB Output is correct
55 Correct 3 ms 71776 KB Output is correct
56 Correct 3 ms 71776 KB Output is correct
57 Correct 2 ms 71776 KB Output is correct
58 Correct 2 ms 71776 KB Output is correct
59 Correct 4 ms 71776 KB Output is correct
60 Correct 3 ms 71776 KB Output is correct
61 Correct 3 ms 71776 KB Output is correct
62 Correct 2 ms 71776 KB Output is correct
63 Correct 4 ms 71776 KB Output is correct
64 Correct 267 ms 78172 KB Output is correct
65 Correct 258 ms 84164 KB Output is correct
66 Correct 255 ms 88732 KB Output is correct
67 Correct 257 ms 97396 KB Output is correct
68 Correct 26 ms 97396 KB Output is correct
69 Correct 23 ms 97396 KB Output is correct
70 Correct 41 ms 97396 KB Output is correct
71 Correct 26 ms 97396 KB Output is correct
72 Correct 25 ms 97396 KB Output is correct
73 Correct 26 ms 97396 KB Output is correct
74 Correct 191 ms 113340 KB Output is correct
75 Correct 196 ms 120020 KB Output is correct
76 Correct 244 ms 122496 KB Output is correct
77 Correct 241 ms 129240 KB Output is correct
78 Correct 275 ms 133204 KB Output is correct
79 Correct 362 ms 139952 KB Output is correct
80 Correct 181 ms 139952 KB Output is correct
81 Correct 231 ms 152644 KB Output is correct
82 Correct 247 ms 159188 KB Output is correct
83 Correct 255 ms 165736 KB Output is correct
84 Correct 285 ms 172480 KB Output is correct
85 Correct 280 ms 179368 KB Output is correct
86 Correct 264 ms 185948 KB Output is correct
87 Correct 297 ms 192580 KB Output is correct
88 Correct 331 ms 199472 KB Output is correct
89 Correct 324 ms 206088 KB Output is correct
90 Correct 341 ms 212908 KB Output is correct
91 Correct 248 ms 219576 KB Output is correct
92 Correct 233 ms 226596 KB Output is correct