#include<bits/stdc++.h> usingnamespace std; constint maxn = 20; int n, arr[maxn], backup[maxn][maxn];
intf(){ int res = 0; for (int i = 0; i + 1 < n; ++i) { if (arr[i + 1] != arr[i] + 1) ++res; } // 向上取整 return (res + 3 - 1) / 3; }
boolcheck(){ for (int i = 0; i < n; ++i) { if (arr[i] != i + 1) returnfalse; } returntrue; }
booldfs(int depth, int mx){ // 最优性剪枝 // 当前开销+未来最小开销都大于阈值,剪枝 if (depth + f() > mx) returnfalse; if (check()) returntrue; for (int l = 0; l < n; ++l) { for (int r = l; r < n; ++r) { // 等效性剪枝,只会朝后交换 // 先前序列向后交换,和当前序列向前交换是等价的 for (int k = r + 1; k < n; ++k) { memcpy(backup[depth], arr, sizeof(arr)); int a, b; for (b = r + 1, a = l; b <= k; ++a, ++b) arr[a] = backup[depth][b]; for (b = l; b <= r; ++a, ++b) arr[a] = backup[depth][b]; if (dfs(depth + 1, mx)) returntrue; memcpy(arr, backup[depth], sizeof(arr)); } } } returnfalse; }
intmain(){ int t; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; ++i) cin >> arr[i]; int depth = 0; while (depth < 5 && !dfs(0, depth)) ++depth; if (depth >= 5) cout << "5 or more" << endl; else cout << depth << endl; } return0; }