#include "stdio.h"
int max_already = 0;
int result[1000];
int tmp_result[1000];
int search_count = 0;
int min(int n, int lb[]) {
int i;
int flag = 0;
int t;
int ret = 0;
for (i=0; i<n;i++) {
if (i == 0) {
ret++;
continue;
}
t = lb[i] - lb[i-1];
if (flag == 0) {
if (t == 1 || t == -1) {
flag = t;
} else {
ret++;
}
} else if (flag == 1) {
if (t == 1 ) {
} else {
ret++;
flag = 0;
}
} else if (flag == -1) {
if (t == -1 ) {
} else {
ret++;
flag = 0;
}
}
}
return ret - 1;
}
int max(int n, int lb[]) {
return 2 * (n-1);
}
int is_sorted(int n, int lb[]) {
return min(n, lb) == 0 ? 1 : 0;
}
void revert(int n, int lb[], int f, int t) {
int i, j;
int value;
for (i=f, j=t; i<j; i++, j--) {
value = lb[i];
lb[i] = lb[j];
lb[j] = value;
}
}
void search(int n, int already, int lb[]) {
int estimate, i, j;
estimate = min(n, lb);
search_count++;
if (estimate + already > max_already) {
return;
}
if (is_sorted(n, lb)) {
if (n > 1 && lb[1] < lb[0]) {
tmp_result[already] = n-1;
already++;
}
if (already < max_already) {
max_already = already;
//printf("find another %d\n", max_already);
for(i=0; i<max_already;i++) {
result[i] = tmp_result[i];
}
}
return;
}
for (i=n-1;i>=0;i--) {
revert(n, lb,0, i);
tmp_result[already] = i;
search(n, already+1, lb);
revert(n, lb, 0, i);
}
}
void main() {
int n;
int lb[1000];
int i;
scanf("%d", &n);
for (i=0;i<n;i++) {
scanf("%d", &lb[i]);
}
max_already = max(n, lb);
//printf("run...\n");
search(n, 0, lb);
printf("searrch %d times..result: at least %d times \n", search_count,max_already);
for(i=0; i<max_already;i++) {
printf("%d\n", result[i]);
}
}
|