#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
ll minimum_walk(vector<int> p, int s){
int n = p.size();
ll ans = 0;
vector<int> C(n, -1), sz;
int k = 0;
for(int i = 0; i < n; i++){
if(C[i] != -1) continue;
int x = p[i];
C[i] = k;
sz.pb(1);
ans+=abs(i-x);
while(x != i){
C[x] = k;
sz.back()++;
ans+=abs(p[x]-x);
x = p[x];
}
k++;
}
vector<set<int>> sth(k);
for(int i = 0; i < n; i++) sth[C[i]].insert(i);
vector<ll> mn(k, 1e9);
for(int i = 0; i < n; i++){
if(C[i] == 0 || sz[C[i]] < 2) continue;
for(int j = 0; j < k; j++){
if(C[i] == j) continue;
auto it = sth[j].upper_bound(i);
if(it != sth[j].begin() && it != sth[j].end()) mn[C[i]] = min(mn[C[i]], ll(i-*prev(it)));
else if(it != sth[j].begin()) mn[C[i]] = min(mn[C[i]], ll(i-*prev(it))*2);
}
}
for(int i = 1; i < k; i++) if(sz[i] > 1) ans+=mn[i];
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '9' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '9' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '9' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3069' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '9' |
7 |
Halted |
0 ms |
0 KB |
- |