Thanos sort is a supervillain sorting algorithm, which works as follows: if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.
Given an input array, what is the size of the longest sorted array you can obtain from it using Thanos sort?
*Infinity Gauntlet required.
Input
The first line of input contains a single number n (1≤n≤16) — the size of the array. n is guaranteed to be a power of 2.
The second line of input contains n space-separated integers ai (1≤ai≤100) — the elements of the array.
Output
Return the maximal length of a sorted array you can obtain using Thanos sort. The elements of the array have to be sorted in non-decreasing order.
Examples
input
4
1 2 2 4
output
4
input
8
11 12 1 2 13 14 3 4
output
2
input
4
7 6 5 4
output
1
Note
In the first example the array is already sorted, so no finger snaps are required.
In the second example the array actually has a subarray of 4 sorted elements, but you can not remove elements from different sides of the array in one finger snap. Each time you have to remove either the whole first half or the whole second half, so you’ll have to snap your fingers twice to get to a 2-element sorted array.
In the third example the array is sorted in decreasing order, so you can only save one element from the ultimate destruction.
题目大意: 给出一个数n(2的次幂),下一行给出n个数,在这n个数中,如果不是非递减顺序,则选择前面一半或后面一半进行删除操作,重复此操作至所得到的序列为非递减的,求该序列的最大长度。
思路: 由于n只取1,2,4,8,16这几个数,对每种情况进行枚举区间。
#include <iostream>
using namespace std;
int main() {
int n, a[20], flag = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int num = a[0];
for(int i = 1; i < n; i++) { // 判断一开始是否全部非递减顺序
if(a[i] < num) {
flag = 1;
break;
}
num = a[i];
}
if(!flag) printf("%d", n); // 一开始就是非递减顺序,直接输出n
else {
switch(n) {
case 2: // n为2直接输出1
printf("1\n");
break;
case 4: // n为4枚举前两个和后两个
if(a[1] >= a[0] || (a[3] >= a[2])) printf("2\n");
else printf("1\n");
break;
case 8: // n为8枚举前四个,后四个,前四个中的前两个,后两个,后四个中的前两个和后两个
if((a[3] >= a[2] && a[2] >= a[1] && a[1] >= a[0]) || (a[7] >= a[6] && a[6] >= a[5] && a[5] >= a[4])) printf("4\n");
else if((a[1] >= a[0]) || (a[3] >= a[2]) || (a[5] >= a[4]) || (a[7] >= a[6])) printf("2\n");
else printf("1\n");
break;
case 16: // n为16枚举前八个,后八个,前八个中的前四个,后四个,前四个中的前两个和后两个,后四个中的前两个和后两个,后八个中的前四个,后四个,前四个中的前两个和后两个,后四个中的前两个和后两个
if((a[7] >= a[6] && a[6] >= a[5] && a[5] >= a[4] && a[4] >= a[3] && a[3] >= a[2] && a[2] >= a[1] && a[1] >= a[0]) || (a[15] >= a[14] && a[14] >= a[13] && a[13] >= a[12] && a[12] >= a[11] && a[11] >= a[10] && a[10] >= a[9] && a[9] >= a[8])) printf("8\n");
else if((a[3] >= a[2] && a[2] >= a[1] && a[1] >= a[0]) || (a[7] >= a[6] && a[6] >= a[5] && a[5] >= a[4]) || (a[11] >= a[10] && a[10] >= a[9] && a[9] >= a[8]) || (a[15] >= a[14] && a[14] >= a[13] && a[13] >= a[12])) printf("4\n");
else if((a[1] >= a[0]) || (a[3] >= a[2]) || (a[5] >= a[4]) || (a[7] >= a[6]) || (a[9] >= a[8]) || (a[11] >= a[10]) || (a[13] >= a[12]) || (a[15] >= a[14])) printf("2\n");
else printf("1\n");
break;
}
}
return 0;
}