hdu 5532 Almost Sorted Array

http://acm.hdu.edu.cn/showproblem.php?pid=5532

 

 题目大意:
给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻的元素可以相等))
 
将序列里分成两种可能讨论,该序列除了一个元素之外要么递增要么递减,只需满足一个即可
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

const int N = 1e5 + 10;

int a[N], f, n;

void Increase()
{
    int num = 0, index;
    for(int i = 1 ; i < n ; i++)
    {
        if(a[i - 1] > a[i])
           {
               num++;
               index = i;
           }
    }
    if(num == 0)
        f = 1;
    else if(num == 1)
    {//这些情况的例子 3 1 1 2 3   /*******/   1 2 4 3 4    /*********/    1 1 2 1 1 4    /*****/  1 2 3 4 3
        if(index == 1 || a[index - 1] <= a[index + 1] || a[index - 2] <= a[index] || index == n - 1)
            f = 1;
    }//红色部分为index标记的元素
}

void Decrease()
{
    int index, num = 0;
    for(int i = 1 ; i < n ; i++)
    {
        if(a[i - 1] < a[i])
        {
            num++;
            index = i;
        }
    }
    if(num == 0)
        f = 1;
    else if(num == 1)
    {
        if(index == 1 || a[index - 1] >= a[index + 1] || a[index - 2] >= a[index] || index == n - 1)
            f = 1;
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        f = 0;
        scanf("%d", &n);
        for(int i = 0 ; i < n ; i++)
            scanf("%d", &a[i]);
        Increase();
        if(f == 1)//如果满足递增序列,即可得出结果
           {
               printf("YES
");
               continue;
           }
        Decrease();//否则判断其是否满足递减序列
        if(f == 1)
            printf("YES
");
        else
            printf("NO
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/qq2424260747/p/4930363.html