zoukankan      html  css  js  c++  java
  • Almost Sorted Array

    http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=646&pid=1006

    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    using namespace std;
    const int maxn=1e5+10;
    int ac[maxn],flag,n;
    int Increase()
    {
        int ans,index;
        ans=index=0;
        for(int i=2; i<=n; i++)
        {
            if(ac[i]<ac[i-1])
            {
                ans++;
                index=i;
            }
        }
        if(ans==0)
            flag=1;
        if(ans==1)
        {
            if(index==n || index==2 || ac[index-1] <= ac[index+1] || ac[index-2] <= ac[index] )
                flag=1;///如果这个不满足元素的左边和右边还是非递减的,那么可以去掉该元素,或者该元素前的第二个元素和该元素是非递减的,可以去掉该元素之前的第一个元素
        }
        if(flag==1)
            return 1;
        return 0;
    }
    int Decrease()
    {
        int ans,index;
        ans=index=0;
        for(int i=2; i<=n; i++)
        {
            if(ac[i]>ac[i-1])
            {
                ans++;
                index=i;
            }
        }
        if(ans==0)
            flag=1;
        if(ans==1)
        {
            if(index==2 || index==n || ac[index-1]>=ac[index+1] || ac[index-2]>=ac[index])
                flag=1;
        }
        if(flag)
            return 1;
        return 0;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            flag=0;
            scanf("%d",&n);
            for(int i=1; i<=n; i++)
                scanf("%d",&ac[i]);
            if(Decrease())//先判断是否是递增序列;找到不满足条件的元素下标,并统计个数,个数<=1时才满足条件
                printf("YES
    ");
            else if(Increase())//如果不是递增在判断其是否是递减序列
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }
    ps : 4 4 1 2 3
    4 4 2 7 3
    4 4 7 2 3
    5 1 2 6 4 5
    4 1 1 0 1 1
    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.

    1T2000
    2n105
    1ai105
    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
  • 相关阅读:
    Demo
    Demo
    z-yelir-~
    CSP考前总结
    NOIP刷题
    清北学堂
    qsing
    【csp模拟赛九】--dfs3
    【csp模拟赛九】--dfs2
    【csp模拟赛九】--dfs
  • 原文地址:https://www.cnblogs.com/cn-blog-cn/p/4929093.html
Copyright © 2011-2022 走看看