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.
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.
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.
4 1 2 2 4
4
8 11 12 1 2 13 14 3 4
2
4 7 6 5 4
1
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 th
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<iostream> using namespace std; int a[100]; int b[17],c[17],d[17]; int ff(int x,int y){ int t=1,sum=1; for(int i=x+1;i<=y;i++){ if(a[i]>=a[i-1]) sum++; else{ t=0; } } if(t==1) return sum; else return max(ff(x,(x+y-1)/2),ff((x+y+1)/2,y)); } int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } cout<<ff(1,n)<<endl; return 0; }
The input contains a single integer aa (1≤a≤991≤a≤99).
Output "YES" or "NO".
5
YES
13
NO
24
NO
46
YES
这个就是脑洞题了,题目信息很少,kanban数就是把数字转化为英语,
然后含有k,n,a,b的为no
没有这些字母的是yes;真是脑洞大开
The input contains a single integer aa (0≤a≤150≤a≤15).
Output a single integer.
3
13