#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) continue;
if(vis[col[j]] == false) ++cnt;
vis[col[j]] = true;
}
if(cnt == id) {
opt = min(opt, 2 * add);
continue;
}
for(int j = s-1; j >= 0; j--) {
if(col[j] == -1) continue;
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 |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5344 KB |
Output is correct |
3 |
Correct |
7 ms |
5344 KB |
Output is correct |
4 |
Correct |
7 ms |
5344 KB |
Output is correct |
5 |
Correct |
7 ms |
5344 KB |
Output is correct |
6 |
Incorrect |
8 ms |
5396 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5344 KB |
Output is correct |
3 |
Correct |
7 ms |
5344 KB |
Output is correct |
4 |
Correct |
7 ms |
5344 KB |
Output is correct |
5 |
Correct |
7 ms |
5344 KB |
Output is correct |
6 |
Incorrect |
8 ms |
5396 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5344 KB |
Output is correct |
3 |
Correct |
7 ms |
5344 KB |
Output is correct |
4 |
Correct |
7 ms |
5344 KB |
Output is correct |
5 |
Correct |
7 ms |
5344 KB |
Output is correct |
6 |
Incorrect |
8 ms |
5396 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
5400 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4724' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5344 KB |
Output is correct |
3 |
Correct |
7 ms |
5344 KB |
Output is correct |
4 |
Correct |
7 ms |
5344 KB |
Output is correct |
5 |
Correct |
7 ms |
5344 KB |
Output is correct |
6 |
Incorrect |
8 ms |
5396 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |