#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll minimum_walk(vi p, int s) {
int n=p.size();
vi used(n);
vii r; ll ans=0;
FOR(i,0,n){
if (used[i] || p[i]==i) continue;
int cur=i; int mx=0;
while (p[cur] != i){
ans += abs(p[cur]-cur);
mx=max(mx,cur); used[cur]=1; cur=p[cur];
}
ans += abs(i-cur); mx=max(mx,cur); used[cur]=1;
r.pb({i,mx});
}
if (r.empty()) return ans;
int len=r.size(); bool sol=true; if (p[s]!=s) sol=false;
if (r[0].fi>s) {ans += 2*(r[0].fi-s); sol=false;}
int mx=r[0].fi;
FOR(i,0,len){
if (r[i].fi > mx) {
ans += 2*(r[i].fi-mx);
if (r[i].fi > s && s > mx) sol=false;
}
mx=max(mx,r[i].se);
}
if (s>mx) {ans += 2*(s-mx); sol=false;}
//if (!sol) return ans;
int L=s,R=s;
while (p[L]==L) L--;
while (p[R]==R) R++;
ans += 2*min(s-L,R-s);
return ans;
}
Compilation message
books.cpp: In function 'll minimum_walk(vi, int)':
books.cpp:32:25: warning: variable 'sol' set but not used [-Wunused-but-set-variable]
32 | int len=r.size(); bool sol=true; if (p[s]!=s) sol=false;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |