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

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

Almost Sorted Array的更多相关文章

  1. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  5. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  6. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  7. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  10. Remove Duplicates from Sorted Array II

    题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

随机推荐

  1. Java for LeetCode 230 Kth Smallest Element in a BST

    解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...

  2. Django~Views

    In Django, web pages and other content are delivered by views. To get from a URL to a view, Django u ...

  3. ubuntu vsftp 安装

    1.输入sudo apt-get install vsftpd 回车 这样就安装完毕了,然后去建立一个ftp的帐号,我这里使用的是ftp. 2.输入useradd ftp 回车 输入密码 回车 这样帐 ...

  4. js 中 toString( ) 和valueOf( )

    1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...

  5. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

  7. iOS block 的底层实现

    其实swift 的闭包跟 OC的block 是一样一样的,学会了block,你swift里边的闭包就会无师自通. 参考:http://www.jianshu.com/p/e23078c11518 ht ...

  8. 25个增强iOS应用程序性能的提示和技巧(初级篇)

    25个增强iOS应用程序性能的提示和技巧(初级篇) 标签: ios内存管理性能优化 2013-12-13 10:53 916人阅读 评论(0) 收藏 举报  分类: IPhone开发高级系列(34)  ...

  9. [Android Pro] svn实例

    referece : http://www.cnblogs.com/cnblogsfans/archive/2010/03/21/1690891.html 签出 svn checkout URL pa ...

  10. [Android Pro] Android保存图片到系统图库

    http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...