hdu 5532 Almost Sorted Array(模拟)
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?
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>.
For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
YES
YES
NO
给出一个序列(长度>=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(模拟)的更多相关文章
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- HDU 5532——Almost Sorted Array——————【技巧】
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- hdu 5532 Almost Sorted Array (水题)
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU - 5532 Almost Sorted Array (最长非严格单调子序列)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...
- 【HDU 5532 Almost Sorted Array】水题,模拟
给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...
- hdu 5532 Almost Sorted Array
http://acm.hdu.edu.cn/showproblem.php?pid=5532 题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...
- 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 ...
- HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array
Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...
随机推荐
- myeclipse修改tomcat端口
myeclipse自带的tomcat可以直接在myeclipse下面进行配置,但是如果是配置的tomcat 只能自己手动修改了
- svo的一些博客解析
记录一边学习 白巧克力: svo代码笔记 http://blog.csdn.net/heyijia0327/article/details/51083398 卢彦斌:svo原理解析 东北大学孙志明:s ...
- Eclipse选中变量名,相同变量都变色显示
Eclipse选中变量名,相同变量都变色显示 java文件的设置"Window"-"preferences"-"Java"-"Ed ...
- oninput,onpropertychange,onchange的使用方法和差别
1.前言 因为工作须要,需实现一个相似于微博输入框的功能,在用户动态输入文字的时候,改动提示“您还能够输入XX字”.例如以下图所看到的: 因此,略微研究了一下oninput,onpropertycha ...
- ASP.NET 开发框架汇总
先简单记录一下,以后慢慢添加 1.ASP.NET Aries 2.ASP.NET DevExpress
- ScriptManager需要用到的JS
<script type="text/javascript"> Sys.Application.add_load(function() { var form = Sys ...
- SQL日期格式转换(经常用又经常忘记的东西)转载自http://www.cnblogs.com/wangyuelang0526/archive/2012/06/06/2538224.html
Select CONVERT(varchar(100), GETDATE(), 8):14:53:14Select CONVERT(varchar(100), GETDATE(), 9): 06 6 ...
- PHP学习笔记五【方法】
<?php $num1=34; $num2=90; $oper="+"; $res=0; switch($oper) { case "+": $res=$ ...
- HTTP状态码搜集
一.1xx消息 这一类型的状态码,代表请求已经接受,需要继续处理. 这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束. 由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...
- iOS 使用NJKWebViewProgress做webview进度条
NJKWebViewProgress地址:https://github.com/ninjinkun/NJKWebViewProgress 导入头文件 #import "NJKWebViewP ...