답안 #986143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
986143 2024-05-19T21:31:20 Z mariaclara 고대 책들 (IOI17_books) C++17
100 / 100
143 ms 58676 KB
#include "books.h"
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair
#define pb push_back
#define fr first
#define sc second

ll minimum_walk(vector<int> p, int s) {
	int n = sz(p), L = n, R = -1;
	vector<int> l(n), r(n);
	vector<bool> vis(n,0);
	vector<pii> aux, range;

	ll ans = 0;
	for(int i = 0; i < n; i++) {
		int x = i, m = i;
		vector<int> cicle;
		while(!vis[x]) {
			vis[x] = 1;
			cicle.pb(x);
			x = p[x];
			m = max(m, x);
		}

		for(int it : cicle) l[it] = i, r[it] = m;

		ans += abs(p[i]-i);
		aux.pb({i,m});
		if(i != m or i == s) L = min(L,i), R = max(R,m);
	}

	for(int i = 0; i < sz(aux);) {
		int l = aux[i].fr, r = aux[i].sc, j = i;
		while(j < sz(aux) and aux[j].fr <= r) {
			r = max(r, aux[j].sc);
			j++;
		}
		i=j;
		range.pb({l,r});
	}

	for(int i = 1; i < sz(range); i++)
		if(range[i-1].fr >= L and range[i].sc <= R)
			ans += 2*(range[i].fr - range[i-1].sc);

	vector<pii> a(n), b(n);
	for(int i = 1; i < n; i++) {
		if(l[i-1] <= l[i]) {
			a[i] = mk(i-1, max(0,l[i] - i + 1));
			continue;
		}
		a[i].fr = a[i-1].fr;
		while(l[a[i].fr] > l[i]) a[i].fr = a[a[i].fr].fr;
		a[i].sc = max(0, l[i] - a[i].fr);
	}

	b[n-1] = mk(n-1,0);
	for(int i = n-2; i >= 0; i--) {
		if(r[i+1] >= r[i]) {
			b[i] = mk(i+1, max(0,i+1 - r[i]));
			continue;
		}
		b[i].fr = b[i+1].fr;
		while(r[b[i].fr] < r[i]) b[i].fr = b[b[i].fr].fr;
		b[i].sc = max(0, b[i].fr - r[i]);
	}

	vector<int> dist(n, n);
	deque<int> fila;
	fila.emplace_front(s);
	dist[s] = 0;
	vis.clear();
	vis.resize(n,0);

	while(!fila.empty()) {
		int x = fila.front();
		fila.pop_front();

		if(vis[x]) continue;
		vis[x] = 1;

		if(dist[a[x].fr] > dist[x] + a[x].sc) {
			if(a[x].sc == 0) fila.emplace_front(a[x].fr);
			else fila.emplace_back(a[x].fr);
			dist[a[x].fr] = dist[x] + a[x].sc;
		}
		if(dist[b[x].fr] > dist[x] + b[x].sc) {
			if(b[x].sc == 0) fila.emplace_front(b[x].fr);
			else fila.emplace_back(b[x].fr);
			dist[b[x].fr] = dist[x] + b[x].sc;
		}
	}

	for(int i = 0; i < sz(range); i++) 
		if(range[i].sc >= s) { R = range[i].sc; break; }

	return ans + 2*dist[R];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 500 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 500 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 432 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 500 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 432 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 141 ms 51024 KB Output is correct
31 Correct 143 ms 50776 KB Output is correct
32 Correct 128 ms 58092 KB Output is correct
33 Correct 132 ms 56880 KB Output is correct
34 Correct 140 ms 56748 KB Output is correct
35 Correct 134 ms 54872 KB Output is correct
36 Correct 124 ms 53436 KB Output is correct
37 Correct 116 ms 50356 KB Output is correct
38 Correct 117 ms 50356 KB Output is correct
39 Correct 117 ms 50356 KB Output is correct
40 Correct 119 ms 50360 KB Output is correct
41 Correct 134 ms 50636 KB Output is correct
42 Correct 127 ms 50460 KB Output is correct
43 Correct 124 ms 50120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 444 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 500 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 432 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 141 ms 51024 KB Output is correct
31 Correct 143 ms 50776 KB Output is correct
32 Correct 128 ms 58092 KB Output is correct
33 Correct 132 ms 56880 KB Output is correct
34 Correct 140 ms 56748 KB Output is correct
35 Correct 134 ms 54872 KB Output is correct
36 Correct 124 ms 53436 KB Output is correct
37 Correct 116 ms 50356 KB Output is correct
38 Correct 117 ms 50356 KB Output is correct
39 Correct 117 ms 50356 KB Output is correct
40 Correct 119 ms 50360 KB Output is correct
41 Correct 134 ms 50636 KB Output is correct
42 Correct 127 ms 50460 KB Output is correct
43 Correct 124 ms 50120 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 444 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 600 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 127 ms 50136 KB Output is correct
65 Correct 117 ms 50248 KB Output is correct
66 Correct 109 ms 50256 KB Output is correct
67 Correct 112 ms 50136 KB Output is correct
68 Correct 14 ms 5336 KB Output is correct
69 Correct 14 ms 5328 KB Output is correct
70 Correct 12 ms 5328 KB Output is correct
71 Correct 13 ms 5344 KB Output is correct
72 Correct 13 ms 5328 KB Output is correct
73 Correct 12 ms 5124 KB Output is correct
74 Correct 137 ms 55752 KB Output is correct
75 Correct 135 ms 55608 KB Output is correct
76 Correct 127 ms 50616 KB Output is correct
77 Correct 123 ms 50156 KB Output is correct
78 Correct 128 ms 50252 KB Output is correct
79 Correct 123 ms 50388 KB Output is correct
80 Correct 131 ms 58676 KB Output is correct
81 Correct 121 ms 50356 KB Output is correct
82 Correct 124 ms 50360 KB Output is correct
83 Correct 135 ms 50120 KB Output is correct
84 Correct 131 ms 50272 KB Output is correct
85 Correct 125 ms 50356 KB Output is correct
86 Correct 120 ms 50612 KB Output is correct
87 Correct 126 ms 50140 KB Output is correct
88 Correct 123 ms 50352 KB Output is correct
89 Correct 137 ms 50280 KB Output is correct
90 Correct 119 ms 50352 KB Output is correct
91 Correct 120 ms 50360 KB Output is correct
92 Correct 121 ms 50352 KB Output is correct