#include <bits/stdc++.h>
#include "books.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using plpii = pair<ll, pii>;
const int N = 1e6+10;
const ll INF = 1e9;
int par[N], qs[N];
int root(int u) {
if (par[u] == u) return u;
return par[u] = root(par[u]);
}
bool merge(int u, int v) {
u = root(u), v = root(v);
if (u == v) return false;
par[u] = v;
return true;
}
ll minimum_walk(vector<int> p, int s)
{
int n = p.size();
iota(par, par+n, 0);
ll dist = 0;
for (int i = 0; i < n; ++i) {
if (p[i] == -1) continue;
if (p[i] == i) continue;
int u = i;
while (p[u] != -1) {
int v = p[u];
dist += abs(u-v);
p[u] = -1;
qs[min(u, v)] += 1;
qs[max(u, v)] -= 1;
u = v;
}
}
for (int i = 0; i < n-1; ++i) {
if (i > 0) qs[i] += qs[i-1];
if (qs[i]) merge(i, i+1);
}
if (p[s] == s) p[s] = -1;
vector<plpii> E;
for (int i = 0; i < n; ++i) if (p[i] == -1) {
for (int j = i+1; j < n; ++j) if (p[j] == -1) {
E.emplace_back(j-i, pii(i, j));
}
}
sort(E.begin(), E.end());
for (auto e : E) {
int w = e.first, u, v;
tie(u, v) = e.second;
if (merge(u, v)) dist += 2*w;
}
return dist;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
2 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
560 KB |
Output is correct |
6 |
Correct |
3 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
3 ms |
664 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
2 ms |
664 KB |
Output is correct |
13 |
Correct |
2 ms |
664 KB |
Output is correct |
14 |
Correct |
2 ms |
664 KB |
Output is correct |
15 |
Correct |
2 ms |
664 KB |
Output is correct |
16 |
Correct |
2 ms |
664 KB |
Output is correct |
17 |
Correct |
2 ms |
664 KB |
Output is correct |
18 |
Correct |
2 ms |
664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
2 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
560 KB |
Output is correct |
6 |
Correct |
3 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
3 ms |
664 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
2 ms |
664 KB |
Output is correct |
13 |
Correct |
2 ms |
664 KB |
Output is correct |
14 |
Correct |
2 ms |
664 KB |
Output is correct |
15 |
Correct |
2 ms |
664 KB |
Output is correct |
16 |
Correct |
2 ms |
664 KB |
Output is correct |
17 |
Correct |
2 ms |
664 KB |
Output is correct |
18 |
Correct |
2 ms |
664 KB |
Output is correct |
19 |
Correct |
80 ms |
8912 KB |
Output is correct |
20 |
Correct |
155 ms |
8912 KB |
Output is correct |
21 |
Correct |
3 ms |
8912 KB |
Output is correct |
22 |
Correct |
21 ms |
8912 KB |
Output is correct |
23 |
Correct |
22 ms |
8912 KB |
Output is correct |
24 |
Correct |
35 ms |
8912 KB |
Output is correct |
25 |
Correct |
61 ms |
8928 KB |
Output is correct |
26 |
Correct |
78 ms |
8928 KB |
Output is correct |
27 |
Correct |
75 ms |
8928 KB |
Output is correct |
28 |
Correct |
74 ms |
8928 KB |
Output is correct |
29 |
Correct |
25 ms |
8928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
2 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
560 KB |
Output is correct |
6 |
Correct |
3 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
3 ms |
664 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
2 ms |
664 KB |
Output is correct |
13 |
Correct |
2 ms |
664 KB |
Output is correct |
14 |
Correct |
2 ms |
664 KB |
Output is correct |
15 |
Correct |
2 ms |
664 KB |
Output is correct |
16 |
Correct |
2 ms |
664 KB |
Output is correct |
17 |
Correct |
2 ms |
664 KB |
Output is correct |
18 |
Correct |
2 ms |
664 KB |
Output is correct |
19 |
Correct |
80 ms |
8912 KB |
Output is correct |
20 |
Correct |
155 ms |
8912 KB |
Output is correct |
21 |
Correct |
3 ms |
8912 KB |
Output is correct |
22 |
Correct |
21 ms |
8912 KB |
Output is correct |
23 |
Correct |
22 ms |
8912 KB |
Output is correct |
24 |
Correct |
35 ms |
8912 KB |
Output is correct |
25 |
Correct |
61 ms |
8928 KB |
Output is correct |
26 |
Correct |
78 ms |
8928 KB |
Output is correct |
27 |
Correct |
75 ms |
8928 KB |
Output is correct |
28 |
Correct |
74 ms |
8928 KB |
Output is correct |
29 |
Correct |
25 ms |
8928 KB |
Output is correct |
30 |
Execution timed out |
2131 ms |
1045748 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
1045748 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
2 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
560 KB |
Output is correct |
6 |
Correct |
3 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
3 ms |
664 KB |
Output is correct |
10 |
Correct |
3 ms |
664 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
2 ms |
664 KB |
Output is correct |
13 |
Correct |
2 ms |
664 KB |
Output is correct |
14 |
Correct |
2 ms |
664 KB |
Output is correct |
15 |
Correct |
2 ms |
664 KB |
Output is correct |
16 |
Correct |
2 ms |
664 KB |
Output is correct |
17 |
Correct |
2 ms |
664 KB |
Output is correct |
18 |
Correct |
2 ms |
664 KB |
Output is correct |
19 |
Correct |
80 ms |
8912 KB |
Output is correct |
20 |
Correct |
155 ms |
8912 KB |
Output is correct |
21 |
Correct |
3 ms |
8912 KB |
Output is correct |
22 |
Correct |
21 ms |
8912 KB |
Output is correct |
23 |
Correct |
22 ms |
8912 KB |
Output is correct |
24 |
Correct |
35 ms |
8912 KB |
Output is correct |
25 |
Correct |
61 ms |
8928 KB |
Output is correct |
26 |
Correct |
78 ms |
8928 KB |
Output is correct |
27 |
Correct |
75 ms |
8928 KB |
Output is correct |
28 |
Correct |
74 ms |
8928 KB |
Output is correct |
29 |
Correct |
25 ms |
8928 KB |
Output is correct |
30 |
Execution timed out |
2131 ms |
1045748 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |