#include "books.h"
#ifndef EVAL
#include "grader.cpp"
#endif // EVAL
#include <bits/stdc++.h>
using namespace std;
#define lol long long
#define pii pair<int,int>
#define pll pair<lol,lol>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%d",&a);
#define scan2(a,b) scanf("%d %d",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-12
int vis[1000001],n,tl,tr,t[1000001];
lol dp[2002][2002];
map<vector<int>,int> mp;
pii q[1000001];
void extend(int &l,int &r){
int ll = min(q[t[l]].fr,q[t[r]].fr),rr = max(q[t[l]].sc,q[t[r]].sc);
while (1){
if (ll < l){
ll = min(ll,q[t[--l]].fr);
rr = max(rr,q[t[l]].sc);
}
else if (r < rr){
ll = min(ll,q[t[++r]].fr);
rr = max(rr,q[t[r]].sc);
}
else break;
}
}
lol rec(int l,int r){
if (l < tl || tr < r)ret 1e9+7;
extend(l,r);
if (l == tl && r == tr)ret 0;
lol &res = dp[l][r];
if (res != -1)ret dp[l][r];
res = min(rec(l-1,r),rec(l,r+1))+2;
ret res;
}
lol minimum_walk(vector<int> p, int s) {
memset(dp,-1,sizeof(dp));
n = p.size();
int i,col=0;
tl = tr = s;
lol ans=0;
for (i=0;i<n;++i){
if (vis[i] == 1)continue;
int x = i,l=i,r=i;
while (vis[x] == 0){
vis[x] = 1;
l = min(l,x);
r = max(x,r);
t[x] = col;
ans += abs(p[x]-x);
x = p[x];
}
q[col++] = {l,r};
if (l != r){
tl = min(tl,l);
tr = max(tr,r);
}
}
ret ans+rec(s,s);
}
/*
4 0
0 2 3 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31744 KB |
Output is correct |
2 |
Correct |
15 ms |
31724 KB |
Output is correct |
3 |
Correct |
16 ms |
31724 KB |
Output is correct |
4 |
Correct |
15 ms |
31744 KB |
Output is correct |
5 |
Correct |
16 ms |
31852 KB |
Output is correct |
6 |
Correct |
15 ms |
31724 KB |
Output is correct |
7 |
Correct |
16 ms |
31852 KB |
Output is correct |
8 |
Correct |
16 ms |
31724 KB |
Output is correct |
9 |
Correct |
16 ms |
31744 KB |
Output is correct |
10 |
Correct |
16 ms |
31724 KB |
Output is correct |
11 |
Correct |
16 ms |
31724 KB |
Output is correct |
12 |
Correct |
16 ms |
31724 KB |
Output is correct |
13 |
Correct |
15 ms |
31724 KB |
Output is correct |
14 |
Correct |
16 ms |
31724 KB |
Output is correct |
15 |
Correct |
17 ms |
31724 KB |
Output is correct |
16 |
Correct |
15 ms |
31724 KB |
Output is correct |
17 |
Correct |
16 ms |
31724 KB |
Output is correct |
18 |
Correct |
16 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31744 KB |
Output is correct |
2 |
Correct |
15 ms |
31724 KB |
Output is correct |
3 |
Correct |
16 ms |
31724 KB |
Output is correct |
4 |
Correct |
15 ms |
31744 KB |
Output is correct |
5 |
Correct |
16 ms |
31852 KB |
Output is correct |
6 |
Correct |
15 ms |
31724 KB |
Output is correct |
7 |
Correct |
16 ms |
31852 KB |
Output is correct |
8 |
Correct |
16 ms |
31724 KB |
Output is correct |
9 |
Correct |
16 ms |
31744 KB |
Output is correct |
10 |
Correct |
16 ms |
31724 KB |
Output is correct |
11 |
Correct |
16 ms |
31724 KB |
Output is correct |
12 |
Correct |
16 ms |
31724 KB |
Output is correct |
13 |
Correct |
15 ms |
31724 KB |
Output is correct |
14 |
Correct |
16 ms |
31724 KB |
Output is correct |
15 |
Correct |
17 ms |
31724 KB |
Output is correct |
16 |
Correct |
15 ms |
31724 KB |
Output is correct |
17 |
Correct |
16 ms |
31724 KB |
Output is correct |
18 |
Correct |
16 ms |
31724 KB |
Output is correct |
19 |
Correct |
16 ms |
31724 KB |
Output is correct |
20 |
Correct |
15 ms |
31724 KB |
Output is correct |
21 |
Correct |
15 ms |
31724 KB |
Output is correct |
22 |
Correct |
16 ms |
31744 KB |
Output is correct |
23 |
Correct |
16 ms |
31744 KB |
Output is correct |
24 |
Correct |
16 ms |
31724 KB |
Output is correct |
25 |
Correct |
16 ms |
31724 KB |
Output is correct |
26 |
Correct |
16 ms |
31724 KB |
Output is correct |
27 |
Correct |
16 ms |
31724 KB |
Output is correct |
28 |
Correct |
16 ms |
31724 KB |
Output is correct |
29 |
Correct |
16 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31744 KB |
Output is correct |
2 |
Correct |
15 ms |
31724 KB |
Output is correct |
3 |
Correct |
16 ms |
31724 KB |
Output is correct |
4 |
Correct |
15 ms |
31744 KB |
Output is correct |
5 |
Correct |
16 ms |
31852 KB |
Output is correct |
6 |
Correct |
15 ms |
31724 KB |
Output is correct |
7 |
Correct |
16 ms |
31852 KB |
Output is correct |
8 |
Correct |
16 ms |
31724 KB |
Output is correct |
9 |
Correct |
16 ms |
31744 KB |
Output is correct |
10 |
Correct |
16 ms |
31724 KB |
Output is correct |
11 |
Correct |
16 ms |
31724 KB |
Output is correct |
12 |
Correct |
16 ms |
31724 KB |
Output is correct |
13 |
Correct |
15 ms |
31724 KB |
Output is correct |
14 |
Correct |
16 ms |
31724 KB |
Output is correct |
15 |
Correct |
17 ms |
31724 KB |
Output is correct |
16 |
Correct |
15 ms |
31724 KB |
Output is correct |
17 |
Correct |
16 ms |
31724 KB |
Output is correct |
18 |
Correct |
16 ms |
31724 KB |
Output is correct |
19 |
Correct |
16 ms |
31724 KB |
Output is correct |
20 |
Correct |
15 ms |
31724 KB |
Output is correct |
21 |
Correct |
15 ms |
31724 KB |
Output is correct |
22 |
Correct |
16 ms |
31744 KB |
Output is correct |
23 |
Correct |
16 ms |
31744 KB |
Output is correct |
24 |
Correct |
16 ms |
31724 KB |
Output is correct |
25 |
Correct |
16 ms |
31724 KB |
Output is correct |
26 |
Correct |
16 ms |
31724 KB |
Output is correct |
27 |
Correct |
16 ms |
31724 KB |
Output is correct |
28 |
Correct |
16 ms |
31724 KB |
Output is correct |
29 |
Correct |
16 ms |
31724 KB |
Output is correct |
30 |
Correct |
219 ms |
54052 KB |
Output is correct |
31 |
Correct |
216 ms |
54124 KB |
Output is correct |
32 |
Correct |
145 ms |
62060 KB |
Output is correct |
33 |
Correct |
182 ms |
89580 KB |
Output is correct |
34 |
Correct |
183 ms |
89580 KB |
Output is correct |
35 |
Correct |
174 ms |
79724 KB |
Output is correct |
36 |
Correct |
158 ms |
62316 KB |
Output is correct |
37 |
Correct |
170 ms |
54720 KB |
Output is correct |
38 |
Correct |
149 ms |
54124 KB |
Output is correct |
39 |
Correct |
148 ms |
54144 KB |
Output is correct |
40 |
Correct |
152 ms |
54124 KB |
Output is correct |
41 |
Correct |
168 ms |
54124 KB |
Output is correct |
42 |
Correct |
166 ms |
54124 KB |
Output is correct |
43 |
Correct |
158 ms |
60028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31724 KB |
Output is correct |
2 |
Correct |
16 ms |
31724 KB |
Output is correct |
3 |
Correct |
18 ms |
31724 KB |
Output is correct |
4 |
Correct |
19 ms |
31724 KB |
Output is correct |
5 |
Correct |
18 ms |
31724 KB |
Output is correct |
6 |
Correct |
17 ms |
31724 KB |
Output is correct |
7 |
Correct |
16 ms |
31724 KB |
Output is correct |
8 |
Correct |
15 ms |
31724 KB |
Output is correct |
9 |
Correct |
15 ms |
31724 KB |
Output is correct |
10 |
Correct |
17 ms |
31732 KB |
Output is correct |
11 |
Correct |
18 ms |
31724 KB |
Output is correct |
12 |
Correct |
16 ms |
31724 KB |
Output is correct |
13 |
Correct |
19 ms |
31724 KB |
Output is correct |
14 |
Correct |
15 ms |
31724 KB |
Output is correct |
15 |
Correct |
15 ms |
31724 KB |
Output is correct |
16 |
Correct |
17 ms |
31724 KB |
Output is correct |
17 |
Correct |
18 ms |
31724 KB |
Output is correct |
18 |
Correct |
16 ms |
31724 KB |
Output is correct |
19 |
Correct |
16 ms |
31724 KB |
Output is correct |
20 |
Correct |
15 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31744 KB |
Output is correct |
2 |
Correct |
15 ms |
31724 KB |
Output is correct |
3 |
Correct |
16 ms |
31724 KB |
Output is correct |
4 |
Correct |
15 ms |
31744 KB |
Output is correct |
5 |
Correct |
16 ms |
31852 KB |
Output is correct |
6 |
Correct |
15 ms |
31724 KB |
Output is correct |
7 |
Correct |
16 ms |
31852 KB |
Output is correct |
8 |
Correct |
16 ms |
31724 KB |
Output is correct |
9 |
Correct |
16 ms |
31744 KB |
Output is correct |
10 |
Correct |
16 ms |
31724 KB |
Output is correct |
11 |
Correct |
16 ms |
31724 KB |
Output is correct |
12 |
Correct |
16 ms |
31724 KB |
Output is correct |
13 |
Correct |
15 ms |
31724 KB |
Output is correct |
14 |
Correct |
16 ms |
31724 KB |
Output is correct |
15 |
Correct |
17 ms |
31724 KB |
Output is correct |
16 |
Correct |
15 ms |
31724 KB |
Output is correct |
17 |
Correct |
16 ms |
31724 KB |
Output is correct |
18 |
Correct |
16 ms |
31724 KB |
Output is correct |
19 |
Correct |
16 ms |
31724 KB |
Output is correct |
20 |
Correct |
15 ms |
31724 KB |
Output is correct |
21 |
Correct |
15 ms |
31724 KB |
Output is correct |
22 |
Correct |
16 ms |
31744 KB |
Output is correct |
23 |
Correct |
16 ms |
31744 KB |
Output is correct |
24 |
Correct |
16 ms |
31724 KB |
Output is correct |
25 |
Correct |
16 ms |
31724 KB |
Output is correct |
26 |
Correct |
16 ms |
31724 KB |
Output is correct |
27 |
Correct |
16 ms |
31724 KB |
Output is correct |
28 |
Correct |
16 ms |
31724 KB |
Output is correct |
29 |
Correct |
16 ms |
31724 KB |
Output is correct |
30 |
Correct |
219 ms |
54052 KB |
Output is correct |
31 |
Correct |
216 ms |
54124 KB |
Output is correct |
32 |
Correct |
145 ms |
62060 KB |
Output is correct |
33 |
Correct |
182 ms |
89580 KB |
Output is correct |
34 |
Correct |
183 ms |
89580 KB |
Output is correct |
35 |
Correct |
174 ms |
79724 KB |
Output is correct |
36 |
Correct |
158 ms |
62316 KB |
Output is correct |
37 |
Correct |
170 ms |
54720 KB |
Output is correct |
38 |
Correct |
149 ms |
54124 KB |
Output is correct |
39 |
Correct |
148 ms |
54144 KB |
Output is correct |
40 |
Correct |
152 ms |
54124 KB |
Output is correct |
41 |
Correct |
168 ms |
54124 KB |
Output is correct |
42 |
Correct |
166 ms |
54124 KB |
Output is correct |
43 |
Correct |
158 ms |
60028 KB |
Output is correct |
44 |
Correct |
20 ms |
31724 KB |
Output is correct |
45 |
Correct |
16 ms |
31724 KB |
Output is correct |
46 |
Correct |
18 ms |
31724 KB |
Output is correct |
47 |
Correct |
19 ms |
31724 KB |
Output is correct |
48 |
Correct |
18 ms |
31724 KB |
Output is correct |
49 |
Correct |
17 ms |
31724 KB |
Output is correct |
50 |
Correct |
16 ms |
31724 KB |
Output is correct |
51 |
Correct |
15 ms |
31724 KB |
Output is correct |
52 |
Correct |
15 ms |
31724 KB |
Output is correct |
53 |
Correct |
17 ms |
31732 KB |
Output is correct |
54 |
Correct |
18 ms |
31724 KB |
Output is correct |
55 |
Correct |
16 ms |
31724 KB |
Output is correct |
56 |
Correct |
19 ms |
31724 KB |
Output is correct |
57 |
Correct |
15 ms |
31724 KB |
Output is correct |
58 |
Correct |
15 ms |
31724 KB |
Output is correct |
59 |
Correct |
17 ms |
31724 KB |
Output is correct |
60 |
Correct |
18 ms |
31724 KB |
Output is correct |
61 |
Correct |
16 ms |
31724 KB |
Output is correct |
62 |
Correct |
16 ms |
31724 KB |
Output is correct |
63 |
Correct |
15 ms |
31724 KB |
Output is correct |
64 |
Runtime error |
216 ms |
114028 KB |
Execution killed with signal 11 |
65 |
Halted |
0 ms |
0 KB |
- |