#include "books.h"
#include "bits/stdc++.h"
#define MAXN 1000009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
map<PII,int>dp;
int L[MAXN],R[MAXN],bel[MAXN],S;
int destL,destR;
void extend(int &l,int &r){
int lp=l,rp=r;
if(bel[l])umin(lp,L[bel[l]]),umax(rp,R[bel[l]]);
if(bel[r])umin(lp,L[bel[r]]),umax(rp,R[bel[r]]);
while(l!=lp or r!=rp){
if(l>lp and bel[--l])umin(lp,L[bel[l]]),umax(rp,R[bel[l]]);
if(r<rp and bel[++r])umin(lp,L[bel[r]]),umax(rp,R[bel[r]]);
}
}
int rec(int l,int r){
extend(l,r);
if(l==destL and r==destR)return 0;
if(dp.find(mp(l,r))!=dp.end())return dp[mp(l,r)];
if(l==destL)return dp[mp(l,r)]=rec(l,r+1)+2;
if(r==destR)return dp[mp(l,r)]=rec(l-1,r)+2;
int fl=l-1,fr=r;extend(fl,fr);
int el=l,er=r+1;extend(el,er);
int ans_l=1,ans_r=1;
while(fl!=destL and fr<er){
ans_l++;fl--;
extend(fl,fr);
}
if(fr<er)return dp[mp(l,r)]=ans_l*2+rec(fl,fr);
fl=l,fr=r+1;extend(fl,fr);
el=l-1,er=r;extend(el,er);
while(fr!=destR and fl>el){
ans_r++;fr++;
extend(fl,fr);
}
return dp[mp(l,r)]=min(ans_l,ans_r)*2+rec(fl,fr);
}
long long minimum_walk(vector<int> p, int s) {
int n=int(p.size());
vector<int>vis(n);
destL=destR=s;
ll res=0;
for(int i=0;i<n;i++){
res+=abs(p[i]-i);
if(p[i]!=i and !vis[i]){
int to=i;L[++S]=i;
do{umax(R[S],to);bel[to]=S;vis[to]=1;to=p[to];}while(to!=i);
umin(destL,L[S]);umax(destR,R[S]);
}
}
return rec(s,s)+res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
263 ms |
15980 KB |
Output is correct |
31 |
Correct |
316 ms |
15980 KB |
Output is correct |
32 |
Correct |
132 ms |
12140 KB |
Output is correct |
33 |
Correct |
418 ms |
127596 KB |
Output is correct |
34 |
Correct |
372 ms |
127684 KB |
Output is correct |
35 |
Correct |
341 ms |
96100 KB |
Output is correct |
36 |
Correct |
223 ms |
40300 KB |
Output is correct |
37 |
Correct |
149 ms |
17348 KB |
Output is correct |
38 |
Correct |
153 ms |
16196 KB |
Output is correct |
39 |
Correct |
170 ms |
16108 KB |
Output is correct |
40 |
Correct |
169 ms |
15980 KB |
Output is correct |
41 |
Correct |
203 ms |
15980 KB |
Output is correct |
42 |
Correct |
207 ms |
15940 KB |
Output is correct |
43 |
Correct |
192 ms |
17900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
263 ms |
15980 KB |
Output is correct |
31 |
Correct |
316 ms |
15980 KB |
Output is correct |
32 |
Correct |
132 ms |
12140 KB |
Output is correct |
33 |
Correct |
418 ms |
127596 KB |
Output is correct |
34 |
Correct |
372 ms |
127684 KB |
Output is correct |
35 |
Correct |
341 ms |
96100 KB |
Output is correct |
36 |
Correct |
223 ms |
40300 KB |
Output is correct |
37 |
Correct |
149 ms |
17348 KB |
Output is correct |
38 |
Correct |
153 ms |
16196 KB |
Output is correct |
39 |
Correct |
170 ms |
16108 KB |
Output is correct |
40 |
Correct |
169 ms |
15980 KB |
Output is correct |
41 |
Correct |
203 ms |
15980 KB |
Output is correct |
42 |
Correct |
207 ms |
15940 KB |
Output is correct |
43 |
Correct |
192 ms |
17900 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
364 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
178 ms |
16876 KB |
Output is correct |
65 |
Correct |
160 ms |
23532 KB |
Output is correct |
66 |
Correct |
158 ms |
22908 KB |
Output is correct |
67 |
Correct |
165 ms |
22764 KB |
Output is correct |
68 |
Correct |
16 ms |
2540 KB |
Output is correct |
69 |
Correct |
20 ms |
2540 KB |
Output is correct |
70 |
Correct |
19 ms |
2540 KB |
Output is correct |
71 |
Correct |
32 ms |
2668 KB |
Output is correct |
72 |
Correct |
15 ms |
2540 KB |
Output is correct |
73 |
Correct |
17 ms |
2668 KB |
Output is correct |
74 |
Correct |
248 ms |
68436 KB |
Output is correct |
75 |
Correct |
273 ms |
79240 KB |
Output is correct |
76 |
Correct |
173 ms |
24644 KB |
Output is correct |
77 |
Correct |
180 ms |
24684 KB |
Output is correct |
78 |
Correct |
176 ms |
25668 KB |
Output is correct |
79 |
Correct |
165 ms |
25708 KB |
Output is correct |
80 |
Correct |
143 ms |
18756 KB |
Output is correct |
81 |
Correct |
269 ms |
84052 KB |
Output is correct |
82 |
Correct |
273 ms |
84076 KB |
Output is correct |
83 |
Correct |
175 ms |
29764 KB |
Output is correct |
84 |
Correct |
212 ms |
27972 KB |
Output is correct |
85 |
Correct |
177 ms |
24768 KB |
Output is correct |
86 |
Correct |
182 ms |
23492 KB |
Output is correct |
87 |
Correct |
222 ms |
50360 KB |
Output is correct |
88 |
Correct |
193 ms |
40644 KB |
Output is correct |
89 |
Correct |
204 ms |
31956 KB |
Output is correct |
90 |
Correct |
156 ms |
23020 KB |
Output is correct |
91 |
Correct |
157 ms |
22852 KB |
Output is correct |
92 |
Correct |
168 ms |
22764 KB |
Output is correct |