#include "books.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using ii = pair<int, int>;
struct UF {
vi e;
UF(int N) : e(N, -1) {}
int repr(int u) {
while(e[u] >= 0) u = e[u];
return u;
}
int join(int u, int v) {
u = repr(u);
v = repr(v);
if(u == v) return 0;
if(e[u] >= e[v]) swap(u, v);
e[u] += e[v];
e[v] = u;
return 1;
}
};
long long minimum_walk(vi p, int s) {
int n = p.size();
vi ip(n);
for(int i = 0; i < n; ++i)
ip[p[i]] = i;
using ll = long long;
ll re = 0, nrcic = 0;
vi viz(n, 0), are_st(n, 0);
for(int i = 0; i < n; ++i) {
if(!viz[i]) {
int u = i, mi = i;
while(!viz[u]) {
viz[u] = 1;
u = p[u];
mi = min(mi, u);
}
u = i;
do {
if(u > mi) are_st[u] = 1;
u = p[u];
} while(u != i);
++nrcic;
}
}
vll dp(n, 0);
for(int i = 1; i < n; ++i) {
if(are_st[i]) dp[i] = dp[i - 1];
else dp[i] = 2 + dp[i - 1];
}
for(int i = 0; i < n; ++i) {
re += abs(p[i] - i);
}
for(int i = n - 1; i >= 0; --i) {
if(p[i] != i) {
re += dp[i];
break;
}
}
return re;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4186' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |