#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int NOT_VIS = 0;
const int CUR_VIS = 1;
const int VIS = 2;
map<pair<vector<int>, pair<int, int>>, pair<int, ll>> vis {};
inline ll recSolve(int pos, vector<int> vals, int carry) {
// move out of bounds
if (pos >= (int)vals.size() || pos < 0)
return 1e9;
// don't allow to cycle
if (vis[{vals, {pos, carry}}].first == CUR_VIS)
return 1e9;
// if already visited, return answer
if (vis[{vals, {pos, carry}}].first == VIS)
return vis[{vals, {pos, carry}}].second;
ll mnAns = 1e9;
bool ok = true;
for (int i = 0; i < (int)vals.size(); i++)
if (vals[i] != i)
ok = false;
// if we have correct tables
if (ok) {
//printf("food bruh %d %d\n", pos);
vis[{vals, {pos, carry}}] = {VIS, pos};
return pos;
}
// mark as visiting
vis[{vals, {pos, carry}}] = {CUR_VIS, 1e9};
// jump to the right
mnAns = min(mnAns, 1 + recSolve(pos + 1, vals, carry));
// jump to the left
mnAns = min(mnAns, 1 + recSolve(pos - 1, vals, carry));
// change carried book
swap(carry, vals[pos]);
mnAns = min(mnAns, recSolve(pos, vals, carry));
swap(carry, vals[pos]);
vis[{vals, {pos, carry}}] = {VIS, mnAns};
return mnAns;
}
ll minimum_walk(vector<int> p, int s) {
int n = (int)p.size();
return recSolve(0, p, -1);
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:52:9: warning: unused variable 'n' [-Wunused-variable]
52 | int n = (int)p.size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
3rd lines differ - on the 1st token, expected: '6', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
3rd lines differ - on the 1st token, expected: '6', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
3rd lines differ - on the 1st token, expected: '6', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2084 ms |
223992 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
3rd lines differ - on the 1st token, expected: '6', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |