1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 #include <cstring> 5 #include <cstdlib> 6 #include <ctime> 7 #include <set> 8 #include <map> 9 using namespace std; 10 11 const int maxn=1100; 12 13 struct point 14 { 15 int x, y; 16 }; 17 18 int n; 19 int sum; 20 int a[maxn], des[maxn]; 21 point ans[10]; 22 23 void init() 24 { 25 scanf("%d", &n); 26 a[0]=0; a[n+1]=n+1; 27 for (int i=1; i<=n; ++i) scanf("%d", &a[i]); 28 } 29 void rev(int le, int ri) 30 { 31 for (int i=le; i<=(ri+le)/2; ++i) 32 swap(a[i], a[ri-(i-le)]); 33 } 34 bool check() 35 { 36 for (int i=1; i<=n; ++i) 37 if (a[i]!=i) return false; 38 return true; 39 } 40 void print(int step) 41 { 42 printf("%d\n", step); 43 for (int i=step; i>0; --i) 44 printf("%d %d\n", ans[i].x, ans[i].y); 45 exit(0); 46 } 47 void dfs(int k) 48 { 49 if (check()) print(k-1); 50 if (k>3) return; 51 for (int i=1; i<n; ++i) 52 for (int j=i+1; j<=n; ++j) 53 if (abs(a[i-1]-a[j])==1 || abs(a[i]-a[j+1])==1) 54 { 55 ans[k].x=i; 56 ans[k].y=j; 57 rev(i, j); 58 dfs(k+1); 59 rev(i, j); 60 } 61 } 62 int main() 63 { 64 freopen("input.txt", "r", stdin); 65 freopen("output.txt", "w", stdout); 66 init(); 67 dfs(1); 68 return 0; 69 }
时间: 2024-10-21 15:26:13