#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll minimum_walk(vector<int> p, int s) {
int nums = p.size();
vector<int> cyc(nums, -1);
ll ans = 0;
int mn = s, mx = s;
vector<pair<int, int>> ranges;
for (int i = 0; i < nums; i++)
if (cyc[i] == -1){
int curr = i, cmn = i, cmx = i;
do{
ans += abs(curr - p[curr]);
cyc[curr] = i;
cmn = min(cmn, curr);
cmx = max(cmn, curr);
curr = p[curr];
} while (curr != i);
ranges.push_back({cmn, cmx});
if (p[i] != i){
mn = min(mn, cmn);
mx = max(mx, cmx);
}
}
sort(ranges.begin(), ranges.end());
int cmx = mn;
ll cnt = 0;
for (auto &[l, r] : ranges){
if (l < mn) continue;
if (l > mx) break;
if (l != r || r > cmx) cnt++;
cmx = max(cmx, r);
}
return ans + (cnt - 1) * 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3106' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |