#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);
}
ll l = s, r = s;
ll nl = l, nr = r;
do
{
if (nl >= 0 && (uf.border(nl).first < l || uf.border(nl).second > r))
{
safecost += 2 * (l - nl);
nl = l = min(l, uf.border(nl).first);
nr = r = max(r, uf.border(nl).second);
}
if (nr < N && (uf.border(nr).first < l || uf.border(nr).second > r))
{
safecost += 2 * (nr - r);
nl = l = min(l, uf.border(nr).first);
nr = r = max(r, uf.border(nr).second);
}
--nl, ++nr;
} while (L < l || r < R);
return safecost;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
300 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
300 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
257 ms |
53980 KB |
Output is correct |
31 |
Correct |
253 ms |
53996 KB |
Output is correct |
32 |
Correct |
106 ms |
53980 KB |
Output is correct |
33 |
Correct |
115 ms |
58192 KB |
Output is correct |
34 |
Correct |
112 ms |
58196 KB |
Output is correct |
35 |
Correct |
119 ms |
58172 KB |
Output is correct |
36 |
Correct |
135 ms |
58184 KB |
Output is correct |
37 |
Correct |
122 ms |
54528 KB |
Output is correct |
38 |
Correct |
120 ms |
54220 KB |
Output is correct |
39 |
Correct |
119 ms |
54124 KB |
Output is correct |
40 |
Correct |
137 ms |
53976 KB |
Output is correct |
41 |
Correct |
179 ms |
53992 KB |
Output is correct |
42 |
Correct |
180 ms |
54092 KB |
Output is correct |
43 |
Correct |
121 ms |
58172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22144' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
300 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
257 ms |
53980 KB |
Output is correct |
31 |
Correct |
253 ms |
53996 KB |
Output is correct |
32 |
Correct |
106 ms |
53980 KB |
Output is correct |
33 |
Correct |
115 ms |
58192 KB |
Output is correct |
34 |
Correct |
112 ms |
58196 KB |
Output is correct |
35 |
Correct |
119 ms |
58172 KB |
Output is correct |
36 |
Correct |
135 ms |
58184 KB |
Output is correct |
37 |
Correct |
122 ms |
54528 KB |
Output is correct |
38 |
Correct |
120 ms |
54220 KB |
Output is correct |
39 |
Correct |
119 ms |
54124 KB |
Output is correct |
40 |
Correct |
137 ms |
53976 KB |
Output is correct |
41 |
Correct |
179 ms |
53992 KB |
Output is correct |
42 |
Correct |
180 ms |
54092 KB |
Output is correct |
43 |
Correct |
121 ms |
58172 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22144' |
50 |
Halted |
0 ms |
0 KB |
- |