#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e6 + 20;
int par[maxn];
int root(int v)
{
return par[v] < 0? v : par[v] = root(par[v]);
}
bool merge(int v , int u)
{
v = root(v) , u = root(u);
if(v == u)
return 0;
par[v] = u;
return 1;
}
int cnt[maxn];
long long minimum_walk(vector<int> p, int s)
{
memset(par , -1 , sizeof par);
ll res = 0;
int n = p.size();
vector<int> val;
for(int i = 0; i < n; i++)
{
res += abs(p[i] - i) , merge(i , p[i]);
if(i < p[i])
cnt[i]++ , cnt[p[i]]--;
if(p[i] != i || i == s)
val.pb(i);
}
for(int i = 1; i < n; i++)
cnt[i] += cnt[i - 1];
for(int i = 0; i < n; i++)
if(cnt[i])
merge(i , i + 1);
int m = val.size();
vector<pair<int , pair<int , int> > > tmp;
for(int i = 0; i + 1 < m; i++)
tmp.pb({val[i + 1] - val[i] , {val[i] , val[i + 1]}});
sort(tmp.begin() , tmp.end());
for(auto x : tmp)
{
int a = x.second.first , b = x.second.second;
if(merge(a , b))
res += 2 * x.first;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4260 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
5 ms |
4216 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
5 ms |
4216 KB |
Output is correct |
12 |
Correct |
5 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
5 ms |
4216 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4216 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4260 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
5 ms |
4216 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
5 ms |
4216 KB |
Output is correct |
12 |
Correct |
5 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
5 ms |
4216 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4216 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
19 |
Correct |
6 ms |
4344 KB |
Output is correct |
20 |
Correct |
5 ms |
4344 KB |
Output is correct |
21 |
Correct |
6 ms |
4216 KB |
Output is correct |
22 |
Correct |
6 ms |
4316 KB |
Output is correct |
23 |
Correct |
5 ms |
4208 KB |
Output is correct |
24 |
Correct |
6 ms |
4220 KB |
Output is correct |
25 |
Correct |
7 ms |
4208 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
6 ms |
4344 KB |
Output is correct |
28 |
Correct |
5 ms |
4216 KB |
Output is correct |
29 |
Correct |
5 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4260 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
5 ms |
4216 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
5 ms |
4216 KB |
Output is correct |
12 |
Correct |
5 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
5 ms |
4216 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4216 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
19 |
Correct |
6 ms |
4344 KB |
Output is correct |
20 |
Correct |
5 ms |
4344 KB |
Output is correct |
21 |
Correct |
6 ms |
4216 KB |
Output is correct |
22 |
Correct |
6 ms |
4316 KB |
Output is correct |
23 |
Correct |
5 ms |
4208 KB |
Output is correct |
24 |
Correct |
6 ms |
4220 KB |
Output is correct |
25 |
Correct |
7 ms |
4208 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
6 ms |
4344 KB |
Output is correct |
28 |
Correct |
5 ms |
4216 KB |
Output is correct |
29 |
Correct |
5 ms |
4216 KB |
Output is correct |
30 |
Correct |
331 ms |
42040 KB |
Output is correct |
31 |
Correct |
315 ms |
41996 KB |
Output is correct |
32 |
Correct |
148 ms |
22704 KB |
Output is correct |
33 |
Correct |
244 ms |
32360 KB |
Output is correct |
34 |
Correct |
246 ms |
32416 KB |
Output is correct |
35 |
Correct |
283 ms |
40664 KB |
Output is correct |
36 |
Correct |
339 ms |
41648 KB |
Output is correct |
37 |
Correct |
394 ms |
41944 KB |
Output is correct |
38 |
Correct |
392 ms |
42020 KB |
Output is correct |
39 |
Correct |
393 ms |
42040 KB |
Output is correct |
40 |
Correct |
395 ms |
42004 KB |
Output is correct |
41 |
Correct |
320 ms |
41980 KB |
Output is correct |
42 |
Correct |
353 ms |
42072 KB |
Output is correct |
43 |
Correct |
264 ms |
32432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
4216 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4260 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
4216 KB |
Output is correct |
9 |
Correct |
5 ms |
4216 KB |
Output is correct |
10 |
Correct |
5 ms |
4216 KB |
Output is correct |
11 |
Correct |
5 ms |
4216 KB |
Output is correct |
12 |
Correct |
5 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
4216 KB |
Output is correct |
14 |
Correct |
5 ms |
4216 KB |
Output is correct |
15 |
Correct |
5 ms |
4216 KB |
Output is correct |
16 |
Correct |
5 ms |
4216 KB |
Output is correct |
17 |
Correct |
5 ms |
4216 KB |
Output is correct |
18 |
Correct |
5 ms |
4216 KB |
Output is correct |
19 |
Correct |
6 ms |
4344 KB |
Output is correct |
20 |
Correct |
5 ms |
4344 KB |
Output is correct |
21 |
Correct |
6 ms |
4216 KB |
Output is correct |
22 |
Correct |
6 ms |
4316 KB |
Output is correct |
23 |
Correct |
5 ms |
4208 KB |
Output is correct |
24 |
Correct |
6 ms |
4220 KB |
Output is correct |
25 |
Correct |
7 ms |
4208 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
6 ms |
4344 KB |
Output is correct |
28 |
Correct |
5 ms |
4216 KB |
Output is correct |
29 |
Correct |
5 ms |
4216 KB |
Output is correct |
30 |
Correct |
331 ms |
42040 KB |
Output is correct |
31 |
Correct |
315 ms |
41996 KB |
Output is correct |
32 |
Correct |
148 ms |
22704 KB |
Output is correct |
33 |
Correct |
244 ms |
32360 KB |
Output is correct |
34 |
Correct |
246 ms |
32416 KB |
Output is correct |
35 |
Correct |
283 ms |
40664 KB |
Output is correct |
36 |
Correct |
339 ms |
41648 KB |
Output is correct |
37 |
Correct |
394 ms |
41944 KB |
Output is correct |
38 |
Correct |
392 ms |
42020 KB |
Output is correct |
39 |
Correct |
393 ms |
42040 KB |
Output is correct |
40 |
Correct |
395 ms |
42004 KB |
Output is correct |
41 |
Correct |
320 ms |
41980 KB |
Output is correct |
42 |
Correct |
353 ms |
42072 KB |
Output is correct |
43 |
Correct |
264 ms |
32432 KB |
Output is correct |
44 |
Incorrect |
5 ms |
4216 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |