Dora and Search

原题链接

题解

对于原序列而言,如果第一个元素是最大值或最小值,那么l肯定不能落在这,由于r也不可能落在这,所以相当于这个元素被剔除了
那么对于区间 \([1,n]\) 的研究就等价于对 \([2,n]\) 的研究
由此可以推出之后的做法

code

#include<bits/stdc++.h>
using namespace std;
int a[200006]={0};
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)  cin>>a[i];

        int l=1,r=n,high=n,low=1,x=0,y=0;
        while(l<r)
        {
            if(a[l]==low)
            {
                l++;
                low++;
            }
            else if(a[l]==high)
            {
                l++;
                high--;
            }
            else x=1;
            if(a[r]==high)
            {
                r--;
                high--;
            }
            else if(a[r]==low)
            {
                r--;
                low++;
            }
            else y=1;
            if(x&&y)break;
            else x=y=0;
        }
        if(l<r)printf("%d %d\n",l,r);
        else puts("-1");
    }
    return 0;
}

posted @ 2024-02-24 01:46  纯粹的  阅读(25)  评论(0)    收藏  举报