// M
#include<bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
const int N = 1000006;
int n, st, P[N], A[N], mn[N], mx[N];
int Find(int v)
{
return (P[v] < 0 ? v : (P[v] = Find(P[v])));
}
inline void Merge(int v, int u)
{
v = Find(v);
u = Find(u);
if (v == u)
return ;
P[u] = v;
mn[v] = min(mn[v], mn[u]);
mx[v] = max(mx[v], mx[u]);
}
long long minimum_walk(vector < int > _A, int _st)
{
n = (int)_A.size();
st = _st + 1;
for (int i = 1; i <= n; i ++)
A[i] = _A[i - 1] + 1;
ll SM = 0, tot = 0;
for (int i = 1; i <= n; i ++)
SM += abs(A[i] - i);
memset(P, -1, sizeof(P));
for (int i = 1; i <= n; i ++)
mn[i] = mx[i] = i;
set < int > ST;
for (int i = 1; i <= n; i ++)
ST.insert(i);
for (int i = 1; i <= n; i ++)
if (i != A[i])
{
int l = min(i, A[i]);
int r = max(i, A[i]);
auto it = ST.lower_bound(l);
while (it != ST.end() && * it < r)
Merge(* it, (* it) + 1), it = ST.erase(it);
}
vector < pair < int , int > > vec;
for (int i = 1; i <= n; i ++)
if (Find(i) == i && i != A[i])
vec.push_back({mn[i], mx[i]});
sort(vec.begin(), vec.end());
while (vec.size() > 1 && vec[(int)vec.size() - 2].second >= st)
tot += vec.back().first - vec[(int)vec.size() - 2].second, vec.pop_back();
if (vec.size() && vec.back().first >= st)
tot += vec.back().first - st, vec.pop_back();
reverse(vec.begin(), vec.end());
while (vec.size() > 1 && vec[(int)vec.size() - 2].first <= st)
tot += vec[(int)vec.size() - 2].first - vec.back().second, vec.pop_back();
if (vec.size() && vec.back().second <= st)
tot += st - vec.back().second, vec.pop_back();
if (vec.size())
assert(vec.size() == 1 && vec[0].first <= st && st <= vec[0].second);
tot = tot * 2 + SM;
return tot;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4224 KB |
Output is correct |
2 |
Correct |
3 ms |
4224 KB |
Output is correct |
3 |
Correct |
3 ms |
4224 KB |
Output is correct |
4 |
Correct |
3 ms |
4224 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
3 ms |
4224 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
2 ms |
4224 KB |
Output is correct |
14 |
Correct |
3 ms |
4224 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
3 ms |
4224 KB |
Output is correct |
17 |
Correct |
3 ms |
4224 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4224 KB |
Output is correct |
2 |
Correct |
3 ms |
4224 KB |
Output is correct |
3 |
Correct |
3 ms |
4224 KB |
Output is correct |
4 |
Correct |
3 ms |
4224 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
3 ms |
4224 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
2 ms |
4224 KB |
Output is correct |
14 |
Correct |
3 ms |
4224 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
3 ms |
4224 KB |
Output is correct |
17 |
Correct |
3 ms |
4224 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
3 ms |
4352 KB |
Output is correct |
20 |
Correct |
3 ms |
4352 KB |
Output is correct |
21 |
Correct |
3 ms |
4352 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4352 KB |
Output is correct |
24 |
Correct |
3 ms |
4352 KB |
Output is correct |
25 |
Correct |
3 ms |
4352 KB |
Output is correct |
26 |
Correct |
3 ms |
4352 KB |
Output is correct |
27 |
Correct |
3 ms |
4352 KB |
Output is correct |
28 |
Correct |
3 ms |
4352 KB |
Output is correct |
29 |
Correct |
3 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4224 KB |
Output is correct |
2 |
Correct |
3 ms |
4224 KB |
Output is correct |
3 |
Correct |
3 ms |
4224 KB |
Output is correct |
4 |
Correct |
3 ms |
4224 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
3 ms |
4224 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
2 ms |
4224 KB |
Output is correct |
14 |
Correct |
3 ms |
4224 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
3 ms |
4224 KB |
Output is correct |
17 |
Correct |
3 ms |
4224 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
3 ms |
4352 KB |
Output is correct |
20 |
Correct |
3 ms |
4352 KB |
Output is correct |
21 |
Correct |
3 ms |
4352 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4352 KB |
Output is correct |
24 |
Correct |
3 ms |
4352 KB |
Output is correct |
25 |
Correct |
3 ms |
4352 KB |
Output is correct |
26 |
Correct |
3 ms |
4352 KB |
Output is correct |
27 |
Correct |
3 ms |
4352 KB |
Output is correct |
28 |
Correct |
3 ms |
4352 KB |
Output is correct |
29 |
Correct |
3 ms |
4352 KB |
Output is correct |
30 |
Correct |
439 ms |
72824 KB |
Output is correct |
31 |
Correct |
449 ms |
72764 KB |
Output is correct |
32 |
Correct |
384 ms |
72824 KB |
Output is correct |
33 |
Correct |
527 ms |
75036 KB |
Output is correct |
34 |
Correct |
542 ms |
75116 KB |
Output is correct |
35 |
Correct |
536 ms |
75060 KB |
Output is correct |
36 |
Correct |
517 ms |
74096 KB |
Output is correct |
37 |
Correct |
501 ms |
72772 KB |
Output is correct |
38 |
Correct |
499 ms |
72952 KB |
Output is correct |
39 |
Correct |
520 ms |
72952 KB |
Output is correct |
40 |
Correct |
523 ms |
72824 KB |
Output is correct |
41 |
Correct |
493 ms |
72832 KB |
Output is correct |
42 |
Correct |
503 ms |
72884 KB |
Output is correct |
43 |
Correct |
456 ms |
72832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4352 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4224 KB |
Output is correct |
2 |
Correct |
3 ms |
4224 KB |
Output is correct |
3 |
Correct |
3 ms |
4224 KB |
Output is correct |
4 |
Correct |
3 ms |
4224 KB |
Output is correct |
5 |
Correct |
3 ms |
4224 KB |
Output is correct |
6 |
Correct |
3 ms |
4224 KB |
Output is correct |
7 |
Correct |
3 ms |
4224 KB |
Output is correct |
8 |
Correct |
3 ms |
4224 KB |
Output is correct |
9 |
Correct |
3 ms |
4224 KB |
Output is correct |
10 |
Correct |
3 ms |
4224 KB |
Output is correct |
11 |
Correct |
3 ms |
4224 KB |
Output is correct |
12 |
Correct |
3 ms |
4224 KB |
Output is correct |
13 |
Correct |
2 ms |
4224 KB |
Output is correct |
14 |
Correct |
3 ms |
4224 KB |
Output is correct |
15 |
Correct |
3 ms |
4224 KB |
Output is correct |
16 |
Correct |
3 ms |
4224 KB |
Output is correct |
17 |
Correct |
3 ms |
4224 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
3 ms |
4352 KB |
Output is correct |
20 |
Correct |
3 ms |
4352 KB |
Output is correct |
21 |
Correct |
3 ms |
4352 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4352 KB |
Output is correct |
24 |
Correct |
3 ms |
4352 KB |
Output is correct |
25 |
Correct |
3 ms |
4352 KB |
Output is correct |
26 |
Correct |
3 ms |
4352 KB |
Output is correct |
27 |
Correct |
3 ms |
4352 KB |
Output is correct |
28 |
Correct |
3 ms |
4352 KB |
Output is correct |
29 |
Correct |
3 ms |
4352 KB |
Output is correct |
30 |
Correct |
439 ms |
72824 KB |
Output is correct |
31 |
Correct |
449 ms |
72764 KB |
Output is correct |
32 |
Correct |
384 ms |
72824 KB |
Output is correct |
33 |
Correct |
527 ms |
75036 KB |
Output is correct |
34 |
Correct |
542 ms |
75116 KB |
Output is correct |
35 |
Correct |
536 ms |
75060 KB |
Output is correct |
36 |
Correct |
517 ms |
74096 KB |
Output is correct |
37 |
Correct |
501 ms |
72772 KB |
Output is correct |
38 |
Correct |
499 ms |
72952 KB |
Output is correct |
39 |
Correct |
520 ms |
72952 KB |
Output is correct |
40 |
Correct |
523 ms |
72824 KB |
Output is correct |
41 |
Correct |
493 ms |
72832 KB |
Output is correct |
42 |
Correct |
503 ms |
72884 KB |
Output is correct |
43 |
Correct |
456 ms |
72832 KB |
Output is correct |
44 |
Incorrect |
3 ms |
4352 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |