#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> v,c,vc;
int search(int n, vector<int> &p) {
if(v[n] || p[n] == n) return 0;
v[n] = 1;
vc[c[n]] = 1;
int r = 0;
if(n > 0 && c[n-1] != c[n] && !vc[c[n-1]]) {
int k = search(n-1, p);
if(k) r += k + 2;
}
if(n+1 < p.size() && c[n+1] != c[n] && !vc[c[n+1]]) {
int k = search(n+1, p);
if(k) r += k + 2;
}
r += abs(n-p[n]);
r += search(p[n], p);
return r;
}
ll minimum_walk(vector<int> p, int s) {
int n = p.size();
c.resize(n,-1);
v.resize(n,0);
vc.resize(n,0);
for(int i = 0; i < n; i++) {
if(c[i] == -1) {
c[i] = i;
int k = p[i];
while(k != i) {
c[k] = i;
k = p[k];
}
}
}
int r = search(s, p);
for(int i = 1; i < n; i++) {
if(s >= i) {
int k = search(s-i, p);
if(k) r += 2*i + k;
}
if(s+i < n) {
int k = search(s+i, p);
if(k) r += 2*i + k;
}
}
return r;
}
Compilation message
books.cpp: In function 'int search(int, std::vector<int>&)':
books.cpp:18:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | if(n+1 < p.size() && c[n+1] != c[n] && !vc[c[n+1]]) {
| ~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 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 |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 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 |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 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 |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '82012' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |