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.

≤T≤
≤n≤
≤ai≤
There are at most test cases with n>.
Output
For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
Sample Input

 
Sample Output
YES
YES
NO
 
Source
 

给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列。

对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出YES,不成立则输出NO。

由于持平不影响整体单调性,为了直观,我在以下把“不降”称为“递增/升序”,把“不增”称为“递减/降序”。

递增和递减是对称的,这里先考虑递增,递减改个符号和最值就好。

我们把为维护单调性而去掉的那个点称为“坏点”。由题目的要求,“可改造”可等价于“只存在一个坏点”。

对于“坏点”的判断,我们可以先找出是否只存在一组“逆序”。

对于“almosted sorted”递增序列,只存在一组逆序无非以下四种情况(这里先不考虑逆序在边界)。

现在考虑逆序在边界的情况。由于a[]数组元素下标是1~n,而此题1<=ai<=100000,那么对于递增序列,可把a[0]设为1、把a[n+1]设为100000作为首尾哨兵节点,一定不会破坏整体单调性;递减序列做对称的处理。这样边界也可以像中间一样处理。

由于三种情况满足一种即可,而第二种可以看作第三种和第四种的交集,故只需按照第三种和第四种的情况对a[]数组各进行一次遍历,满足一种即可输出YES。

对于坏点的处理,我们采用“当它不存在”的策略,所以首次遇到坏点,忽略它,再次遇到坏点,则此种情况不成立。

至于如何由“逆序”推出“坏点”,并实现几种情况的判断,我们遍历i:0~n,对于第一对逆序a[i]>a[i+1],我们可以:

先采取“左归”(第三种),即把a[i]当作坏点,判断a[i-1]和a[i+1]是否升序(若不升序则相当于构成了第二对逆序,出现第二个坏点);

若左归不成立,再采取“右归”(第四种),即把a[i+1]当坏点,同理判断a[i]和a[i+2]是否升序。

 
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1000000
int n;
int a[N];
int b[N];
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
int flag=;
b[]=;
b[n+]=inf;
int num1=;
for(int i=;i<n;i++){//左
if(b[i]<=b[i+]) continue;
num1++;
if(b[i-]<=b[i+] && num1<=) continue;
flag=;
break;
}
if(flag){
printf("YES\n");
continue;
}
num1=;
flag=;
for(int i=;i<n;i++){//右
if(b[i]<=b[i+]) continue;
num1++;
if(b[i]<=b[i+] && num1<=) continue;
flag=;
break;
}
if(flag){
printf("YES\n");
continue;
} a[]=inf;
a[n+]=;
int num2=;
flag=;
for(int i=;i<n;i++){
if(a[i]>=a[i+]) continue;
num2++;
if(a[i-]>=a[i+] && num2<=) continue;
flag=;
break;
}
if(flag){
printf("YES\n");
continue;
} num2=;
flag=;
for(int i=;i<n;i++){
if(a[i]>=a[i+])continue;
num2++;
if(a[i]>=a[i+] && num2<=) continue;
flag=;
break;
}
if(flag){
printf("YES\n");
continue;
}
printf("NO\n");
}
return ;
}
 

hdu 5532 Almost Sorted Array(模拟)的更多相关文章

  1. HDU 5532 Almost Sorted Array (最长非递减子序列)

    题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...

  2. HDU 5532——Almost Sorted Array——————【技巧】

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  3. hdu 5532 Almost Sorted Array (水题)

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  4. hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  5. HDU - 5532 Almost Sorted Array (最长非严格单调子序列)

    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...

  6. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  7. hdu 5532 Almost Sorted Array

    http://acm.hdu.edu.cn/showproblem.php?pid=5532  题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...

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

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  9. HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

    Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...

随机推荐

  1. 网站服务器、VPS和虚拟主机的联系与区别

     网站服务器是指在互联网数据中心中存放网站的服务器.主要用于网站的互联网中的发布.应用,是网络应用的基础硬件设施.简单的说服务器就是一台电脑,只是这台电脑因为要24 小时高速运行,所以配置要比一般的家 ...

  2. Unity 读取Excel

    游戏有大多数配置文件,比如玩家等级,游戏商店信息等等.通常情况下把这些放入excel中来读取 第一种解决方案: xlsx –> csv –> 改变成UTF-8 或者Unicode编码 –& ...

  3. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

  4. 使用ASIHttoRequest需要导入的framework

    需要导入如下framework libxml2.2.dylib libz.1.2.5.dylib MobileCoreServices.framework SystemConfiguration.fr ...

  5. 熬之滴水穿石:Spring--精简的J2EE(5)

                                   47--Spring的MVC 在Spring的框架中也存在MVC这样的模式,在Spring下有2个这样的控制器一个叫Controller, ...

  6. UVa 1394: And Then There Was One

    设置一个数组Winner记录经典约瑟夫问题中的剩余者即可递归解决该问题. 注: 约瑟夫问题:有编号为0~n-1的n个人,从0号开始报数1,2,3......报到k的杀死,然后从下一个人开始继续报数1, ...

  7. Android Project from Existing Code 生成 R 文件错误、失败等问题解决办法 - 持续更新

    Android Project from Existing Code 生成 R 文件错误.失败等问题解决办法 - 持续更新 git  上的项目,pull下来之后用Android Project fro ...

  8. 在Swift中使用遗留的C API

    Swift的类型系统的设计目的在于简化我们的生活,为此它强制用户遵守严格的代码规范来达到这一点.毫无疑问这是一件大好事,它鼓励程序员们编写 更好更正确的代码.然而,当Swift与历史遗留的代码库.特别 ...

  9. angular $location常用方法使用

    $location提供了一些常用的操作和获取地址栏里的地址的方法. <script type="text/javascript"> angular.module('ap ...

  10. css中bug记录

    1.margin塌陷,通俗叫法. 假如一个盒子box里边嵌套了两个盒子(记为box1,box2).box1的margin-top不会如预想的一样在box顶部撑开一个边距,而是以包含box的容器上边界为 ...