#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define lg length()
#define pb push_back
#define INF 2000000005
#define LINF 1000000000000000005
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(rng);}
#include "books.h"
long long minimum_walk(vector<int> p, int s) {
long long sum = 0;
int L = -1,R = -1;
int n = sz(p);
for(int i=0;i<n;i++) sum += abs(i - p[i]);
for(int i=0;i<n;i++){
if(p[i]!=i){
L = i;
break;
}
}
if(L == -1) return 0;
for(int i=n-1;i>=0;i--){
if(p[i]!=i){
R = i;
break;
}
}
L = min(L, s);
R = max(R, s);
vector<int> v(n, 0);
int l=s,r=s;
queue<int> q;
q.push(s);
long long ans = 0;
while(l != L || r != R){
while(q.size()){
int i = q.front();
q.pop();
if(!v[i]){
int ln=i,rn=i;
v[i] = 1;
int j = p[i];
while(j != i){
v[j] = 1;
ln = min(ln, j);
rn = max(rn, j);
j = p[j];
}
for(int j=ln;j<l;j++) q.push(j);
for(int j=r+1;j<=rn;j++) q.push(j);
l = min(l, ln); r = max(r, rn);
}
}
long long cl = 0, cr = 0;
vector<int> qL, qR;
int sl = -1, sr = -1;
int oldl = l, oldr = r;
while(l != L){
l--;
q.push(l);
cl += 2;
while(q.size()){
int i = q.front();
q.pop();
if(!v[i]){
int ln=i,rn=i;
v[i] = 1;
qL.push_back(i);
int j = p[i];
while(j != i){
v[j] = 1;
qL.push_back(j);
ln = min(ln, j);
rn = max(rn, j);
j = p[j];
}
if(rn > r){
sl = i;
break;
}
for(int j=ln;j<l;j++) q.push(j);
for(int j=r+1;j<=rn;j++) q.push(j);
l = min(l, ln); r = max(r, rn);
}
}
if(sl != -1) break;
}
for(int i : qL) v[i] = 0;
while(sz(q)) q.pop();
l = oldl; r = oldr;
while(r != R){
r++;
q.push(r);
cr += 2;
while(q.size()){
int i = q.front();
q.pop();
if(!v[i]){
int ln=i,rn=i;
v[i] = 1;
qL.push_back(i);
int j = p[i];
while(j != i){
v[j] = 1;
qL.push_back(j);
ln = min(ln, j);
rn = max(rn, j);
j = p[j];
}
if(ln < l){
sr = i;
break;
}
for(int j=ln;j<l;j++) q.push(j);
for(int j=r+1;j<=rn;j++) q.push(j);
l = min(l, ln); r = max(r, rn);
}
}
if(sr != -1) break;
}
for(int i : qR) v[i] = 0;
while(sz(q)) q.pop();
l = oldl; r = oldr;
if(sl == -1){
ans += cl + cr;
break;
}
else{
if(cl < cr) ans += cl, q.push(sl);
else ans += cr, q.push(sr);
}
}
return ans + sum;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
292 KB |
Output is correct |
30 |
Correct |
201 ms |
16368 KB |
Output is correct |
31 |
Correct |
225 ms |
16368 KB |
Output is correct |
32 |
Correct |
123 ms |
8320 KB |
Output is correct |
33 |
Correct |
165 ms |
16472 KB |
Output is correct |
34 |
Correct |
144 ms |
16420 KB |
Output is correct |
35 |
Correct |
144 ms |
16436 KB |
Output is correct |
36 |
Correct |
147 ms |
16408 KB |
Output is correct |
37 |
Correct |
140 ms |
16484 KB |
Output is correct |
38 |
Correct |
137 ms |
16472 KB |
Output is correct |
39 |
Correct |
177 ms |
16460 KB |
Output is correct |
40 |
Correct |
155 ms |
16600 KB |
Output is correct |
41 |
Correct |
179 ms |
18404 KB |
Output is correct |
42 |
Correct |
194 ms |
17924 KB |
Output is correct |
43 |
Correct |
175 ms |
16348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '5012' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
292 KB |
Output is correct |
30 |
Correct |
201 ms |
16368 KB |
Output is correct |
31 |
Correct |
225 ms |
16368 KB |
Output is correct |
32 |
Correct |
123 ms |
8320 KB |
Output is correct |
33 |
Correct |
165 ms |
16472 KB |
Output is correct |
34 |
Correct |
144 ms |
16420 KB |
Output is correct |
35 |
Correct |
144 ms |
16436 KB |
Output is correct |
36 |
Correct |
147 ms |
16408 KB |
Output is correct |
37 |
Correct |
140 ms |
16484 KB |
Output is correct |
38 |
Correct |
137 ms |
16472 KB |
Output is correct |
39 |
Correct |
177 ms |
16460 KB |
Output is correct |
40 |
Correct |
155 ms |
16600 KB |
Output is correct |
41 |
Correct |
179 ms |
18404 KB |
Output is correct |
42 |
Correct |
194 ms |
17924 KB |
Output is correct |
43 |
Correct |
175 ms |
16348 KB |
Output is correct |
44 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '5012' |
45 |
Halted |
0 ms |
0 KB |
- |