#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define rep(i , j , k) for(int i = j; i < (int)k; i++)
typedef long long ll;
const int N = 1e6 + 10;
int par[N];
ll le[N], ri[N], arr[N];
int getPar(int v) {
return v == par[v] ? v : par[v] = getPar(par[v]);
}
inline bool merge(int u , int v) {
if ((u = getPar(u)) == (v = getPar(v)))
return false;
par[v] = u;
return true;
}
inline void smax(ll &a, ll b) {
if (a < b) a = b;
}
inline void smin(ll &a, ll b) {
if (b < a) a = b;
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
iota(par , par + n , 0);
vector<pair<int , int>> vp;
ll res = 0;
rep(i , 0 , n) {
res += abs(p[i] - i);
vp.pb({min(i , p[i]) , max(i , p[i])});
}
sort(vp.begin(), vp.end());
int G = -1;
rep(i , 0 , n) {
if (G >=i)
merge(i , i - 1);
G = max(G , vp[i].second);
}
int last = -1;
vector<pair<ll , pair<int , int>>> vec;
rep(i , 0 , n) {
if (i == p[i] && i != s) continue;
if (last == -1 || getPar(last) == getPar(i))
last = i;
else {
vec.pb({2 * (i - last) , {last , i}});
last = i;
}
}
sort(vec.begin(), vec.end());
for (auto e : vec)
if (merge(e.second.first , e.second.second))
res += e.first;
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
319 ms |
36188 KB |
Output is correct |
31 |
Correct |
317 ms |
36052 KB |
Output is correct |
32 |
Correct |
181 ms |
20968 KB |
Output is correct |
33 |
Correct |
231 ms |
27276 KB |
Output is correct |
34 |
Correct |
229 ms |
27248 KB |
Output is correct |
35 |
Correct |
242 ms |
27500 KB |
Output is correct |
36 |
Correct |
225 ms |
23764 KB |
Output is correct |
37 |
Correct |
233 ms |
20896 KB |
Output is correct |
38 |
Correct |
249 ms |
20976 KB |
Output is correct |
39 |
Correct |
246 ms |
20832 KB |
Output is correct |
40 |
Correct |
266 ms |
21332 KB |
Output is correct |
41 |
Correct |
293 ms |
28340 KB |
Output is correct |
42 |
Correct |
285 ms |
26624 KB |
Output is correct |
43 |
Correct |
324 ms |
36264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 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 |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
319 ms |
36188 KB |
Output is correct |
31 |
Correct |
317 ms |
36052 KB |
Output is correct |
32 |
Correct |
181 ms |
20968 KB |
Output is correct |
33 |
Correct |
231 ms |
27276 KB |
Output is correct |
34 |
Correct |
229 ms |
27248 KB |
Output is correct |
35 |
Correct |
242 ms |
27500 KB |
Output is correct |
36 |
Correct |
225 ms |
23764 KB |
Output is correct |
37 |
Correct |
233 ms |
20896 KB |
Output is correct |
38 |
Correct |
249 ms |
20976 KB |
Output is correct |
39 |
Correct |
246 ms |
20832 KB |
Output is correct |
40 |
Correct |
266 ms |
21332 KB |
Output is correct |
41 |
Correct |
293 ms |
28340 KB |
Output is correct |
42 |
Correct |
285 ms |
26624 KB |
Output is correct |
43 |
Correct |
324 ms |
36264 KB |
Output is correct |
44 |
Incorrect |
2 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |