#include "books.h"
#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long lld;
typedef pair<int,int> state;
typedef std::map<state,lld>::iterator mi;
int arr[1000000];
int n;
int maxa,maxb;
map<state,lld>DP;
lld computevalue(int i, int j){
//cout<<i<<" "<<j<<endl;
if(i<=maxa && maxb<=j)return 0;
mi it=DP.find(state(i,j));
if(it!=DP.end())return it->second;
int start=i;
int end=j;
int ps=start;
int pe=end;
while(pe<=end){
while(ps>=start){
start=min(start,arr[ps]);
end=max(end,arr[ps]);
ps--;
}
start=min(start,arr[pe]);
end=max(end,arr[pe]);
pe++;
}
lld ans=1000000000000000000;
if(start<=maxa && maxb<=end){
DP[state(i,j)]=0;
return 0;
}
if(start>maxa){
ans=min(ans,computevalue(start-1,end)+2);
}
if(end<maxb){
ans=min(ans,computevalue(start,end+1)+2);
}
DP[state(i,j)]=ans;
return ans;
}
lld abs(lld x){
if(x>0)return x;
return -x;
}
long long minimum_walk(std::vector<int> p, int s) {
n=p.size();
for(int i=0;i<n;i++){
arr[i]=p[i];
}
maxa=n;
for(int i=0;i<n;i++){
if(p[i]!=i){
maxa=i;
i=n;
}
}
maxb=-1;
for(int i=n-1;i>-1;i--){
if(p[i]!=i){
maxb=i;
i=-1;
}
}
//cout<<maxa<<" "<<maxb<<endl;
lld ans=0;
for(int i=0;i<n;i++)ans+=abs(p[i]-i);
//cout<<ans<<endl;
ans+=computevalue(s,s);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
3 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
532 KB |
Output is correct |
6 |
Correct |
2 ms |
532 KB |
Output is correct |
7 |
Correct |
3 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
576 KB |
Output is correct |
11 |
Correct |
3 ms |
576 KB |
Output is correct |
12 |
Correct |
2 ms |
616 KB |
Output is correct |
13 |
Correct |
3 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
2 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
3 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
532 KB |
Output is correct |
6 |
Correct |
2 ms |
532 KB |
Output is correct |
7 |
Correct |
3 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
576 KB |
Output is correct |
11 |
Correct |
3 ms |
576 KB |
Output is correct |
12 |
Correct |
2 ms |
616 KB |
Output is correct |
13 |
Correct |
3 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
2 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
19 |
Correct |
2 ms |
772 KB |
Output is correct |
20 |
Correct |
2 ms |
772 KB |
Output is correct |
21 |
Correct |
3 ms |
772 KB |
Output is correct |
22 |
Correct |
3 ms |
772 KB |
Output is correct |
23 |
Correct |
3 ms |
772 KB |
Output is correct |
24 |
Correct |
3 ms |
772 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
2 ms |
772 KB |
Output is correct |
27 |
Correct |
3 ms |
772 KB |
Output is correct |
28 |
Correct |
3 ms |
772 KB |
Output is correct |
29 |
Correct |
3 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
3 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
532 KB |
Output is correct |
6 |
Correct |
2 ms |
532 KB |
Output is correct |
7 |
Correct |
3 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
576 KB |
Output is correct |
11 |
Correct |
3 ms |
576 KB |
Output is correct |
12 |
Correct |
2 ms |
616 KB |
Output is correct |
13 |
Correct |
3 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
2 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
19 |
Correct |
2 ms |
772 KB |
Output is correct |
20 |
Correct |
2 ms |
772 KB |
Output is correct |
21 |
Correct |
3 ms |
772 KB |
Output is correct |
22 |
Correct |
3 ms |
772 KB |
Output is correct |
23 |
Correct |
3 ms |
772 KB |
Output is correct |
24 |
Correct |
3 ms |
772 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
2 ms |
772 KB |
Output is correct |
27 |
Correct |
3 ms |
772 KB |
Output is correct |
28 |
Correct |
3 ms |
772 KB |
Output is correct |
29 |
Correct |
3 ms |
772 KB |
Output is correct |
30 |
Correct |
168 ms |
14500 KB |
Output is correct |
31 |
Correct |
184 ms |
14564 KB |
Output is correct |
32 |
Correct |
142 ms |
14564 KB |
Output is correct |
33 |
Correct |
429 ms |
104732 KB |
Output is correct |
34 |
Correct |
479 ms |
104732 KB |
Output is correct |
35 |
Correct |
384 ms |
104732 KB |
Output is correct |
36 |
Correct |
202 ms |
104732 KB |
Output is correct |
37 |
Correct |
154 ms |
104732 KB |
Output is correct |
38 |
Correct |
162 ms |
104732 KB |
Output is correct |
39 |
Correct |
178 ms |
104732 KB |
Output is correct |
40 |
Correct |
168 ms |
104732 KB |
Output is correct |
41 |
Correct |
174 ms |
104732 KB |
Output is correct |
42 |
Correct |
159 ms |
104732 KB |
Output is correct |
43 |
Correct |
142 ms |
104732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
104732 KB |
Output is correct |
2 |
Correct |
4 ms |
104732 KB |
Output is correct |
3 |
Correct |
38 ms |
104732 KB |
Output is correct |
4 |
Correct |
62 ms |
104732 KB |
Output is correct |
5 |
Correct |
63 ms |
104732 KB |
Output is correct |
6 |
Correct |
3 ms |
104732 KB |
Output is correct |
7 |
Correct |
3 ms |
104732 KB |
Output is correct |
8 |
Correct |
2 ms |
104732 KB |
Output is correct |
9 |
Correct |
3 ms |
104732 KB |
Output is correct |
10 |
Correct |
3 ms |
104732 KB |
Output is correct |
11 |
Correct |
7 ms |
104732 KB |
Output is correct |
12 |
Correct |
8 ms |
104732 KB |
Output is correct |
13 |
Correct |
3 ms |
104732 KB |
Output is correct |
14 |
Correct |
4 ms |
104732 KB |
Output is correct |
15 |
Correct |
2 ms |
104732 KB |
Output is correct |
16 |
Correct |
2 ms |
104732 KB |
Output is correct |
17 |
Correct |
4 ms |
104732 KB |
Output is correct |
18 |
Correct |
4 ms |
104732 KB |
Output is correct |
19 |
Correct |
4 ms |
104732 KB |
Output is correct |
20 |
Correct |
3 ms |
104732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
3 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
532 KB |
Output is correct |
6 |
Correct |
2 ms |
532 KB |
Output is correct |
7 |
Correct |
3 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
3 ms |
576 KB |
Output is correct |
10 |
Correct |
3 ms |
576 KB |
Output is correct |
11 |
Correct |
3 ms |
576 KB |
Output is correct |
12 |
Correct |
2 ms |
616 KB |
Output is correct |
13 |
Correct |
3 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
2 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
19 |
Correct |
2 ms |
772 KB |
Output is correct |
20 |
Correct |
2 ms |
772 KB |
Output is correct |
21 |
Correct |
3 ms |
772 KB |
Output is correct |
22 |
Correct |
3 ms |
772 KB |
Output is correct |
23 |
Correct |
3 ms |
772 KB |
Output is correct |
24 |
Correct |
3 ms |
772 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
2 ms |
772 KB |
Output is correct |
27 |
Correct |
3 ms |
772 KB |
Output is correct |
28 |
Correct |
3 ms |
772 KB |
Output is correct |
29 |
Correct |
3 ms |
772 KB |
Output is correct |
30 |
Correct |
168 ms |
14500 KB |
Output is correct |
31 |
Correct |
184 ms |
14564 KB |
Output is correct |
32 |
Correct |
142 ms |
14564 KB |
Output is correct |
33 |
Correct |
429 ms |
104732 KB |
Output is correct |
34 |
Correct |
479 ms |
104732 KB |
Output is correct |
35 |
Correct |
384 ms |
104732 KB |
Output is correct |
36 |
Correct |
202 ms |
104732 KB |
Output is correct |
37 |
Correct |
154 ms |
104732 KB |
Output is correct |
38 |
Correct |
162 ms |
104732 KB |
Output is correct |
39 |
Correct |
178 ms |
104732 KB |
Output is correct |
40 |
Correct |
168 ms |
104732 KB |
Output is correct |
41 |
Correct |
174 ms |
104732 KB |
Output is correct |
42 |
Correct |
159 ms |
104732 KB |
Output is correct |
43 |
Correct |
142 ms |
104732 KB |
Output is correct |
44 |
Correct |
74 ms |
104732 KB |
Output is correct |
45 |
Correct |
4 ms |
104732 KB |
Output is correct |
46 |
Correct |
38 ms |
104732 KB |
Output is correct |
47 |
Correct |
62 ms |
104732 KB |
Output is correct |
48 |
Correct |
63 ms |
104732 KB |
Output is correct |
49 |
Correct |
3 ms |
104732 KB |
Output is correct |
50 |
Correct |
3 ms |
104732 KB |
Output is correct |
51 |
Correct |
2 ms |
104732 KB |
Output is correct |
52 |
Correct |
3 ms |
104732 KB |
Output is correct |
53 |
Correct |
3 ms |
104732 KB |
Output is correct |
54 |
Correct |
7 ms |
104732 KB |
Output is correct |
55 |
Correct |
8 ms |
104732 KB |
Output is correct |
56 |
Correct |
3 ms |
104732 KB |
Output is correct |
57 |
Correct |
4 ms |
104732 KB |
Output is correct |
58 |
Correct |
2 ms |
104732 KB |
Output is correct |
59 |
Correct |
2 ms |
104732 KB |
Output is correct |
60 |
Correct |
4 ms |
104732 KB |
Output is correct |
61 |
Correct |
4 ms |
104732 KB |
Output is correct |
62 |
Correct |
4 ms |
104732 KB |
Output is correct |
63 |
Correct |
3 ms |
104732 KB |
Output is correct |
64 |
Execution timed out |
2053 ms |
104732 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |