#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
map<vector<int>,int> dist;
void bfs(vector<int> s) {
queue<vector<int>> q;
dist[s] = 0;
q.push(s);
while (q.size()) {
auto u = q.front(); q.pop();
for (int i = 0; i < u.size(); i++) {
auto v = u;
reverse(v.begin() + i, v.end());
if (dist.find(v) == dist.end()) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
}
void precompute() {
for (int i = 1; i <= 8; i++) {
vector<int> s(i);
iota(s.rbegin(), s.rend(), 0);
bfs(s);
}
}
void solve() {
int n; cin >> n;
vector<ar<int,2>> a(n);
for (int i = 0; i < n; i++) {
int x; cin >> x;
a[i] = {x, i};
}
sort(a.begin(), a.end());
vector<int> b(n);
for (int i = 0; i < n; i++) b[a[i][1]] = i;
cout << dist[b] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
precompute();
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
Compilation message
pancake.cpp: In function 'void bfs(std::vector<int>)':
pancake.cpp:21:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < u.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
6784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
7020 KB |
Output is correct |