This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
int col[1000010];
bool vis[1000010];
long long minimum_walk(std::vector<int> p, int s) {
long long ans = 0;
int n = p.size();
for(int i = 0; i < n-1; i++) {
int go_left = 0;
int go_right = 0;
for(int j = 0; j <= i; j++) {
if(p[j] > i) {
++go_left;
}
}
for(int j = i + 1; j < n; j++) {
if(p[j] < i+1) {
++go_right;
}
}
assert(go_left == go_right);
ans += go_left + go_right;
}
memset(col, -1, sizeof col);
int id = 0;
for(int i = 0; i < n; i++) {
if(col[i] != -1 || p[i] == i) continue;
int cur = i;
++id;
while(col[cur] == -1) {
col[cur] = id;
cur = p[cur];
}
}
int opt = 1e9;
for(int i = s; i < n; i++) {
memset(vis, false, sizeof vis);
int cnt = 0;
int add = abs(s - i);
for(int j = s; j <= i; j++) {
if(col[j] != -1) {
if(vis[col[j]] == false) ++cnt;
vis[col[j]] = true;
}
}
for(int j = s; j >= 0; j--) {
if(col[j] != -1) {
if(vis[col[j]] == false) ++cnt;
vis[col[j]] = true;
}
if(cnt == id) {
add += abs(s - j);
break;
}
}
if(cnt == id) {
opt = min(opt, 2 * add);
}
}
ans += opt;
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |