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. buffer正确的拼接方式

    var chunks = []; var size = 0; res.on('data',function(chunk){ chunks.push(chunk); size+= chunk.lengt ...

  2. MySQL Got fatal error 1236原因和解决方法【转】

    本文来自:http://blog.itpub.net/22664653/viewspace-1714269/ 一 前言  MySQL 的主从复制作为一项高可用特性,用于将主库的数据同步到从库,在维护主 ...

  3. Python初识

    Python第一天   一.为什么学Python        作为一名linux运维工程师现在越来越感觉不好干了.没有地位,还待背黑锅,并且运维自动化发展的这么快,普通运维岗位的路也越来越窄(因为我 ...

  4. codevs 3290 华容道(SPFA+bfs)

    codevs 3290华容道 3290 华容道 2013年NOIP全国联赛提高组 时间限制: 1 s  空间限制: 128000 KB 题目描述 Description 小 B 最近迷上了华容道,可是 ...

  5. 【python】dict4ini和xmltodict模块用途

    dict4ini模块:可以读写配置文件 xmltodict模块:将xml和json互相转换  https://pypi.python.org/pypi/xmltodict

  6. bootstrap日期控件在火狐下的模态框中选择时间下拉菜单无效的解决办法

    今天收到程序组提交的一个兼容BUG,在火狐中使用模态框加载日期控件时选择时间下拉菜单没有效果(不能点击),而在谷歌中却是好的, 排错思路:1,在当前页面主层放置一个时间控件,测试通过 2,在ajax加 ...

  7. pod install 慢

    最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...

  8. AFNetworking(AFN)总结

    AFNetworking(AFN) ----主要做下载上传之用 //他是干啥的?发送请求,主要做下载上传之用 (苹果自带有获取服务器数据的方法NSURLConnection send,AFNetwor ...

  9. NYOJ之素数求和问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsoAAAKCCAIAAABH/2gWAAAgAElEQVR4nO3dPVLjStsG4G8T5CyEdF

  10. 三、jQuery--Ajax基础--Ajax全接触--jQuery中的AJAX

    用jQuery实现Ajax jQuery.ajax([settings]) type:类型,“POST”或“GET”,默认为“GET” url:发送请求的地址 data:是一个对象,连同请求发送到服务 ...