#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 && nl != P[nl] && (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);
}
else if (nr < N && nr != P[nr] && (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);
}
else
--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 |
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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 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 |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
249 ms |
47180 KB |
Output is correct |
31 |
Correct |
247 ms |
47180 KB |
Output is correct |
32 |
Correct |
90 ms |
47208 KB |
Output is correct |
33 |
Correct |
112 ms |
51608 KB |
Output is correct |
34 |
Correct |
106 ms |
51420 KB |
Output is correct |
35 |
Correct |
114 ms |
51416 KB |
Output is correct |
36 |
Correct |
127 ms |
51420 KB |
Output is correct |
37 |
Correct |
115 ms |
47892 KB |
Output is correct |
38 |
Correct |
118 ms |
47436 KB |
Output is correct |
39 |
Correct |
117 ms |
47436 KB |
Output is correct |
40 |
Correct |
124 ms |
47212 KB |
Output is correct |
41 |
Correct |
202 ms |
47268 KB |
Output is correct |
42 |
Correct |
149 ms |
47212 KB |
Output is correct |
43 |
Correct |
114 ms |
51424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
2 |
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 |
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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
249 ms |
47180 KB |
Output is correct |
31 |
Correct |
247 ms |
47180 KB |
Output is correct |
32 |
Correct |
90 ms |
47208 KB |
Output is correct |
33 |
Correct |
112 ms |
51608 KB |
Output is correct |
34 |
Correct |
106 ms |
51420 KB |
Output is correct |
35 |
Correct |
114 ms |
51416 KB |
Output is correct |
36 |
Correct |
127 ms |
51420 KB |
Output is correct |
37 |
Correct |
115 ms |
47892 KB |
Output is correct |
38 |
Correct |
118 ms |
47436 KB |
Output is correct |
39 |
Correct |
117 ms |
47436 KB |
Output is correct |
40 |
Correct |
124 ms |
47212 KB |
Output is correct |
41 |
Correct |
202 ms |
47268 KB |
Output is correct |
42 |
Correct |
149 ms |
47212 KB |
Output is correct |
43 |
Correct |
114 ms |
51424 KB |
Output is correct |
44 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
45 |
Halted |
0 ms |
0 KB |
- |