#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;
const int N = 8 + 1;
bool cakeee(int* val);
bool special_case_zero();
bool special_case_one();
bool special_case_two();
void bfs_make_cakeee();
void pre_process();
void solve();
int n;
int a[N];
int limit;
int dist[90000000];
void bfs_make_cakeee() {
auto get_hash = [&] (int* val) {
int res = 0;
for(int i = 1; i <= limit; ++ i) {
res = res * 10 + val[i];
}
return res;
};
memset(dist, - 1, sizeof(dist));
for(int cnt = 1; cnt <= 8; ++ cnt) {
int tmp[N];
limit = cnt;
for(int j = 1; j <= limit; ++ j) tmp[j] = limit - j + 1;
int st = get_hash(tmp);
queue<int> q = {}; q.push(st);
dist[st] = 0;
while(q.empty() ^ 1) {
int u = q.front(); q.pop();
int tmp[N];
int val = u;
for(int i = limit; i >= 1; -- i) {
tmp[i] = val % 10;
val /= 10;
}
for(int i = 1; i <= limit; ++ i) {
reverse(tmp + i, tmp + 1 + limit);
int nxt = get_hash(tmp);
reverse(tmp + i, tmp + 1 + limit);
if(dist[nxt] != - 1) continue;
dist[nxt] = dist[u] + 1;
// cout << nxt << ' ' << dist[nxt] << ' ' << dist[u] << '\n';
q.push(nxt);
}
}
}
}
void solve() {
cin >> n;
for(int i = 1; i <= n; ++ i) cin >> a[i];
a[0] = 1000000000;
if(special_case_zero()) {
cout << 0;
return;
}
if(special_case_one()) {
cout << 1;
return;
}
if(special_case_two()) {
cout << 2;
return;
}
pre_process();
auto get_hash = [&] (int* val) {
int res = 0;
for(int i = 1; i <= n; ++ i) {
res = res * 10 + val[i];
}
return res;
};
cout << dist[get_hash(a)];
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// if (fopen("Cake.inp", "r")) {
// freopen("Cake.inp", "r", stdin);
// freopen("Cake.out", "w", stdout);
// }
bfs_make_cakeee();
int t; cin >> t;
while(t--) {
solve();
cout << '\n';
}
return 0;
}
bool special_case_zero() {
return cakeee(a);
}
bool special_case_one() {
int tmp[N];
for(int i = 1; i <= n; ++ i) {
for(int j = 0; j <= n; ++ j) tmp[j] = a[j];
reverse(tmp + i, tmp + n + 1);
if(cakeee(tmp)) return true;
}
return false;
}
bool special_case_two() {
int tmp[N];
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
for(int k = 0; k <= n; ++ k) tmp[k] = a[k];
reverse(tmp + i, tmp + n + 1);
reverse(tmp + j, tmp + n + 1);
if(cakeee(tmp)) return true;
}
}
return false;
}
bool cakeee(int* val) {
for(int i = 1; i <= n; ++ i)
if(val[i] > val[i - 1]) return false;
return true;
}
void pre_process() {
vector<int> Pos = {};
for(int i = 1; i <= n; ++ i) {
Pos.push_back(a[i]);
}
sort(Pos.begin(), Pos.end());
Pos.resize(unique(Pos.begin(), Pos.end()) - Pos.begin());
for(int i = 1; i <= n; ++ i) {
a[i] = lower_bound(Pos.begin(), Pos.end(), a[i]) - Pos.begin() + 1;
}
}
/// Code by vuavisao
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
97 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
94 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
103 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
107 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
103 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |