#include "books.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+5;
typedef long long ll;
typedef pair<int, int> pii;
bool vis[MAXN];
int uf[MAXN];
vector<int> cont[MAXN];
// pii range[MAXN];
// vector<int> in_comp[MAXN];
int par[MAXN];
int dist[MAXN];
int n;
int _abs(int v) {
return v < 0 ? -v : v;
}
int find(int i) {
return uf[i] == i ? i : uf[i] = find(uf[i]);
}
void merge(int i, int j) {
i = find(i);
j = find(j);
if (i == j) return;
// assert(i != j);
if (cont[i].size() < cont[j].size()) swap(i, j);
for (int v: cont[j]) {
cont[i].push_back(v);
uf[v] = i;
}
cont[j].clear();
uf[j] = i;
// range[i].first = min(range[i].first, range[j].first);
// range[i].second = max(range[i].second, range[j].second);
}
void upd(int cur, queue<int> &q) {
int cdist = 1+dist[find(cur)];
if (cur && dist[find(cur-1)] > cdist) {
assert(dist[find(cur-1)] == MAXN);
dist[find(cur-1)] = cdist;
q.push(find(cur-1));
}
if (cur < n-1 && dist[find(cur+1)] > cdist) {
assert(dist[find(cur+1)] == MAXN);
dist[find(cur+1)] = cdist;
q.push(find(cur+1));
}
}
ll minimum_walk(vector<int> p, int s) {
ll ans = 0;
n = p.size();
for (int i = 0; i < n; i++) ans += _abs(p[i]-i);
if (ans == 0) return 0;
vector<pii> intervals;
for (int i = 0; i < n; i++) {
if (vis[i]) continue;
int cur = i;
int l = i, r = i;
do {
l = min(l, cur);
r = max(r, cur);
uf[cur] = i;
cont[i].push_back(cur);
vis[cur] = 1;
cur = p[cur];
} while (cur != i);
if (l == r && i == s) intervals.push_back(pii(l, r));
sort(cont[i].begin(), cont[i].end());
if (r > l) {
for (int j = 0; j < cont[i].size()-1; j++) intervals.push_back(pii(cont[i][j], cont[i][j+1]));
}
// range[i] = pii(l, r);
}
sort(intervals.begin(), intervals.end()); // they are probably already sorted
fill(dist, dist+n, MAXN);
int curr = intervals[0].second;
vector<int> stck;
for (int i = 0; i < intervals.size(); i++) {
while (!stck.empty() && stck.back() < intervals[i].first) {
int v = stck.back(); stck.pop_back();
if (stck.empty()) dist[find(v)] = 0;
// else par[find(v)] = find(stck.back());
}
while (!stck.empty() && stck.back() <= intervals[i].second) {
int v = stck.back(); stck.pop_back();
merge(v, intervals[i].second);
}
stck.push_back(intervals[i].second);
// assert(curr != intervals[i].first);
if (curr > intervals[i].first) {
curr = max(curr, intervals[i].second);
continue;
}
ans += 2*(intervals[i].first-curr);
curr = intervals[i].second;
}
// for (int i = 1; i < stck.size(); i++) par[find(stck[i])] = find(stck[i-1]);
dist[find(stck[0])] = 0;
queue<int> q;
for (int i = 0; i < n; i++) {
if (dist[i] == 0) dist[find(i)] = 0;
}
for (int i = 0; i < n; i++) {
if (i == find(i) && dist[i] == 0) q.push(i);
}
while (!q.empty()) {
int v = q.front(); q.pop();
for (int e: cont[v]) upd(e, q);
}
// return 0;
// int dist = 0;
// s = find(s);
// while (par[s] != -1) {
// int nxt = find(par[s]);
// dist += min(range[nxt].second-range[s].second, range[s].first-range[nxt].first);
// s = nxt;
// }
return ans+2*dist[find(s)];
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int j = 0; j < cont[i].size()-1; j++) intervals.push_back(pii(cont[i][j], cont[i][j+1]));
| ~~^~~~~~~~~~~~~~~~~~
books.cpp:88:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23732 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23688 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23732 KB |
Output is correct |
16 |
Correct |
12 ms |
23700 KB |
Output is correct |
17 |
Correct |
12 ms |
23700 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23732 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23688 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23732 KB |
Output is correct |
16 |
Correct |
12 ms |
23700 KB |
Output is correct |
17 |
Correct |
12 ms |
23700 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23704 KB |
Output is correct |
22 |
Correct |
12 ms |
23796 KB |
Output is correct |
23 |
Correct |
12 ms |
23792 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
12 ms |
23800 KB |
Output is correct |
27 |
Correct |
11 ms |
23836 KB |
Output is correct |
28 |
Correct |
11 ms |
23828 KB |
Output is correct |
29 |
Correct |
12 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23732 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23688 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23732 KB |
Output is correct |
16 |
Correct |
12 ms |
23700 KB |
Output is correct |
17 |
Correct |
12 ms |
23700 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23704 KB |
Output is correct |
22 |
Correct |
12 ms |
23796 KB |
Output is correct |
23 |
Correct |
12 ms |
23792 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
12 ms |
23800 KB |
Output is correct |
27 |
Correct |
11 ms |
23836 KB |
Output is correct |
28 |
Correct |
11 ms |
23828 KB |
Output is correct |
29 |
Correct |
12 ms |
23840 KB |
Output is correct |
30 |
Correct |
278 ms |
54048 KB |
Output is correct |
31 |
Correct |
259 ms |
55572 KB |
Output is correct |
32 |
Correct |
88 ms |
31620 KB |
Output is correct |
33 |
Correct |
193 ms |
65952 KB |
Output is correct |
34 |
Correct |
197 ms |
65952 KB |
Output is correct |
35 |
Correct |
192 ms |
63388 KB |
Output is correct |
36 |
Correct |
200 ms |
59032 KB |
Output is correct |
37 |
Correct |
200 ms |
56468 KB |
Output is correct |
38 |
Correct |
208 ms |
55960 KB |
Output is correct |
39 |
Correct |
216 ms |
55840 KB |
Output is correct |
40 |
Correct |
226 ms |
55340 KB |
Output is correct |
41 |
Correct |
278 ms |
53604 KB |
Output is correct |
42 |
Correct |
240 ms |
55852 KB |
Output is correct |
43 |
Correct |
258 ms |
65760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23736 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23724 KB |
Output is correct |
5 |
Correct |
13 ms |
23844 KB |
Output is correct |
6 |
Correct |
14 ms |
23840 KB |
Output is correct |
7 |
Correct |
12 ms |
23760 KB |
Output is correct |
8 |
Correct |
13 ms |
23844 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23832 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23732 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23688 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23732 KB |
Output is correct |
16 |
Correct |
12 ms |
23700 KB |
Output is correct |
17 |
Correct |
12 ms |
23700 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23836 KB |
Output is correct |
21 |
Correct |
11 ms |
23704 KB |
Output is correct |
22 |
Correct |
12 ms |
23796 KB |
Output is correct |
23 |
Correct |
12 ms |
23792 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
12 ms |
23800 KB |
Output is correct |
27 |
Correct |
11 ms |
23836 KB |
Output is correct |
28 |
Correct |
11 ms |
23828 KB |
Output is correct |
29 |
Correct |
12 ms |
23840 KB |
Output is correct |
30 |
Correct |
278 ms |
54048 KB |
Output is correct |
31 |
Correct |
259 ms |
55572 KB |
Output is correct |
32 |
Correct |
88 ms |
31620 KB |
Output is correct |
33 |
Correct |
193 ms |
65952 KB |
Output is correct |
34 |
Correct |
197 ms |
65952 KB |
Output is correct |
35 |
Correct |
192 ms |
63388 KB |
Output is correct |
36 |
Correct |
200 ms |
59032 KB |
Output is correct |
37 |
Correct |
200 ms |
56468 KB |
Output is correct |
38 |
Correct |
208 ms |
55960 KB |
Output is correct |
39 |
Correct |
216 ms |
55840 KB |
Output is correct |
40 |
Correct |
226 ms |
55340 KB |
Output is correct |
41 |
Correct |
278 ms |
53604 KB |
Output is correct |
42 |
Correct |
240 ms |
55852 KB |
Output is correct |
43 |
Correct |
258 ms |
65760 KB |
Output is correct |
44 |
Correct |
12 ms |
23736 KB |
Output is correct |
45 |
Correct |
12 ms |
23764 KB |
Output is correct |
46 |
Correct |
12 ms |
23764 KB |
Output is correct |
47 |
Correct |
15 ms |
23724 KB |
Output is correct |
48 |
Correct |
13 ms |
23844 KB |
Output is correct |
49 |
Correct |
14 ms |
23840 KB |
Output is correct |
50 |
Correct |
12 ms |
23760 KB |
Output is correct |
51 |
Correct |
13 ms |
23844 KB |
Output is correct |
52 |
Correct |
12 ms |
23764 KB |
Output is correct |
53 |
Correct |
12 ms |
23764 KB |
Output is correct |
54 |
Correct |
12 ms |
23764 KB |
Output is correct |
55 |
Correct |
12 ms |
23764 KB |
Output is correct |
56 |
Correct |
12 ms |
23764 KB |
Output is correct |
57 |
Correct |
13 ms |
23764 KB |
Output is correct |
58 |
Correct |
11 ms |
23764 KB |
Output is correct |
59 |
Correct |
12 ms |
23832 KB |
Output is correct |
60 |
Correct |
11 ms |
23764 KB |
Output is correct |
61 |
Correct |
12 ms |
23764 KB |
Output is correct |
62 |
Correct |
12 ms |
23764 KB |
Output is correct |
63 |
Correct |
12 ms |
23756 KB |
Output is correct |
64 |
Correct |
210 ms |
66424 KB |
Output is correct |
65 |
Correct |
216 ms |
70272 KB |
Output is correct |
66 |
Correct |
204 ms |
58672 KB |
Output is correct |
67 |
Correct |
231 ms |
58168 KB |
Output is correct |
68 |
Correct |
31 ms |
27832 KB |
Output is correct |
69 |
Correct |
38 ms |
27696 KB |
Output is correct |
70 |
Correct |
32 ms |
27980 KB |
Output is correct |
71 |
Correct |
31 ms |
28476 KB |
Output is correct |
72 |
Correct |
28 ms |
28804 KB |
Output is correct |
73 |
Correct |
32 ms |
27476 KB |
Output is correct |
74 |
Correct |
198 ms |
68676 KB |
Output is correct |
75 |
Correct |
197 ms |
68804 KB |
Output is correct |
76 |
Correct |
270 ms |
68436 KB |
Output is correct |
77 |
Correct |
273 ms |
68556 KB |
Output is correct |
78 |
Correct |
223 ms |
66080 KB |
Output is correct |
79 |
Correct |
225 ms |
65984 KB |
Output is correct |
80 |
Correct |
95 ms |
34276 KB |
Output is correct |
81 |
Correct |
156 ms |
66096 KB |
Output is correct |
82 |
Correct |
154 ms |
66156 KB |
Output is correct |
83 |
Correct |
201 ms |
62500 KB |
Output is correct |
84 |
Correct |
206 ms |
62904 KB |
Output is correct |
85 |
Correct |
199 ms |
59628 KB |
Output is correct |
86 |
Correct |
205 ms |
60016 KB |
Output is correct |
87 |
Correct |
161 ms |
72484 KB |
Output is correct |
88 |
Correct |
177 ms |
68404 KB |
Output is correct |
89 |
Correct |
194 ms |
63212 KB |
Output is correct |
90 |
Correct |
198 ms |
61220 KB |
Output is correct |
91 |
Correct |
200 ms |
57768 KB |
Output is correct |
92 |
Correct |
227 ms |
59608 KB |
Output is correct |