#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
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 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
544 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
2 |
Halted |
0 ms |
0 KB |
- |