#include "books.h"
#define N 1000005
#define f first
#define ss second
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ll ans = 0;
ll n, maior[N], menor[N], qtd = 0, ok[N], en;
int pai[N], peso[N];
int find(int x)
{
if(x == pai[x]) return x;
return pai[x] = find(pai[x]);
}
void join(int a, int b)
{
a = find(a), b = find(b);
if(a == b) return;
if(peso[a] > peso[b]) pai[b] = a;
else if(peso[a] < peso[b]) pai[a] = b;
else pai[a] = b, peso[b] ++;
}
vector<int> g;
void dfs(ll x, ll &mx, ll &mn)
{
mx = max(mx, maior[x]);
if(ok[x]) return;
ok[x] = 1;
dfs(g[x], mx, mn);
}
ll minimum_walk(vector<int> p, int s)
{
n = p.size();
g = p;
for(ll i = 0; i < n; i++) ans += abs(i - p[i]);
for(int i = 0; i < n; i++) maior[i] = menor[i] = i;
for(ll i = n - 1; i >= 0; i--)
{
if(p[i] != i)
{
en = i;
break;
}
}
vector<pii> sweep;
for(int i = 0; i <= en; i++)
{
if(ok[i]) continue;
dfs(i, maior[i], menor[i]);
sweep.push_back({menor[i], maior[i]});
}
sort(sweep.begin(), sweep.end());
for(int i = 0; i < (int)sweep.size(); i++) pai[i] = i;
pii a = {-1, -1};
for(int i = 0; i < (int)sweep.size(); i++)
{
auto b = sweep[i];
if(b.f <= a.f) join(i, a.ss);
a = max(a, {b.ss, i});
//for(int j = 0; j < i; j++)
//{
//}
}
set<int> q;
for(int i = 0; i < (int)sweep.size(); i++)
{
q.insert(find(i));
}
ll qtd = q.size();
//cout<<q.size()<<"\n";
return ans + 2*(qtd - 1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
440 KB |
Output is correct |
3 |
Correct |
3 ms |
516 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
516 KB |
Output is correct |
6 |
Correct |
3 ms |
548 KB |
Output is correct |
7 |
Correct |
2 ms |
548 KB |
Output is correct |
8 |
Correct |
2 ms |
548 KB |
Output is correct |
9 |
Correct |
3 ms |
548 KB |
Output is correct |
10 |
Correct |
3 ms |
552 KB |
Output is correct |
11 |
Correct |
2 ms |
656 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
2 ms |
832 KB |
Output is correct |
16 |
Correct |
2 ms |
832 KB |
Output is correct |
17 |
Correct |
2 ms |
832 KB |
Output is correct |
18 |
Correct |
2 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
440 KB |
Output is correct |
3 |
Correct |
3 ms |
516 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
516 KB |
Output is correct |
6 |
Correct |
3 ms |
548 KB |
Output is correct |
7 |
Correct |
2 ms |
548 KB |
Output is correct |
8 |
Correct |
2 ms |
548 KB |
Output is correct |
9 |
Correct |
3 ms |
548 KB |
Output is correct |
10 |
Correct |
3 ms |
552 KB |
Output is correct |
11 |
Correct |
2 ms |
656 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
2 ms |
832 KB |
Output is correct |
16 |
Correct |
2 ms |
832 KB |
Output is correct |
17 |
Correct |
2 ms |
832 KB |
Output is correct |
18 |
Correct |
2 ms |
832 KB |
Output is correct |
19 |
Correct |
3 ms |
832 KB |
Output is correct |
20 |
Correct |
2 ms |
832 KB |
Output is correct |
21 |
Correct |
2 ms |
832 KB |
Output is correct |
22 |
Correct |
3 ms |
832 KB |
Output is correct |
23 |
Correct |
3 ms |
832 KB |
Output is correct |
24 |
Correct |
2 ms |
832 KB |
Output is correct |
25 |
Correct |
3 ms |
832 KB |
Output is correct |
26 |
Correct |
2 ms |
832 KB |
Output is correct |
27 |
Correct |
3 ms |
832 KB |
Output is correct |
28 |
Correct |
2 ms |
832 KB |
Output is correct |
29 |
Correct |
3 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
440 KB |
Output is correct |
3 |
Correct |
3 ms |
516 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
516 KB |
Output is correct |
6 |
Correct |
3 ms |
548 KB |
Output is correct |
7 |
Correct |
2 ms |
548 KB |
Output is correct |
8 |
Correct |
2 ms |
548 KB |
Output is correct |
9 |
Correct |
3 ms |
548 KB |
Output is correct |
10 |
Correct |
3 ms |
552 KB |
Output is correct |
11 |
Correct |
2 ms |
656 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
2 ms |
832 KB |
Output is correct |
16 |
Correct |
2 ms |
832 KB |
Output is correct |
17 |
Correct |
2 ms |
832 KB |
Output is correct |
18 |
Correct |
2 ms |
832 KB |
Output is correct |
19 |
Correct |
3 ms |
832 KB |
Output is correct |
20 |
Correct |
2 ms |
832 KB |
Output is correct |
21 |
Correct |
2 ms |
832 KB |
Output is correct |
22 |
Correct |
3 ms |
832 KB |
Output is correct |
23 |
Correct |
3 ms |
832 KB |
Output is correct |
24 |
Correct |
2 ms |
832 KB |
Output is correct |
25 |
Correct |
3 ms |
832 KB |
Output is correct |
26 |
Correct |
2 ms |
832 KB |
Output is correct |
27 |
Correct |
3 ms |
832 KB |
Output is correct |
28 |
Correct |
2 ms |
832 KB |
Output is correct |
29 |
Correct |
3 ms |
832 KB |
Output is correct |
30 |
Correct |
316 ms |
35780 KB |
Output is correct |
31 |
Correct |
328 ms |
35964 KB |
Output is correct |
32 |
Correct |
204 ms |
35964 KB |
Output is correct |
33 |
Correct |
497 ms |
76708 KB |
Output is correct |
34 |
Correct |
448 ms |
83432 KB |
Output is correct |
35 |
Correct |
418 ms |
83432 KB |
Output is correct |
36 |
Correct |
291 ms |
83432 KB |
Output is correct |
37 |
Correct |
214 ms |
83432 KB |
Output is correct |
38 |
Correct |
198 ms |
83432 KB |
Output is correct |
39 |
Correct |
212 ms |
83432 KB |
Output is correct |
40 |
Correct |
208 ms |
83432 KB |
Output is correct |
41 |
Correct |
228 ms |
89956 KB |
Output is correct |
42 |
Correct |
236 ms |
96700 KB |
Output is correct |
43 |
Correct |
274 ms |
112240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
112240 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 |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
440 KB |
Output is correct |
3 |
Correct |
3 ms |
516 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
516 KB |
Output is correct |
6 |
Correct |
3 ms |
548 KB |
Output is correct |
7 |
Correct |
2 ms |
548 KB |
Output is correct |
8 |
Correct |
2 ms |
548 KB |
Output is correct |
9 |
Correct |
3 ms |
548 KB |
Output is correct |
10 |
Correct |
3 ms |
552 KB |
Output is correct |
11 |
Correct |
2 ms |
656 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
2 ms |
832 KB |
Output is correct |
16 |
Correct |
2 ms |
832 KB |
Output is correct |
17 |
Correct |
2 ms |
832 KB |
Output is correct |
18 |
Correct |
2 ms |
832 KB |
Output is correct |
19 |
Correct |
3 ms |
832 KB |
Output is correct |
20 |
Correct |
2 ms |
832 KB |
Output is correct |
21 |
Correct |
2 ms |
832 KB |
Output is correct |
22 |
Correct |
3 ms |
832 KB |
Output is correct |
23 |
Correct |
3 ms |
832 KB |
Output is correct |
24 |
Correct |
2 ms |
832 KB |
Output is correct |
25 |
Correct |
3 ms |
832 KB |
Output is correct |
26 |
Correct |
2 ms |
832 KB |
Output is correct |
27 |
Correct |
3 ms |
832 KB |
Output is correct |
28 |
Correct |
2 ms |
832 KB |
Output is correct |
29 |
Correct |
3 ms |
832 KB |
Output is correct |
30 |
Correct |
316 ms |
35780 KB |
Output is correct |
31 |
Correct |
328 ms |
35964 KB |
Output is correct |
32 |
Correct |
204 ms |
35964 KB |
Output is correct |
33 |
Correct |
497 ms |
76708 KB |
Output is correct |
34 |
Correct |
448 ms |
83432 KB |
Output is correct |
35 |
Correct |
418 ms |
83432 KB |
Output is correct |
36 |
Correct |
291 ms |
83432 KB |
Output is correct |
37 |
Correct |
214 ms |
83432 KB |
Output is correct |
38 |
Correct |
198 ms |
83432 KB |
Output is correct |
39 |
Correct |
212 ms |
83432 KB |
Output is correct |
40 |
Correct |
208 ms |
83432 KB |
Output is correct |
41 |
Correct |
228 ms |
89956 KB |
Output is correct |
42 |
Correct |
236 ms |
96700 KB |
Output is correct |
43 |
Correct |
274 ms |
112240 KB |
Output is correct |
44 |
Incorrect |
2 ms |
112240 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |