#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1e6;
int n, a, b, who[mxN];
vector<int> adj[mxN], adj2[mxN], st, adj3[mxN];
bool vis[mxN];
ar<int, 3> dp[mxN];
void dfs1(int u) {
vis[u]=1;
for(int v : adj[u])
if(!vis[v])
dfs1(v);
st.push_back(u);
}
void dfs2(int u, int r) {
vis[u]=0;
who[u]=r;
for(int v : adj2[u])
if(vis[v])
dfs2(v, r);
}
ll minimum_walk(vector<int> p, int s) {
n=p.size();
ll ans=0;
for(; a<s&&p[a]==a; ++a);
for(b=n-1; b>s&&p[b]==b; --b);
for(int i=a; i<s; ++i)
adj[i].push_back(i+1);
for(int i=b; i>s; --i)
adj[i].push_back(i-1);
for(int i=a; i<=b; ++i) {
ans+=abs(i-p[i]);
adj[i].push_back(p[i]);
}
for(int i=a; i<=b; ++i)
if(!vis[i])
dfs1(i);
for(int i=a; i<=b; ++i)
for(int j : adj[i])
adj2[j].push_back(i);
for(int i=st.size()-1; ~i; --i) {
if(!vis[st[i]])
continue;
dfs2(st[i], st[i]);
dp[st[i]]={n, n, n};
if(!vis[a]&&st[i]==who[a])
dp[st[i]][0]=0;
if(!vis[b]&&st[i]==who[b])
dp[st[i]][1]=0;
for(int j : adj2[st[i]])
if(who[j]!=st[i])
for(int k : {0, 1, 2})
dp[st[i]][k]=min(dp[who[j]][k]+1, dp[st[i]][k]);
dp[st[i]][2]=min(dp[st[i]][0]+dp[st[i]][1], dp[st[i]][2]);
}
return ans+2*dp[who[s]][2];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
70776 KB |
Output is correct |
2 |
Correct |
45 ms |
70824 KB |
Output is correct |
3 |
Correct |
43 ms |
70784 KB |
Output is correct |
4 |
Correct |
43 ms |
70776 KB |
Output is correct |
5 |
Correct |
42 ms |
70776 KB |
Output is correct |
6 |
Correct |
44 ms |
70776 KB |
Output is correct |
7 |
Correct |
42 ms |
70776 KB |
Output is correct |
8 |
Correct |
42 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70776 KB |
Output is correct |
10 |
Correct |
42 ms |
70780 KB |
Output is correct |
11 |
Correct |
42 ms |
70776 KB |
Output is correct |
12 |
Correct |
43 ms |
70776 KB |
Output is correct |
13 |
Correct |
43 ms |
70776 KB |
Output is correct |
14 |
Correct |
42 ms |
70784 KB |
Output is correct |
15 |
Correct |
43 ms |
70776 KB |
Output is correct |
16 |
Correct |
43 ms |
70776 KB |
Output is correct |
17 |
Correct |
42 ms |
70784 KB |
Output is correct |
18 |
Correct |
42 ms |
70780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
70776 KB |
Output is correct |
2 |
Correct |
45 ms |
70824 KB |
Output is correct |
3 |
Correct |
43 ms |
70784 KB |
Output is correct |
4 |
Correct |
43 ms |
70776 KB |
Output is correct |
5 |
Correct |
42 ms |
70776 KB |
Output is correct |
6 |
Correct |
44 ms |
70776 KB |
Output is correct |
7 |
Correct |
42 ms |
70776 KB |
Output is correct |
8 |
Correct |
42 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70776 KB |
Output is correct |
10 |
Correct |
42 ms |
70780 KB |
Output is correct |
11 |
Correct |
42 ms |
70776 KB |
Output is correct |
12 |
Correct |
43 ms |
70776 KB |
Output is correct |
13 |
Correct |
43 ms |
70776 KB |
Output is correct |
14 |
Correct |
42 ms |
70784 KB |
Output is correct |
15 |
Correct |
43 ms |
70776 KB |
Output is correct |
16 |
Correct |
43 ms |
70776 KB |
Output is correct |
17 |
Correct |
42 ms |
70784 KB |
Output is correct |
18 |
Correct |
42 ms |
70780 KB |
Output is correct |
19 |
Correct |
44 ms |
70904 KB |
Output is correct |
20 |
Correct |
43 ms |
71032 KB |
Output is correct |
21 |
Correct |
43 ms |
70776 KB |
Output is correct |
22 |
Incorrect |
43 ms |
70904 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2788' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
70776 KB |
Output is correct |
2 |
Correct |
45 ms |
70824 KB |
Output is correct |
3 |
Correct |
43 ms |
70784 KB |
Output is correct |
4 |
Correct |
43 ms |
70776 KB |
Output is correct |
5 |
Correct |
42 ms |
70776 KB |
Output is correct |
6 |
Correct |
44 ms |
70776 KB |
Output is correct |
7 |
Correct |
42 ms |
70776 KB |
Output is correct |
8 |
Correct |
42 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70776 KB |
Output is correct |
10 |
Correct |
42 ms |
70780 KB |
Output is correct |
11 |
Correct |
42 ms |
70776 KB |
Output is correct |
12 |
Correct |
43 ms |
70776 KB |
Output is correct |
13 |
Correct |
43 ms |
70776 KB |
Output is correct |
14 |
Correct |
42 ms |
70784 KB |
Output is correct |
15 |
Correct |
43 ms |
70776 KB |
Output is correct |
16 |
Correct |
43 ms |
70776 KB |
Output is correct |
17 |
Correct |
42 ms |
70784 KB |
Output is correct |
18 |
Correct |
42 ms |
70780 KB |
Output is correct |
19 |
Correct |
44 ms |
70904 KB |
Output is correct |
20 |
Correct |
43 ms |
71032 KB |
Output is correct |
21 |
Correct |
43 ms |
70776 KB |
Output is correct |
22 |
Incorrect |
43 ms |
70904 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2788' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
70876 KB |
Output is correct |
2 |
Correct |
43 ms |
70912 KB |
Output is correct |
3 |
Correct |
44 ms |
70904 KB |
Output is correct |
4 |
Incorrect |
43 ms |
70904 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '2818' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
70776 KB |
Output is correct |
2 |
Correct |
45 ms |
70824 KB |
Output is correct |
3 |
Correct |
43 ms |
70784 KB |
Output is correct |
4 |
Correct |
43 ms |
70776 KB |
Output is correct |
5 |
Correct |
42 ms |
70776 KB |
Output is correct |
6 |
Correct |
44 ms |
70776 KB |
Output is correct |
7 |
Correct |
42 ms |
70776 KB |
Output is correct |
8 |
Correct |
42 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70776 KB |
Output is correct |
10 |
Correct |
42 ms |
70780 KB |
Output is correct |
11 |
Correct |
42 ms |
70776 KB |
Output is correct |
12 |
Correct |
43 ms |
70776 KB |
Output is correct |
13 |
Correct |
43 ms |
70776 KB |
Output is correct |
14 |
Correct |
42 ms |
70784 KB |
Output is correct |
15 |
Correct |
43 ms |
70776 KB |
Output is correct |
16 |
Correct |
43 ms |
70776 KB |
Output is correct |
17 |
Correct |
42 ms |
70784 KB |
Output is correct |
18 |
Correct |
42 ms |
70780 KB |
Output is correct |
19 |
Correct |
44 ms |
70904 KB |
Output is correct |
20 |
Correct |
43 ms |
71032 KB |
Output is correct |
21 |
Correct |
43 ms |
70776 KB |
Output is correct |
22 |
Incorrect |
43 ms |
70904 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2788' |
23 |
Halted |
0 ms |
0 KB |
- |