2015ACM/ICPC亚洲区长春站-重现赛 1006 Almost Sorted Array

Problem Description:

We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.

We say an array is sorted if its elements are in non-decreasing order or non-increasing order. We say an array is almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now you are given an array a1,a2,…,an, is it almost sorted?

Input:

The first line contains an integer T indicating the total number of test cases. Each test case starts with an integer n in one line, then one line with n integers a1,a2,…,an.

1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.

Output:

For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).

Sample Input:

3

3

2 1 7

3

3 2 1

5

3 1 4 1 5

Sample Output:

YES

YES

NO

题意:给出一个序列,如果去掉一个元素,问剩下的序列是否是有序序列。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int N=1e5+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;

typedef long long LL;

int a[N], flag;

void Increase(int n)
{
    int i, num, idex;

    num = flag = 0;

    for (i = 2; i <= n; i++)
    {
        if (a[i] < a[i-1])
        {
            num++;
            idex = i; ///标记不满足元素的下标
        }
    }

    if (num == 0) flag = 1;

    if (num == 1)
    {
        if (idex == n || idex == 2 || a[idex-2] <= a[idex] || a[idex-1] <= a[idex+1])
            flag = 1; ///如果这个不满足元素的左边和右边还是非递减的,那么可以去掉该元素,或者该元素前的第二个元素和该元素是非递减的,可以去掉该元素之前的第一个元素
    }
}

void Decrease(int n)
{
    int idex, i, num;

    num = flag = 0;

    for (i = 2; i <= n; i++)
    {
        if (a[i] > a[i-1])
        {
            num++;
            idex = i;
        }
    }

    if (num == 0) flag = 1;

    if (num == 1)
    {
        if (idex == 2 || idex == n || a[idex-1] >= a[idex+1] || a[idex-2] >= a[idex])
            flag = 1;
    }
}

int main ()
{
    int T, i, n;

    scanf("%d", &T);

    while (T--)
    {
        scanf("%d", &n);
        for (i = 1; i <= n; i++)
            scanf("%d", &a[i]);

        Increase(n); ///先判断是否是非递减序列:找到不满足条件的元素下标,并统计个数,个数<=1时才满足条件
        if (flag == 0) Decrease(n); ///如果不是非递减序列再判断是否是非递增序列

        if (flag == 1) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}
时间: 2024-12-14 03:43:45

2015ACM/ICPC亚洲区长春站-重现赛 1006 Almost Sorted Array的相关文章

2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 447    Accepted Submission(s): 201 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort,

2015ACM/ICPC亚洲区长春站 G hdu 5534 Partial Tree

Partial Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 228    Accepted Submission(s): 138 Problem Description In mathematics, and more specifically in graph theory, a tree is an undirect

2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building

House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 123 Problem Description Have you ever played the video game Minecraft? This game has been one of t

2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 368    Accepted Submission(s): 202 Problem Description John is a manager of a CPU chip factory, the factory produces lots of chips

2015ACM/ICPC亚洲区长春站 B hdu 5528 Count a * b

Count a * b Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 211    Accepted Submission(s): 116 Problem Description Marry likes to count the number of ways to choose two non-negative integers a

2015ACM/ICPC亚洲区长春站 A hdu 5527 Too Rich

Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 245    Accepted Submission(s): 76 Problem Description You are a rich person, and you think your wallet is too heavy and full now. So yo

2015ACM/ICPC亚洲区长春站 G hdu 5533 Dancing Stars on Me

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 186    Accepted Submission(s): 124 Problem Description The sky was brushed clean by the wind and the stars were cold in a bl

2015ACM/ICPC亚洲区长春站 E hdu 5531 Rebuild

Rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 113 Problem Description Archaeologists find ruins of Ancient ACM Civilization, and they want to rebuild i

2014ACM/ICPC亚洲区北京站-重现赛 [B.Black And White] 涂色DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:在一个N * M的棋盘里涂色,要求i颜色要涂ci次.ci求和为N * M. 关键思想:从第一个点开始着色,一行一行涂,注意一旦找到答案后面就不必搜,当剩下个数为n时若有种颜色>n/2上取整就不必搜了(!). #include <iostream> #include <iomanip> #include <cstdio> #include <cst