#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] = 1e8;
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);
if (dist > 1) continue;
ans = min(ans, dist + brute(p, i));
}
if (pos == -1){
int temp = p[s];
p[s] = n;
ans = min(ans, brute(p, s));
p[s] = temp;
}
// bool good = 0;
// if (p[0] == 0 && p[1] == 4 && p[2] == 2 && p[3] == 1) good = 1;
// if (p[0] == 0 && p[1] == 4 && p[2] == 2 && p[3] == 3 && s == 3) good = 1;
// if (good){
// cout<<"In\n";
// for (int i : p) cout << i << " ";
// cout<<"\n";
// }
if (pos != -1){
for (int i = 0; i < n; ++i)
if (p[i] != n){
int dist = abs(s - i);
if (dist > 1) continue;
int temp = p[i];
p[i] = pos;
int v = brute(p, i);
ans = min(ans, dist + v);
p[i] = temp;
} else {
p[i] = pos;
int dist = abs(i - s);
if (dist > 1) continue;
// if (good) for (int i : p) cout << i << " ";
// if (good) cout<<endl;
ans = min(ans, dist + brute(p, i));
p[i] = n;
}
}
// for (int i : p) cout << i << " "; cout << endl;
// if (good){
// for (int i : p) cout << i << " ";
// cout << endl;
// cout << s _ ans _ pos << " Out\n"<<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
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:98:1: warning: control reaches end of non-void function [-Wreturn-type]
98 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |