I - Inverted Deck

题目链接

这个题是个思维题,题目重点的地方就是找非递增的左右边界,由于可能会有前后相同的,所以定义一个t控制相同元素的第一个位置。左右边界正常记即可,最后再判断一下是否只有一段递减序列。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 10010;

int a[1010];
int main()
{
    
    
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
    {
    
    
        scanf("%d", &a[i]);
    }
    int l = 0, r;
    for(int i = 1, t; i <= n; i ++)
    {
    
    
        if(!l && a[i] < a[i - 1])
        l = t;
        if(l && a[i] > a[i - 1])
        {
    
    
            r = i;
            break;
        }
        if(a[i] != a[i - 1]) t = i;
    }
    if(!l) l = 1, r = 2;
    reverse(a + l, a + r);
    for(int i = 1; i <= n; i ++)
    {
    
    
        if(a[i] < a[i - 1]){
    
    
        cout << "impossible\n";
        return 0;
        }
    }
    printf("%d %d\n", l, r - 1);
}

猜你喜欢

转载自blog.csdn.net/qq_47783181/article/details/111145177
今日推荐