#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){for(auto x:v)outv(x);}
template<class T> void outp(T p){cout<<'('<<p.fi<<','<<p.se<<')'<<endl;}
template<class T> void outvp(T v){for(auto p:v)cout<<'('<<p.fi<<','<<p.se<<')';cout<<endl;}
const ll inf=1001001001001001001;
ll minimum_walk(vector<int> p,int s){
int n=p.size();
vi gr(n,-1);
rep(i,n)if(p[i]==i)gr[i]=-2;
ll ans=0,c=0;
vp v;
rep(i,n)if(gr[i]==-1){
ll mi=i,ma=i;
gr[i]=c;
ll nw=p[i];
ans+=abs(nw-i);
while(nw!=i){
gr[nw]=c;
chmax(ma,nw);chmin(mi,nw);
ans+=abs(nw-p[nw]);
nw=p[nw];
}
c++;
v.pb(mi,ma);
}
v.pb(s,s);
if(gr[s]==-2)gr[s]=c;
if(n<=1000){
vvi dp(n,vi(n,inf));
ll a=0,b=n-1;
while(gr[a]==-2)a++;
while(gr[b]==-2)b--;
dp[a][b]=0;
vvi ma(n,vi(n,-inf)),mi(n,vi(n,inf));
rep(i,n)REP(j,i,n){
if(gr[j]!=-2){
ma[i][j]=v[gr[j]].se;
mi[i][j]=v[gr[j]].fi;
}
if(j){
chmax(ma[i][j],ma[i][j-1]);
chmin(mi[i][j],mi[i][j-1]);
}
}
function<ll(ll,ll)> dfs=[&](ll l,ll r){
if(dp[l][r]!=inf)return dp[l][r];
if(mi[l][r]<l){
dp[l][r]=dfs(mi[l][r],r);
return dp[l][r];
}
if(ma[l][r]>r){
dp[l][r]=dfs(l,ma[l][r]);
return dp[l][r];
}
if(r!=n+1)chmin(dp[l][r],dfs(l,r+1)+2);
if(l)chmin(dp[l][r],dfs(l-1,r)+2);
return dp[l][r];
};
return dfs(s,s)+ans;
}
sort(all(v));
vp st;
for(auto x:v){
if(st.size()&&st.back().se>=x.fi)chmax(st.back().se,x.se);
else{
if(st.size())ans+=(x.fi-st.back().se)*2;
st.pb(x);
}
}
return ans;
}
# |
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 |
288 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
288 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
20 ms |
24044 KB |
Output is correct |
20 |
Correct |
20 ms |
23916 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
19 ms |
24044 KB |
Output is correct |
23 |
Correct |
19 ms |
24044 KB |
Output is correct |
24 |
Correct |
19 ms |
24044 KB |
Output is correct |
25 |
Correct |
20 ms |
24044 KB |
Output is correct |
26 |
Correct |
21 ms |
23916 KB |
Output is correct |
27 |
Correct |
20 ms |
23916 KB |
Output is correct |
28 |
Correct |
20 ms |
23916 KB |
Output is correct |
29 |
Correct |
19 ms |
23916 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 |
288 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
20 ms |
24044 KB |
Output is correct |
20 |
Correct |
20 ms |
23916 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
19 ms |
24044 KB |
Output is correct |
23 |
Correct |
19 ms |
24044 KB |
Output is correct |
24 |
Correct |
19 ms |
24044 KB |
Output is correct |
25 |
Correct |
20 ms |
24044 KB |
Output is correct |
26 |
Correct |
21 ms |
23916 KB |
Output is correct |
27 |
Correct |
20 ms |
23916 KB |
Output is correct |
28 |
Correct |
20 ms |
23916 KB |
Output is correct |
29 |
Correct |
19 ms |
23916 KB |
Output is correct |
30 |
Correct |
208 ms |
22764 KB |
Output is correct |
31 |
Correct |
195 ms |
22636 KB |
Output is correct |
32 |
Correct |
134 ms |
22636 KB |
Output is correct |
33 |
Correct |
173 ms |
31980 KB |
Output is correct |
34 |
Correct |
169 ms |
31956 KB |
Output is correct |
35 |
Correct |
174 ms |
32228 KB |
Output is correct |
36 |
Correct |
165 ms |
28500 KB |
Output is correct |
37 |
Correct |
144 ms |
23420 KB |
Output is correct |
38 |
Correct |
136 ms |
22892 KB |
Output is correct |
39 |
Correct |
149 ms |
23020 KB |
Output is correct |
40 |
Correct |
146 ms |
22764 KB |
Output is correct |
41 |
Correct |
162 ms |
22636 KB |
Output is correct |
42 |
Correct |
152 ms |
22636 KB |
Output is correct |
43 |
Correct |
173 ms |
26972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
24052 KB |
Output is correct |
2 |
Correct |
19 ms |
23916 KB |
Output is correct |
3 |
Correct |
22 ms |
24044 KB |
Output is correct |
4 |
Incorrect |
29 ms |
24044 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1336' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
288 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
20 ms |
24044 KB |
Output is correct |
20 |
Correct |
20 ms |
23916 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
19 ms |
24044 KB |
Output is correct |
23 |
Correct |
19 ms |
24044 KB |
Output is correct |
24 |
Correct |
19 ms |
24044 KB |
Output is correct |
25 |
Correct |
20 ms |
24044 KB |
Output is correct |
26 |
Correct |
21 ms |
23916 KB |
Output is correct |
27 |
Correct |
20 ms |
23916 KB |
Output is correct |
28 |
Correct |
20 ms |
23916 KB |
Output is correct |
29 |
Correct |
19 ms |
23916 KB |
Output is correct |
30 |
Correct |
208 ms |
22764 KB |
Output is correct |
31 |
Correct |
195 ms |
22636 KB |
Output is correct |
32 |
Correct |
134 ms |
22636 KB |
Output is correct |
33 |
Correct |
173 ms |
31980 KB |
Output is correct |
34 |
Correct |
169 ms |
31956 KB |
Output is correct |
35 |
Correct |
174 ms |
32228 KB |
Output is correct |
36 |
Correct |
165 ms |
28500 KB |
Output is correct |
37 |
Correct |
144 ms |
23420 KB |
Output is correct |
38 |
Correct |
136 ms |
22892 KB |
Output is correct |
39 |
Correct |
149 ms |
23020 KB |
Output is correct |
40 |
Correct |
146 ms |
22764 KB |
Output is correct |
41 |
Correct |
162 ms |
22636 KB |
Output is correct |
42 |
Correct |
152 ms |
22636 KB |
Output is correct |
43 |
Correct |
173 ms |
26972 KB |
Output is correct |
44 |
Correct |
28 ms |
24052 KB |
Output is correct |
45 |
Correct |
19 ms |
23916 KB |
Output is correct |
46 |
Correct |
22 ms |
24044 KB |
Output is correct |
47 |
Incorrect |
29 ms |
24044 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1336' |
48 |
Halted |
0 ms |
0 KB |
- |