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 "books.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ld long double
#define fi first
#define se second
#define F first
#define S second
#define pii pair < int , int >
#define _ <<" "<<
#define TIME 1.0 * clock() / CLOCKS_PER_SEC
vector < int > a;
int S, n;
map < vector < int > , int > mp[30];
int brute(vector < int > p, int s){
if (mp[s].count(p)) return mp[s][p];
mp[s][p] = 1e7;
bool bad = 0;
for (int i = 0; i < n; ++i)
if (p[i] != i){
bad = 1;
break;
}
if (!bad){
mp[s][p] = abs(s - S);
return abs(s - S);
}
int pos = -1;
bool byl[n] = {};
for (int i = 0; i < n; ++i)
if (p[i] != n)
byl[p[i]] = 1;
for (int i = 0; i < n; ++i)
if (!byl[i]){
pos = i;
break;
}
int ans = 1e7;
for (int i = 0; i < n; ++i){
int dist = abs(i - s);
ans = min(ans, dist + brute(p, i));
}
if (pos == -1){
int temp = p[s];
p[s] = 0;
ans = min(ans, brute(p, s));
p[s] = temp;
}
if (pos != -1){
for (int i = 0; i < n; ++i)
if (p[i] != n){
int dist = abs(s - i);
int temp = p[i];
p[i] = pos;
ans = min(ans, dist + brute(p, i));
p[i] = temp;
} else {
p[i] = pos;
int dist = abs(i - s);
ans = min(ans, dist + brute(p, i));
p[i] = n;
}
}
// for (int i : p) cout << i << " "; cout << endl;
// if (p[0] == 0 && p[1] == 2 && p[2] == 3 && p[3] == 1){
// for (int i : p) cout << i << " ";
// cout << endl;
// cout << s _ ans _ pos << endl << endl;
// }
mp[s][p] = ans;
return ans;
}
long long minimum_walk(std::vector<int> p, int s) {
S = s;
a = p;
n = p.size();
if (n <= 4)
return brute(p, s);
}
Compilation message (stderr)
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:85:1: warning: control reaches end of non-void function [-Wreturn-type]
85 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |