版权声明:版权声明,使用请说明出处 https://blog.csdn.net/qq_41835683/article/details/88962363
看代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2e5+5;
int s[maxn];
signed main(){
/*
if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.
Return the maximal length of a sorted array you can obtain using Thanos sort.
*/
int n;cin>>n;
for(int i=0;i<n;i++)cin>>s[i];
int ans=0;
for(int i=1;i<=n;i<<=1){//for Every len
for(int j=0;j<n;j+=i){
if(is_sorted(s+j,s+j+i))ans=i;//sort(s+0,s+n) 0 n-1
}
}
cout<<ans<<endl;
return 0;
}
递归:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2e5+5;
int s[maxn];
int maxlen(int l,int r){
if(is_sorted(s+l,s+r+1))return r-l+1;
return max(maxlen(l,(l+r)/2),maxlen((l+r)/2+1,r));
}
signed main(){
/*
if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.
Return the maximal length of a sorted array you can obtain using Thanos sort.
*/
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>s[i];
int ans=maxlen(1,n);
cout<<ans<<endl;
return 0;
}