Submission #1184483

#TimeUsernameProblemLanguageResultExecution timeMemory
1184483alterioAncient Books (IOI17_books)C++20
12 / 100
2106 ms1114112 KiB
#include "books.h"
#include <bits/stdc++.h>

using namespace std;

map<pair<vector<int>, pair<int, int>>, int> memo;

int ans, START;

void solve(vector<int> p, int spot, int hold, int moves) {
	if (moves >= ans) return;
	if (memo.count({p, {spot, hold}}) && memo[{p, {spot, hold}}] <= moves) return;
	memo[{p, {spot, hold}}] = moves;
	bool ok = 1;
	for (int i = 0; i < p.size(); i++) if (p[i] != i) ok = 0;
	if (ok) {
		ans = min(ans, moves + abs(START - spot));
		return;
	}
	bool done = 0;
	if (p[spot] != spot) swap(p[spot], hold), done = 1;
	solve(p, spot, hold, moves);
	if (done) swap(p[spot], hold);
	if (spot - 1 >= 0) solve(p, spot - 1, hold, moves + 1);
	if (spot + 1 < p.size()) solve(p, spot + 1, hold, moves + 1);
}

long long minimum_walk(vector<int> p, int s) {
	START = s;
	ans = 1e9;
	solve(p, s, -1, 0);
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...