#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--;
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;
}
else{
ma[i][j]=j;mi[i][j]=i;
}
if(j){
chmax(ma[i][j],ma[i][j-1]);
chmin(mi[i][j],mi[i][j-1]);
}
}
dp[s][s]=0;
rep(len,n)rep(i,n){
ll j=i+len;if(j>=n)continue;
chmin(dp[mi[i][j]][ma[i][j]],dp[i][j]);
if(j!=n-1)chmin(dp[i][j+1],dp[i][j]+2);
if(i)chmin(dp[i-1][j],dp[i][j]+2);
}
return dp[a][b]+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 |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 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 |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
32 ms |
23916 KB |
Output is correct |
20 |
Correct |
32 ms |
23916 KB |
Output is correct |
21 |
Correct |
31 ms |
23916 KB |
Output is correct |
22 |
Correct |
32 ms |
23936 KB |
Output is correct |
23 |
Correct |
32 ms |
23916 KB |
Output is correct |
24 |
Correct |
33 ms |
23916 KB |
Output is correct |
25 |
Correct |
32 ms |
23916 KB |
Output is correct |
26 |
Correct |
32 ms |
23916 KB |
Output is correct |
27 |
Correct |
32 ms |
23916 KB |
Output is correct |
28 |
Correct |
32 ms |
23916 KB |
Output is correct |
29 |
Correct |
31 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 |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
32 ms |
23916 KB |
Output is correct |
20 |
Correct |
32 ms |
23916 KB |
Output is correct |
21 |
Correct |
31 ms |
23916 KB |
Output is correct |
22 |
Correct |
32 ms |
23936 KB |
Output is correct |
23 |
Correct |
32 ms |
23916 KB |
Output is correct |
24 |
Correct |
33 ms |
23916 KB |
Output is correct |
25 |
Correct |
32 ms |
23916 KB |
Output is correct |
26 |
Correct |
32 ms |
23916 KB |
Output is correct |
27 |
Correct |
32 ms |
23916 KB |
Output is correct |
28 |
Correct |
32 ms |
23916 KB |
Output is correct |
29 |
Correct |
31 ms |
23916 KB |
Output is correct |
30 |
Correct |
193 ms |
15932 KB |
Output is correct |
31 |
Correct |
190 ms |
15980 KB |
Output is correct |
32 |
Correct |
119 ms |
16000 KB |
Output is correct |
33 |
Correct |
157 ms |
25172 KB |
Output is correct |
34 |
Correct |
160 ms |
25172 KB |
Output is correct |
35 |
Correct |
163 ms |
25508 KB |
Output is correct |
36 |
Correct |
152 ms |
21848 KB |
Output is correct |
37 |
Correct |
125 ms |
16616 KB |
Output is correct |
38 |
Correct |
129 ms |
16236 KB |
Output is correct |
39 |
Correct |
128 ms |
16108 KB |
Output is correct |
40 |
Correct |
132 ms |
16108 KB |
Output is correct |
41 |
Correct |
155 ms |
15932 KB |
Output is correct |
42 |
Correct |
146 ms |
15980 KB |
Output is correct |
43 |
Correct |
172 ms |
20188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
23916 KB |
Output is correct |
2 |
Correct |
32 ms |
23916 KB |
Output is correct |
3 |
Correct |
32 ms |
23916 KB |
Output is correct |
4 |
Correct |
32 ms |
23916 KB |
Output is correct |
5 |
Correct |
32 ms |
23916 KB |
Output is correct |
6 |
Correct |
32 ms |
23916 KB |
Output is correct |
7 |
Correct |
32 ms |
23916 KB |
Output is correct |
8 |
Correct |
33 ms |
23916 KB |
Output is correct |
9 |
Correct |
33 ms |
23916 KB |
Output is correct |
10 |
Correct |
31 ms |
23916 KB |
Output is correct |
11 |
Correct |
34 ms |
24044 KB |
Output is correct |
12 |
Correct |
34 ms |
24044 KB |
Output is correct |
13 |
Correct |
33 ms |
23916 KB |
Output is correct |
14 |
Correct |
32 ms |
23916 KB |
Output is correct |
15 |
Correct |
32 ms |
23916 KB |
Output is correct |
16 |
Correct |
32 ms |
23916 KB |
Output is correct |
17 |
Correct |
36 ms |
23916 KB |
Output is correct |
18 |
Correct |
34 ms |
23916 KB |
Output is correct |
19 |
Correct |
33 ms |
23916 KB |
Output is correct |
20 |
Correct |
32 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 |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
32 ms |
23916 KB |
Output is correct |
20 |
Correct |
32 ms |
23916 KB |
Output is correct |
21 |
Correct |
31 ms |
23916 KB |
Output is correct |
22 |
Correct |
32 ms |
23936 KB |
Output is correct |
23 |
Correct |
32 ms |
23916 KB |
Output is correct |
24 |
Correct |
33 ms |
23916 KB |
Output is correct |
25 |
Correct |
32 ms |
23916 KB |
Output is correct |
26 |
Correct |
32 ms |
23916 KB |
Output is correct |
27 |
Correct |
32 ms |
23916 KB |
Output is correct |
28 |
Correct |
32 ms |
23916 KB |
Output is correct |
29 |
Correct |
31 ms |
23916 KB |
Output is correct |
30 |
Correct |
193 ms |
15932 KB |
Output is correct |
31 |
Correct |
190 ms |
15980 KB |
Output is correct |
32 |
Correct |
119 ms |
16000 KB |
Output is correct |
33 |
Correct |
157 ms |
25172 KB |
Output is correct |
34 |
Correct |
160 ms |
25172 KB |
Output is correct |
35 |
Correct |
163 ms |
25508 KB |
Output is correct |
36 |
Correct |
152 ms |
21848 KB |
Output is correct |
37 |
Correct |
125 ms |
16616 KB |
Output is correct |
38 |
Correct |
129 ms |
16236 KB |
Output is correct |
39 |
Correct |
128 ms |
16108 KB |
Output is correct |
40 |
Correct |
132 ms |
16108 KB |
Output is correct |
41 |
Correct |
155 ms |
15932 KB |
Output is correct |
42 |
Correct |
146 ms |
15980 KB |
Output is correct |
43 |
Correct |
172 ms |
20188 KB |
Output is correct |
44 |
Correct |
32 ms |
23916 KB |
Output is correct |
45 |
Correct |
32 ms |
23916 KB |
Output is correct |
46 |
Correct |
32 ms |
23916 KB |
Output is correct |
47 |
Correct |
32 ms |
23916 KB |
Output is correct |
48 |
Correct |
32 ms |
23916 KB |
Output is correct |
49 |
Correct |
32 ms |
23916 KB |
Output is correct |
50 |
Correct |
32 ms |
23916 KB |
Output is correct |
51 |
Correct |
33 ms |
23916 KB |
Output is correct |
52 |
Correct |
33 ms |
23916 KB |
Output is correct |
53 |
Correct |
31 ms |
23916 KB |
Output is correct |
54 |
Correct |
34 ms |
24044 KB |
Output is correct |
55 |
Correct |
34 ms |
24044 KB |
Output is correct |
56 |
Correct |
33 ms |
23916 KB |
Output is correct |
57 |
Correct |
32 ms |
23916 KB |
Output is correct |
58 |
Correct |
32 ms |
23916 KB |
Output is correct |
59 |
Correct |
32 ms |
23916 KB |
Output is correct |
60 |
Correct |
36 ms |
23916 KB |
Output is correct |
61 |
Correct |
34 ms |
23916 KB |
Output is correct |
62 |
Correct |
33 ms |
23916 KB |
Output is correct |
63 |
Correct |
32 ms |
23916 KB |
Output is correct |
64 |
Incorrect |
146 ms |
25056 KB |
3rd lines differ - on the 1st token, expected: '9339550', found: '9338938' |
65 |
Halted |
0 ms |
0 KB |
- |