#include "books.h"
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define ii pair<int, int>
#define x first
#define y second
#define vii vector<ii>
#define pb push_back
#define all(x) x.begin(), x.end()
#define loop(i,s,e) for(int i=s;i<e;++i)
#define chkmax(a,b) a = max(a,b)
#define chkmin(a,b) a = min(a,b)
using namespace std;
const int INF = 1e9;
typedef long long ll;
struct DSU{
int n;
vi p, sz;
DSU(int n):n(n), p(n,-1), sz(n,1){}
int find(int c){
if (p[c]==-1) return c;
return p[c] = find(p[c]);
}
void uni(int a, int b){
a = find(a), b = find(b);
if (a==b) return ;
if (sz[a]>sz[b]) swap(a,b);
p[a] = b;
sz[b] += sz[a];
}
};
ll minimum_walk(vi p, int s) {
ll ans = 0;
int n = p.size();
vb check(n,0);
int needl = s, needr = s;
vi v1(n), v2(n);
loop(i,0,n) if(!check[i]){
vi vec;
int mn = i, mx = i;
for(int cur = i;!check[cur]; cur = p[cur]){
check[cur] = 1;
ans+=abs(cur-p[cur]);
vec.pb(cur);
chkmin(mn, cur); chkmax(mx, cur);
}
if (vec.size()>1) chkmax(needr, mx), chkmin(needl, mn);
for(auto x:vec) v2[x] = mx, v1[x] = mn;
}
int l = s, r = s, ml=s, mr=s;
while(l>=ml || r<=mr){
if (l>=ml){
chkmax(mr, v2[l]);
chkmin(ml, v1[l]);
l--;
}
else{
chkmax(mr, v2[r]);
chkmin(ml, v1[r]);
r++;
}
}
vi right(1,l+1), left(1,r-1);
vi comp(n,0); int cnt = 0;
while(r<n){
if (r>mr) right.pb(n), mr++, cnt++;
comp[r] = cnt;
chkmax(mr, v2[r]);
chkmin(right.back(), v1[r]);
r++;
}
cnt = 0;
while(l>=0){
if (l<ml) left.pb(-1), ml--, cnt++;
comp[l] = cnt;
chkmin(ml, v1[l]);
chkmax(left.back(), v2[l]);
l--;
}
int a = left.size(), b = right.size();
for(int &x:left) x = comp[x] + (x>s?a:0);
for(int &x:right) x = comp[x] + (x>s?a:0);
needl = comp[needl], needr = comp[needr];
DSU dsu(a+b);
dsu.uni(0,a); //same one
loop(i,0,a) dsu.uni(i, left[i]);
loop(i,0,b) dsu.uni(i+a, right[i]);
map<int, int> conv; int tmp=0;
loop(i,0,a+b) if(dsu.p[i]==-1) conv[i]=tmp++;
vvi g(tmp);
auto get = [&](int i){
return conv[dsu.find(i)];
};
loop(i,0,a-1){
int aa = get(i), bb = get(i+1);
if (aa!=bb) g[aa].pb(bb);
}
loop(i,0,b-1){
int aa = get(i+a), bb = get(i+a+1);
if (aa!=bb) g[aa].pb(bb);
}
vi vals(tmp);
loop(i,0,a) vals[get(i)]|=1;
loop(i,0,b) vals[get(i+a)]|=2;
//one that has 3 is both
needl = get(needl), needr = get(needr+a); //updating targets
queue<int> q; q.push(get(0));
vi dist(tmp,-1); dist[get(0)] = 0;
int has = 0, lasty = 0, lasth=0;
//cout<<"AB: "<<a<<" "<<b<<" "<<ans<<endl;
while(q.size()){
int cur = q.front(); q.pop();
if (vals[cur]==3) lasty = dist[cur];
if (cur==needl) ans+=2*dist[cur], has|=1;
if (cur==needr) ans+=2*dist[cur], has|=2;
//cout<<"CUR: "<<cur<<" "<<dist[cur]<<endl;
if (has!=0 && lasth==0){
ans-=2*lasty;
}
if (has==3) break;
lasth = has;
for(auto nei:g[cur]) if(dist[nei]==-1){
dist[nei]=dist[cur]+1;
q.push(nei);
}
}
return ans;
}
/*
clear
g++ books.cpp grader.cpp -o a ; ./a
4 0
0 2 3 1
11 2
10 5 2 4 3 8 7 6 1 9 0
4 1 3 2 7 6 5 0
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
432 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
432 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
250 ms |
26400 KB |
Output is correct |
31 |
Correct |
253 ms |
28156 KB |
Output is correct |
32 |
Correct |
915 ms |
147424 KB |
Output is correct |
33 |
Correct |
665 ms |
103656 KB |
Output is correct |
34 |
Correct |
676 ms |
103656 KB |
Output is correct |
35 |
Correct |
529 ms |
81256 KB |
Output is correct |
36 |
Correct |
319 ms |
42348 KB |
Output is correct |
37 |
Correct |
212 ms |
26872 KB |
Output is correct |
38 |
Correct |
190 ms |
26104 KB |
Output is correct |
39 |
Correct |
207 ms |
26104 KB |
Output is correct |
40 |
Correct |
233 ms |
26264 KB |
Output is correct |
41 |
Correct |
243 ms |
27024 KB |
Output is correct |
42 |
Correct |
235 ms |
26940 KB |
Output is correct |
43 |
Correct |
216 ms |
26032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
432 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
250 ms |
26400 KB |
Output is correct |
31 |
Correct |
253 ms |
28156 KB |
Output is correct |
32 |
Correct |
915 ms |
147424 KB |
Output is correct |
33 |
Correct |
665 ms |
103656 KB |
Output is correct |
34 |
Correct |
676 ms |
103656 KB |
Output is correct |
35 |
Correct |
529 ms |
81256 KB |
Output is correct |
36 |
Correct |
319 ms |
42348 KB |
Output is correct |
37 |
Correct |
212 ms |
26872 KB |
Output is correct |
38 |
Correct |
190 ms |
26104 KB |
Output is correct |
39 |
Correct |
207 ms |
26104 KB |
Output is correct |
40 |
Correct |
233 ms |
26264 KB |
Output is correct |
41 |
Correct |
243 ms |
27024 KB |
Output is correct |
42 |
Correct |
235 ms |
26940 KB |
Output is correct |
43 |
Correct |
216 ms |
26032 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
1 ms |
384 KB |
Output is correct |
54 |
Correct |
1 ms |
384 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
225 ms |
27896 KB |
Output is correct |
65 |
Correct |
228 ms |
29192 KB |
Output is correct |
66 |
Correct |
200 ms |
26788 KB |
Output is correct |
67 |
Correct |
195 ms |
26744 KB |
Output is correct |
68 |
Correct |
22 ms |
2936 KB |
Output is correct |
69 |
Correct |
20 ms |
2944 KB |
Output is correct |
70 |
Correct |
20 ms |
2944 KB |
Output is correct |
71 |
Correct |
21 ms |
2944 KB |
Output is correct |
72 |
Correct |
23 ms |
3320 KB |
Output is correct |
73 |
Correct |
19 ms |
2976 KB |
Output is correct |
74 |
Correct |
655 ms |
104564 KB |
Output is correct |
75 |
Correct |
657 ms |
104568 KB |
Output is correct |
76 |
Correct |
218 ms |
26744 KB |
Output is correct |
77 |
Correct |
222 ms |
26744 KB |
Output is correct |
78 |
Correct |
219 ms |
26744 KB |
Output is correct |
79 |
Correct |
222 ms |
26744 KB |
Output is correct |
80 |
Correct |
914 ms |
148392 KB |
Output is correct |
81 |
Correct |
679 ms |
91492 KB |
Output is correct |
82 |
Correct |
689 ms |
91368 KB |
Output is correct |
83 |
Correct |
388 ms |
54328 KB |
Output is correct |
84 |
Correct |
315 ms |
44468 KB |
Output is correct |
85 |
Correct |
233 ms |
31992 KB |
Output is correct |
86 |
Correct |
201 ms |
28280 KB |
Output is correct |
87 |
Correct |
943 ms |
131220 KB |
Output is correct |
88 |
Correct |
802 ms |
113588 KB |
Output is correct |
89 |
Correct |
485 ms |
67824 KB |
Output is correct |
90 |
Correct |
196 ms |
27212 KB |
Output is correct |
91 |
Correct |
203 ms |
26872 KB |
Output is correct |
92 |
Correct |
194 ms |
26876 KB |
Output is correct |