// ~ Be Name Khoda ~ //
#include "books.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 1e6 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int cmp[maxn5], lef[maxn5], rig[maxn5], num = 0;
bool mark[maxn5];
vector <int> per;
void expa(int &l, int &r){
int lq = l, rq = r;
while(true){
lq = min({lq, lef[cmp[l]], lef[cmp[r]]});
rq = max({rq, rig[cmp[l]], rig[cmp[r]]});
//cout << "haaaa " << l << ' ' << r << ' ' << lq << ' ' << rq << endl;
if(l > lq){
l--;
continue;
}
if(r < rq){
r++;
continue;
}
break;
}
}
int solve(int lq, int rq, int s){
int l = s, r = s;
expa(l, r);
int ans = 0;
//cout << l << ' ' << r << ' ' << lq << ' ' << rq << endl;
while(l > lq || r < rq){
int c1 = 0, c2 = 0;
int l1 = l, r1 = r, l2 = l, r2 = r;
while(r1 <= r && l1 > lq){
l1--;
c1++;
expa(l1, r1);
}
while(l2 >= l && r2 < rq){
r2++;
//cout << r2 << ' ' << cmp[r2] << ' ' << rig[cmp[r2]] << endl;
c2++;
expa(l2, r2);
//cout << "hoy " << l2 << ' ' << r2 << ' ' << rig[cmp[r2]] << ' ' << cmp[r2] << endl;
}
ans += min(c1, c2) * 2;
l = min(l1, l2);
r = max(r1, r2);
if((l2 != lq || r1 != rq) && (l == lq && r == rq))
ans += max(c1, c2) * 2;
//cout << l << ' ' << r << ' ' << ans << ' ' << c1 << ' ' << c2 << endl;
//cout << l2 << ' ' << r2 << endl;
}
return ans;
}
void dfs(int v){
mark[v] = true;
cmp[v] = num;
lef[num] = min(lef[num], v);
rig[num] = max(rig[num], v);
if(!mark[per[v]])
dfs(per[v]);
}
long long minimum_walk(std::vector<int> PER, int s) {
per = PER;
int n = per.size();
int r = s, l = s;
ll ans = 0;
for(int i = 0; i < n; i++){
if(!mark[i]){
lef[num] = rig[num] = i;
dfs(i);
num++;
}
//cout << i << ' ' << per[i] << ' ' << cmp[i] << endl;
ans += abs(per[i] - i);
if(i != per[i]){
l = min(l, i);
r = max(r, i);
}
}
if(l == r)
return 0;
//cout << "ha? " << ans << endl;
//cout << l << ' ' << r << ' ' << s << ' ' << cmp[l] << ' ' << cmp[r] << ' ' << num << ' ' << ans << endl;
return solve(l, r, s) + ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 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 |
100 ms |
16884 KB |
Output is correct |
31 |
Correct |
106 ms |
16952 KB |
Output is correct |
32 |
Correct |
83 ms |
24688 KB |
Output is correct |
33 |
Correct |
100 ms |
22260 KB |
Output is correct |
34 |
Correct |
93 ms |
22240 KB |
Output is correct |
35 |
Correct |
101 ms |
21240 KB |
Output is correct |
36 |
Correct |
95 ms |
18932 KB |
Output is correct |
37 |
Correct |
82 ms |
17140 KB |
Output is correct |
38 |
Correct |
82 ms |
16972 KB |
Output is correct |
39 |
Correct |
82 ms |
16880 KB |
Output is correct |
40 |
Correct |
83 ms |
16884 KB |
Output is correct |
41 |
Correct |
102 ms |
16896 KB |
Output is correct |
42 |
Correct |
91 ms |
16880 KB |
Output is correct |
43 |
Correct |
88 ms |
22708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 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 |
100 ms |
16884 KB |
Output is correct |
31 |
Correct |
106 ms |
16952 KB |
Output is correct |
32 |
Correct |
83 ms |
24688 KB |
Output is correct |
33 |
Correct |
100 ms |
22260 KB |
Output is correct |
34 |
Correct |
93 ms |
22240 KB |
Output is correct |
35 |
Correct |
101 ms |
21240 KB |
Output is correct |
36 |
Correct |
95 ms |
18932 KB |
Output is correct |
37 |
Correct |
82 ms |
17140 KB |
Output is correct |
38 |
Correct |
82 ms |
16972 KB |
Output is correct |
39 |
Correct |
82 ms |
16880 KB |
Output is correct |
40 |
Correct |
83 ms |
16884 KB |
Output is correct |
41 |
Correct |
102 ms |
16896 KB |
Output is correct |
42 |
Correct |
91 ms |
16880 KB |
Output is correct |
43 |
Correct |
88 ms |
22708 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
312 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
312 KB |
Output is correct |
64 |
Correct |
91 ms |
29288 KB |
Output is correct |
65 |
Correct |
91 ms |
29936 KB |
Output is correct |
66 |
Correct |
86 ms |
23920 KB |
Output is correct |
67 |
Correct |
89 ms |
23924 KB |
Output is correct |
68 |
Correct |
9 ms |
2772 KB |
Output is correct |
69 |
Correct |
9 ms |
2616 KB |
Output is correct |
70 |
Correct |
10 ms |
2772 KB |
Output is correct |
71 |
Correct |
10 ms |
3080 KB |
Output is correct |
72 |
Correct |
9 ms |
3148 KB |
Output is correct |
73 |
Correct |
9 ms |
2568 KB |
Output is correct |
74 |
Correct |
93 ms |
29032 KB |
Output is correct |
75 |
Correct |
94 ms |
29004 KB |
Output is correct |
76 |
Correct |
94 ms |
29368 KB |
Output is correct |
77 |
Correct |
106 ms |
29364 KB |
Output is correct |
78 |
Correct |
92 ms |
28620 KB |
Output is correct |
79 |
Correct |
92 ms |
28620 KB |
Output is correct |
80 |
Correct |
85 ms |
31464 KB |
Output is correct |
81 |
Correct |
91 ms |
27496 KB |
Output is correct |
82 |
Correct |
89 ms |
27604 KB |
Output is correct |
83 |
Correct |
95 ms |
26608 KB |
Output is correct |
84 |
Correct |
92 ms |
25932 KB |
Output is correct |
85 |
Correct |
88 ms |
24652 KB |
Output is correct |
86 |
Correct |
87 ms |
24056 KB |
Output is correct |
87 |
Correct |
94 ms |
30288 KB |
Output is correct |
88 |
Correct |
98 ms |
29164 KB |
Output is correct |
89 |
Correct |
97 ms |
27236 KB |
Output is correct |
90 |
Correct |
97 ms |
23864 KB |
Output is correct |
91 |
Correct |
91 ms |
23664 KB |
Output is correct |
92 |
Correct |
88 ms |
23668 KB |
Output is correct |