A. Thanos Sort
time limit per test 1 second
memory limit per test 256 megabytes
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 nn (1≤n≤161≤n≤16) — the size of the array. nn is guaranteed to be a power of 2.
The second line of input contains nn space-separated integers aiai (1≤ai≤1001≤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
outpu
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.
题目大意:
给你一个序列,你可以去除它的前半部分或后半部分,得到一个新序列,然后重复此过程,使得最后能得到一个最长的非递减序列。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a[20];
cin >> n;
for(int i=0; i<n; i++)
cin >> a[i];
for(int i=n; i>=1; i>>=1)
for(int j=0; j<n; j+=i){
int ok=1;
for(int k=j; k<i+j-1 && k<n-1; k++){
if(a[k]>a[k+1]){
ok=0;
break;
}
}
if(ok){
cout << i << endl;
return 0;
}
}
}