#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
long long minimum_walk(std::vector<int> p, int s) {
if (s != 0) return 0; // save time
// get each cycle length and min.
int n = p.size();
bool settled[n];
for (int i=0; i<n; i++) settled[i] = false;
ll allmax = 0;
ll currmin;
ll cyclesum = 0;
for (ll ii=0; ii<n; ii++) {
if (settled[ii]) continue;
// now, find this cycle.
ll i = p[ii];
//ll prev = ii;
currmin = i;
settled[ii] = true;
cyclesum += abs(ii-i);
currmin = min(ii, i);
while (i != ii) {
//cout << i << ' ' << p[i] << " -> ";
// do stuff within cycle
settled[i] = true;
cyclesum += abs(i-p[i]);
i = p[i];
currmin = min(currmin, i);
//cout << i << ' ' << p[i] << ": " << cyclesum << endl;
}
//cout << i << ' ' << ii << endl;
settled[ii] = true;
cyclesum += abs(ii-i);
currmin = min(currmin, i);
allmax = max(currmin, allmax);
}
//cout << "CYCLESUM: " << cyclesum << ", ALLMAX: " << allmax << endl;
return cyclesum + 2*allmax;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 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 |
344 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 |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
- |