#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=2e6+99;
const ll inf=1e15;
int n,vis[N],mx1[N],mx2[N],mn1[N],mn2[N];
ll Max[N],dist[N];
queue<int> Q;
vector<int> vec,prt[N];
vector<pair<int,int>> g[N];
ll minimum_walk(vector<int> p, int s){
ll sum=0,ans=0;
n=p.size();
int l=n,r=0;
f(i,0,n){
sum+=abs(p[i]-i);
if(p[i]!=i) minm(l,i),maxm(r,i);
}
minm(l,s);
maxm(r,s);
if(l==r) return 0;
s-=l;
f(i,l,r+1) vec.pb(p[i]-l);
p=vec;
n=p.size();
int mx=0;
l=0,r=n-1;
f(i,0,s){
maxm(mx,p[i]);
if(mx==i){
ans+=2;
l=i+1;
}
}
int mn=n;
f_(i,n-1,s+1){
minm(mn,p[i]);
if(mn==i){
ans+=2;
r=i-1;
}
}
if(l==r) return sum+ans;
vec.clear(); s-=l;
f(i,l,r+1) vec.pb(p[i]-l);
p=vec;
n=p.size();
f(i,0,n){
g[i].pb({p[i],0});
}
f_(i,s,1){
g[i].pb({i-1,1});
g[i-1].pb({i,0});
}
f(i,s,n-1){
g[i].pb({i+1,1});
g[i+1].pb({i,0});
}
prt[0].pb(s);
f(i,0,n) dist[i]=(i==s ? 0 : N);
f(i,0,n){
for(auto u : prt[i]){
if(dist[u]<i) continue ;
for(auto [v,w] : g[u]){
if(dist[u]+w<dist[v]){
dist[v]=dist[u]+w;
prt[dist[v]].pb(v);
}
}
}
}
return ans+sum+2ll*dist[0];
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:86:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for(auto [v,w] : g[u]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
94176 KB |
Output is correct |
2 |
Correct |
49 ms |
94212 KB |
Output is correct |
3 |
Correct |
65 ms |
94180 KB |
Output is correct |
4 |
Correct |
49 ms |
94152 KB |
Output is correct |
5 |
Correct |
49 ms |
94328 KB |
Output is correct |
6 |
Correct |
52 ms |
94216 KB |
Output is correct |
7 |
Correct |
61 ms |
94248 KB |
Output is correct |
8 |
Correct |
76 ms |
94228 KB |
Output is correct |
9 |
Correct |
51 ms |
94196 KB |
Output is correct |
10 |
Correct |
56 ms |
94140 KB |
Output is correct |
11 |
Correct |
56 ms |
94228 KB |
Output is correct |
12 |
Correct |
49 ms |
94188 KB |
Output is correct |
13 |
Correct |
51 ms |
94176 KB |
Output is correct |
14 |
Correct |
50 ms |
94184 KB |
Output is correct |
15 |
Correct |
56 ms |
94160 KB |
Output is correct |
16 |
Correct |
50 ms |
94112 KB |
Output is correct |
17 |
Correct |
47 ms |
94204 KB |
Output is correct |
18 |
Correct |
50 ms |
94244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
94176 KB |
Output is correct |
2 |
Correct |
49 ms |
94212 KB |
Output is correct |
3 |
Correct |
65 ms |
94180 KB |
Output is correct |
4 |
Correct |
49 ms |
94152 KB |
Output is correct |
5 |
Correct |
49 ms |
94328 KB |
Output is correct |
6 |
Correct |
52 ms |
94216 KB |
Output is correct |
7 |
Correct |
61 ms |
94248 KB |
Output is correct |
8 |
Correct |
76 ms |
94228 KB |
Output is correct |
9 |
Correct |
51 ms |
94196 KB |
Output is correct |
10 |
Correct |
56 ms |
94140 KB |
Output is correct |
11 |
Correct |
56 ms |
94228 KB |
Output is correct |
12 |
Correct |
49 ms |
94188 KB |
Output is correct |
13 |
Correct |
51 ms |
94176 KB |
Output is correct |
14 |
Correct |
50 ms |
94184 KB |
Output is correct |
15 |
Correct |
56 ms |
94160 KB |
Output is correct |
16 |
Correct |
50 ms |
94112 KB |
Output is correct |
17 |
Correct |
47 ms |
94204 KB |
Output is correct |
18 |
Correct |
50 ms |
94244 KB |
Output is correct |
19 |
Correct |
50 ms |
94284 KB |
Output is correct |
20 |
Correct |
52 ms |
94244 KB |
Output is correct |
21 |
Correct |
53 ms |
94228 KB |
Output is correct |
22 |
Correct |
50 ms |
94240 KB |
Output is correct |
23 |
Correct |
50 ms |
94176 KB |
Output is correct |
24 |
Correct |
55 ms |
94156 KB |
Output is correct |
25 |
Correct |
52 ms |
94244 KB |
Output is correct |
26 |
Correct |
46 ms |
94232 KB |
Output is correct |
27 |
Correct |
48 ms |
94188 KB |
Output is correct |
28 |
Correct |
63 ms |
94200 KB |
Output is correct |
29 |
Correct |
48 ms |
94284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
94176 KB |
Output is correct |
2 |
Correct |
49 ms |
94212 KB |
Output is correct |
3 |
Correct |
65 ms |
94180 KB |
Output is correct |
4 |
Correct |
49 ms |
94152 KB |
Output is correct |
5 |
Correct |
49 ms |
94328 KB |
Output is correct |
6 |
Correct |
52 ms |
94216 KB |
Output is correct |
7 |
Correct |
61 ms |
94248 KB |
Output is correct |
8 |
Correct |
76 ms |
94228 KB |
Output is correct |
9 |
Correct |
51 ms |
94196 KB |
Output is correct |
10 |
Correct |
56 ms |
94140 KB |
Output is correct |
11 |
Correct |
56 ms |
94228 KB |
Output is correct |
12 |
Correct |
49 ms |
94188 KB |
Output is correct |
13 |
Correct |
51 ms |
94176 KB |
Output is correct |
14 |
Correct |
50 ms |
94184 KB |
Output is correct |
15 |
Correct |
56 ms |
94160 KB |
Output is correct |
16 |
Correct |
50 ms |
94112 KB |
Output is correct |
17 |
Correct |
47 ms |
94204 KB |
Output is correct |
18 |
Correct |
50 ms |
94244 KB |
Output is correct |
19 |
Correct |
50 ms |
94284 KB |
Output is correct |
20 |
Correct |
52 ms |
94244 KB |
Output is correct |
21 |
Correct |
53 ms |
94228 KB |
Output is correct |
22 |
Correct |
50 ms |
94240 KB |
Output is correct |
23 |
Correct |
50 ms |
94176 KB |
Output is correct |
24 |
Correct |
55 ms |
94156 KB |
Output is correct |
25 |
Correct |
52 ms |
94244 KB |
Output is correct |
26 |
Correct |
46 ms |
94232 KB |
Output is correct |
27 |
Correct |
48 ms |
94188 KB |
Output is correct |
28 |
Correct |
63 ms |
94200 KB |
Output is correct |
29 |
Correct |
48 ms |
94284 KB |
Output is correct |
30 |
Correct |
308 ms |
160744 KB |
Output is correct |
31 |
Correct |
299 ms |
160848 KB |
Output is correct |
32 |
Correct |
141 ms |
102000 KB |
Output is correct |
33 |
Correct |
152 ms |
106196 KB |
Output is correct |
34 |
Correct |
145 ms |
106204 KB |
Output is correct |
35 |
Correct |
166 ms |
106300 KB |
Output is correct |
36 |
Correct |
145 ms |
106304 KB |
Output is correct |
37 |
Correct |
148 ms |
106204 KB |
Output is correct |
38 |
Correct |
143 ms |
106200 KB |
Output is correct |
39 |
Correct |
163 ms |
106224 KB |
Output is correct |
40 |
Correct |
150 ms |
106584 KB |
Output is correct |
41 |
Correct |
213 ms |
120228 KB |
Output is correct |
42 |
Correct |
163 ms |
106196 KB |
Output is correct |
43 |
Correct |
276 ms |
160904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
94224 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4002942' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
94176 KB |
Output is correct |
2 |
Correct |
49 ms |
94212 KB |
Output is correct |
3 |
Correct |
65 ms |
94180 KB |
Output is correct |
4 |
Correct |
49 ms |
94152 KB |
Output is correct |
5 |
Correct |
49 ms |
94328 KB |
Output is correct |
6 |
Correct |
52 ms |
94216 KB |
Output is correct |
7 |
Correct |
61 ms |
94248 KB |
Output is correct |
8 |
Correct |
76 ms |
94228 KB |
Output is correct |
9 |
Correct |
51 ms |
94196 KB |
Output is correct |
10 |
Correct |
56 ms |
94140 KB |
Output is correct |
11 |
Correct |
56 ms |
94228 KB |
Output is correct |
12 |
Correct |
49 ms |
94188 KB |
Output is correct |
13 |
Correct |
51 ms |
94176 KB |
Output is correct |
14 |
Correct |
50 ms |
94184 KB |
Output is correct |
15 |
Correct |
56 ms |
94160 KB |
Output is correct |
16 |
Correct |
50 ms |
94112 KB |
Output is correct |
17 |
Correct |
47 ms |
94204 KB |
Output is correct |
18 |
Correct |
50 ms |
94244 KB |
Output is correct |
19 |
Correct |
50 ms |
94284 KB |
Output is correct |
20 |
Correct |
52 ms |
94244 KB |
Output is correct |
21 |
Correct |
53 ms |
94228 KB |
Output is correct |
22 |
Correct |
50 ms |
94240 KB |
Output is correct |
23 |
Correct |
50 ms |
94176 KB |
Output is correct |
24 |
Correct |
55 ms |
94156 KB |
Output is correct |
25 |
Correct |
52 ms |
94244 KB |
Output is correct |
26 |
Correct |
46 ms |
94232 KB |
Output is correct |
27 |
Correct |
48 ms |
94188 KB |
Output is correct |
28 |
Correct |
63 ms |
94200 KB |
Output is correct |
29 |
Correct |
48 ms |
94284 KB |
Output is correct |
30 |
Correct |
308 ms |
160744 KB |
Output is correct |
31 |
Correct |
299 ms |
160848 KB |
Output is correct |
32 |
Correct |
141 ms |
102000 KB |
Output is correct |
33 |
Correct |
152 ms |
106196 KB |
Output is correct |
34 |
Correct |
145 ms |
106204 KB |
Output is correct |
35 |
Correct |
166 ms |
106300 KB |
Output is correct |
36 |
Correct |
145 ms |
106304 KB |
Output is correct |
37 |
Correct |
148 ms |
106204 KB |
Output is correct |
38 |
Correct |
143 ms |
106200 KB |
Output is correct |
39 |
Correct |
163 ms |
106224 KB |
Output is correct |
40 |
Correct |
150 ms |
106584 KB |
Output is correct |
41 |
Correct |
213 ms |
120228 KB |
Output is correct |
42 |
Correct |
163 ms |
106196 KB |
Output is correct |
43 |
Correct |
276 ms |
160904 KB |
Output is correct |
44 |
Incorrect |
55 ms |
94224 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4002942' |
45 |
Halted |
0 ms |
0 KB |
- |