A. Thanos Sort
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
翻译:给一个序列,如果这个序列是从小到大排序的,就输出它的长度,否则就选择去掉它的前半段或者后半段,直到得到一个从小到大排序的序列,输出可能的到的最大值
PS:额,cf的第一道题,看了半天没看懂题意,最后在网上搜的。做法就是递归就行了,但is_sort函数在devc++上用不了,只能用cb,不知道是不是我版本太低的原因
#include<bits/stdc++.h>
using namespace std;
int n,a[105];
int fun(int i,int j){
if(is_sorted(a+i,a+j))
return j-i;
return max(fun(i,(i+j)/2),fun((i+j)/2,j));
}
int main(){
while(cin>>n){
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<fun(0,n)<<endl;
}
return 0;
}