답안 #838333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838333 2023-08-26T15:33:37 Z Johann 고대 책들 (IOI17_books) C++14
100 / 100
262 ms 63828 KB
#include "books.h"
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const ll INF = 1LL << 60;
int N;
ll safecost;
vi vis, P;

struct unionfind
{
	vi arr;
	vpii stuff;
	void init(int size)
	{
		arr.resize(size);
		stuff.resize(size);
		for (int i = 0; i < size; ++i)
			arr[i] = i, stuff[i] = {i, i};
	}
	int find(int i) { return arr[i] = (arr[i] == i) ? i : find(arr[i]); }
	void unite(int a, int b)
	{
		a = find(a), b = find(b);
		stuff[a] = {min(stuff[a].first, stuff[b].first), max(stuff[a].second, stuff[b].second)};
		arr[b] = a;
	}
	pii border(int i) { return stuff[find(i)]; }
};
unionfind uf;

void cycle(int i, int &mini, int &maxi, int cycleCnt)
{
	if (vis[i] != -1)
		return;
	mini = min(mini, i);
	maxi = max(maxi, i);
	vis[i] = cycleCnt;
	safecost += (ll)abs(P[i] - i);
	uf.unite(i, P[i]);
	cycle(P[i], mini, maxi, cycleCnt);
}

vvpii adj;

long long minimum_walk(std::vector<int> p, int s)
{
	N = sz(p);
	P.resize(N);
	for (int i = 0; i < N; ++i)
		P[i] = p[i];
	uf.init(N);

	vis.assign(N, -1);
	ll L = INF, R = -1;
	safecost = 0;
	vpii borders;
	for (int i = 0, mini, maxi; i < N; ++i)
	{
		if (P[i] != i)
			L = min(L, (ll)i), R = max(R, (ll)i);
		if (vis[i] == -1 && P[i] != i)
		{
			mini = maxi = i;
			cycle(i, mini, maxi, sz(borders));
			borders.push_back({mini, maxi});
		}
	}
	if (borders.empty())
		return 0;
	if (s <= L || s >= R)
	{
		if (s >= R)
		{
			s = N - s;
			for (int i = 0, l, r; i < sz(borders); ++i)
			{
				tie(l, r) = borders[i];
				borders[i] = {N - r, N - l};
			}
		}
		sort(all(borders));

		ll pos = s;
		for (pii b : borders)
		{
			safecost += 2 * max(0LL, b.first - pos);
			pos = max(pos, b.second);
		}

		return safecost;
	}

	stack<int> st;
	for (int i = R; i >= L; --i)
	{
		while (!st.empty() && st.top() <= uf.border(i).second)
		{
			int b = st.top();
			if (b != uf.border(b).first)
				uf.unite(i, b);
			st.pop();
		}
		st.push(i);
	}

	pii largestSpanningBow = uf.border(s);
	for (int i = 0; i < N; ++i)
		if (uf.border(i).first < largestSpanningBow.first && largestSpanningBow.second < uf.border(i).second)
			largestSpanningBow = uf.border(i);

	ll l, r;
	tie(l, r) = uf.border(s);
	while (largestSpanningBow.first < l || r < largestSpanningBow.second)
	{
		safecost += 2, --l, ++r;
		l = min(l, min(uf.border(l).first, uf.border(r).first));
		r = max(r, max(uf.border(l).second, uf.border(r).second));
	}

	ll pos = uf.border(L).second;
	for (int i = pos + 1; i <= R; ++i)
	{
		if (i > pos)
			safecost += 2, pos = i;
		pos = max(pos, uf.border(pos).second);
	}

	return safecost;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 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 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 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 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 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 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 234 ms 47212 KB Output is correct
31 Correct 262 ms 47180 KB Output is correct
32 Correct 85 ms 47208 KB Output is correct
33 Correct 101 ms 51440 KB Output is correct
34 Correct 103 ms 51420 KB Output is correct
35 Correct 104 ms 51416 KB Output is correct
36 Correct 111 ms 51420 KB Output is correct
37 Correct 105 ms 47848 KB Output is correct
38 Correct 109 ms 47476 KB Output is correct
39 Correct 110 ms 47480 KB Output is correct
40 Correct 118 ms 47228 KB Output is correct
41 Correct 160 ms 47224 KB Output is correct
42 Correct 142 ms 47228 KB Output is correct
43 Correct 109 ms 51428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 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 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 234 ms 47212 KB Output is correct
31 Correct 262 ms 47180 KB Output is correct
32 Correct 85 ms 47208 KB Output is correct
33 Correct 101 ms 51440 KB Output is correct
34 Correct 103 ms 51420 KB Output is correct
35 Correct 104 ms 51416 KB Output is correct
36 Correct 111 ms 51420 KB Output is correct
37 Correct 105 ms 47848 KB Output is correct
38 Correct 109 ms 47476 KB Output is correct
39 Correct 110 ms 47480 KB Output is correct
40 Correct 118 ms 47228 KB Output is correct
41 Correct 160 ms 47224 KB Output is correct
42 Correct 142 ms 47228 KB Output is correct
43 Correct 109 ms 51428 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 352 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 296 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 114 ms 56148 KB Output is correct
65 Correct 112 ms 56152 KB Output is correct
66 Correct 130 ms 54628 KB Output is correct
67 Correct 136 ms 54336 KB Output is correct
68 Correct 16 ms 5880 KB Output is correct
69 Correct 13 ms 5808 KB Output is correct
70 Correct 12 ms 5972 KB Output is correct
71 Correct 12 ms 5892 KB Output is correct
72 Correct 11 ms 5884 KB Output is correct
73 Correct 12 ms 5492 KB Output is correct
74 Correct 119 ms 59564 KB Output is correct
75 Correct 129 ms 59604 KB Output is correct
76 Correct 130 ms 58196 KB Output is correct
77 Correct 124 ms 58172 KB Output is correct
78 Correct 131 ms 62284 KB Output is correct
79 Correct 147 ms 62292 KB Output is correct
80 Correct 85 ms 53992 KB Output is correct
81 Correct 120 ms 63828 KB Output is correct
82 Correct 121 ms 63792 KB Output is correct
83 Correct 137 ms 58368 KB Output is correct
84 Correct 159 ms 58204 KB Output is correct
85 Correct 137 ms 56164 KB Output is correct
86 Correct 133 ms 55132 KB Output is correct
87 Correct 117 ms 58456 KB Output is correct
88 Correct 122 ms 62316 KB Output is correct
89 Correct 131 ms 62296 KB Output is correct
90 Correct 133 ms 54636 KB Output is correct
91 Correct 135 ms 54248 KB Output is correct
92 Correct 136 ms 54048 KB Output is correct