#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i , j , k) for(int i = j; i < (int)k; i++)
typedef long long ll;
const int N = 1e6 + 10;
int par[N];
ll le[N], ri[N], arr[N];
int getPar(int v) {
return v == par[v] ? v : par[v] = getPar(par[v]);
}
inline bool merge(int u , int v) {
if ((u = getPar(u)) == (v = getPar(v)))
return false;
par[v] = u;
return true;
}
inline void smax(ll &a, ll b) {
if (a < b) a = b;
}
inline void smin(ll &a, ll b) {
if (b < a) a = b;
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
iota(par , par + n , 0);
ll res = 0;
rep(i , 0 , n) {
res += abs(p[i] - i);
merge(p[i] , i);
}
fill(le , le + n , -1e9);
fill(ri , ri + n , 1e9);
rep(i , 0 , n) {
if (i < s)
smax(le[getPar(i)] , i);
else if (i > s)
smin(ri[getPar(i)] , i);
}
ll Rlimit = s , Llimit = s;
fill(arr ,arr + n , s);
rep(i , 0 , n) {
if (par[i] == i && i != getPar(s)) {
if (le[i] < 0) smax(Rlimit , ri[i]);
if (ri[i] >= n) smin(Llimit , le[i]);
if (le[i] >= 0)
arr[le[i]] = ri[i];
}
}
ll ans = 1e18;
rep(i , 0 , s + 1) {
if (i <= Llimit)
smin(ans , res + (Rlimit - i) * 2);
smax(Rlimit , arr[i]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4734' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |