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 ...
随机推荐
- cf472A Design Tutorial: Learn from Math
A. Design Tutorial: Learn from Math time limit per test 1 second memory limit per test 256 megabytes ...
- WordPress 实用SEO插件总结
原文地址:http://www.chinaz.com/web/2013/1122/328044.shtml#changyan_area WordPress- seo扩展插件众多,因此对于初学者来说会看 ...
- Impala 4、Impala JDBC
• 配置: – impala.driver=org.apache.hive.jdbc.HiveDriver – impala.url=jdbc:hive2://node2:21050/;auth=no ...
- No.26
"信是未见之事的实底,是所望之事的确据".
- Android 代码混淆及第三方jar包不被混淆
为了保护代码被反编译,android引入了混淆代码的概念 1.设置混淆 在工程下找到project.properties文件 在文件中加入proguard.config=${sdk.dir}/tool ...
- Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation
问题:Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation 1. Windows-& ...
- 教训:TOJ[4081] God Le wants to know the directory
以前的字符串题本来就弱..2年不写就更弱了.嗯.留作教训 God Le is the most talented ACMer in the TJU-ACM team. When he wants to ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- EF 事务处理 (InnoDB Engine的MySQL表也可以)
备忘 1. 亲测(可以嵌套使用) using (TransactionScope scope = new TransactionScope()) { //操作1 XXEntities.Current. ...
- Android混淆配置文件规范
#打开project.properties文件中的proguard.config. -optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassna ...