# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
598242 | yanndev | Ancient Books (IOI17_books) | C++17 | 0 ms | 0 KiB |
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 <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, val[pos]);
mnAns = min(mnAns, recSolve(pos, vals, carry));
swap(carry, val[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);
}