• F - Thanos Sort


     

     S 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 \le n \le 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 a_iai​ (1 \le a_i \le 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.

    Sample 1

    InputcopyOutputcopy
    4
    1 2 2 4
    
    4
    

    Sample 2

    InputcopyOutputcopy
    8
    11 12 1 2 13 14 3 4
    
    2
    

    Sample 3

    InputcopyOutputcopy
    4
    7 6 5 4
    
    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.

     递归思想,当然数据组数小,可以暴力,我的做法一组测试数据没过,但是能ac,可能学长的题出了问题,然后搜了网上的代码,和自己思路相同,但是有一点我出了问题,没想明白,就贴了两份代码

    1. #include <iostream>
    2. #include <algorithm>
    3. using namespace std;
    4. int n,s[20],ans=1;
    5. int dz(int ll,int rr){
    6. if(is_sorted(s+ll,s+rr+1)){
    7. return rr-ll+1;
    8. }
    9. int mid=(ll+rr)>>1;
    10. dz(ll,mid);
    11. dz(mid + 1,rr);
    12. }
    13. int main(){
    14. scanf("%d",&n);
    15. for(int i=1;i<=n;i++){
    16. scanf("%d",&s[i]);
    17. }
    18. printf("%d", dz(1,n));
    19. }

     

     正确代码

    1. #include <iostream>
    2. #include <algorithm>
    3. using namespace std;
    4. int n,s[20],ans=1;
    5. int dz(int ll,int rr){
    6. if(is_sorted(s+ll,s+rr+1)){
    7. return rr-ll+1;
    8. }
    9. int mid=(ll+rr)>>1;
    10. return max(dz(ll,mid),dz(mid + 1,rr));
    11. }
    12. int main(){
    13. scanf("%d",&n);
    14. for(int i=1;i<=n;i++){
    15. scanf("%d",&s[i]);
    16. }
    17. printf("%d", dz(1,n));
    18. }

  • 相关阅读:
    Jtti:新加坡云服务器怎么部署javaweb
    野火FPGA入门(3):简单组合逻辑
    NodeJS爬取墨刀上的设计图片
    SPDK block device及其编程的简单介绍
    spring security oauth2
    一文带你秒懂 字节序(byte order),比特序(bit order),位域(bit field)
    目标追踪学习经验总结
    Ansys(Maxwell、Simplorer)与Simulink联合仿真(二)直线电机
    基于OpenDaylight和OVSDB搭建VxLAN网络
    从云计算六大技术趋势,看亚马逊云科技的领先优势
  • 原文地址:https://blog.csdn.net/q619718/article/details/127774281