#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<int,int>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "books.h"
long long minimum_walk(std::vector<int> p, int s) {
ll ans = 0;
ll n = p.size();
bool v[n] = {0};
ll dist = 0;
ll l[n],r[n];
vector<ii>rang;
ll L = -1,R;
f(i,0,n){
if(p[i] != i && !v[i]){
ll last = i;
ll st = i;
ll pos = p[i];
v[i] = 1;
l[st] = i,r[st] = pos;
while(pos != st){
l[st] = min(l[st],pos);
r[st] = max(r[st],pos);
v[pos] = 1;
ans += abs(pos - last);
last = pos;
pos = p[pos];
}
ans += abs(pos - last);
l[st] = min(l[st],pos);
r[st] = max(r[st],pos);
rang.pb(ii(l[st],r[st]));
if(v[s] == 1 && L == -1){
L = l[st];
R = r[st];
}
}
}
if(rang.empty()){
return ans;
}
vector<ii>left,right;
ll LL = L,RR = R;
for(auto x:rang){
if(LL <= x.F && x.F <= RR){
L = min(L,x.F);
R = max(R,x.S);
}
else if(LL <= x.S && x.S <= RR){
L = min(L,x.F);
R = max(R,x.S);
}
else if(x.F <= LL && RR <= x.S){
L = min(L,x.F);
R = max(R,x.S);
}
else if(x.F < L){
left.pb(ii(x.S,x.F));
}
else{
assert(x.F > R);
right.pb(ii(x.F,x.S));
}
}
sort(all(left));
sort(all(right));
reverse(all(right));
while(!left.empty()){
dist += L - left.back().S;
L = left.back().F;
while(!left.empty() && left.back().S >= L){
left.pop_back();
}
}
while(!right.empty()){
dist += right.back().F - R;
R = right.back().S;
while(!right.empty() && right.back().F <= R){
right.pop_back();
}
}
ans += 2 * dist;
return ans;
}
/*
int main() {
int n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
*/
/*
4 0
0 2 3 1
*/
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:73:27: warning: 'R' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | else if(x.F <= LL && RR <= x.S){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |