Almost Sorted Array

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 8541    Accepted Submission(s): 1982

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了。almost sorted意味着,从该数列中最多拿走一个数,这个数列就变成有序的了。
 
水题。
有一些坑点吧。要考虑仔细了。
拿判断是否上升almost sorted来说。最多只能有一个下降的点。比方说a[i]<a[i-1],这时候有两种删点的可能,一种是a[i],一种是a[i-1]。两种只要一种满足就行。
 
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int maxn=; int a[maxn+]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",a+i); bool flag1=true,flag2=true; for(int i=,j=;i<=n;i++)
{
if(a[i]<a[i-])
j++;
if(j==)
{
flag1=false;
break;
}
}
if(flag1)
{
for(int i=;i<=n;i++)
{
if(a[i]<a[i-])
{
if(i!=n&&a[i+]<a[i-]&&i-!=&&a[i-]>a[i])
flag1=false;
break;
}
}
} for(int i=,j=;i<=n;i++)
{
if(a[i]>a[i-])
j++;
if(j==)
{
flag2=false;
break;
}
}
if(flag2)
{
for(int i=;i<=n;i++)
{
if(a[i]>a[i-])
{
if(i!=n&&a[i+]>a[i-]&&i-!=&&a[i-]<a[i])
flag2=false;
break;
}
}
} if(flag1||flag2)
printf("YES\n");
else
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 sort, ...

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

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

  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. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

  9. HDU 5538 L - House Building 水题

    L - House Building Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

随机推荐

  1. vue响应式的注意事项

    在html中出现无法显示对象属性的情况,可能是需要在初始化对象时,先定义好属性. <template> <div> <div v-else class="req ...

  2. 看完这篇还不会用Git,那我就哭了!

    你使用过 Git 吗?也许你已经使用了一段时间,但它的许多奥秘仍然令人困惑. Git 是一个版本控制系统,是任何软件开发项目中的主要内容.通常有两个主要用途:代码备份和代码版本控制.你可以逐步处理代码 ...

  3. 各种优化方法总结比较(sgd/momentum/Nesterov/adagrad/adadelta)

    前言 这里讨论的优化问题指的是,给定目标函数f(x),我们需要找到一组参数x,使得f(x)的值最小. 本文以下内容假设读者已经了解机器学习基本知识,和梯度下降的原理. Batch gradient d ...

  4. Python3学习-基础

    1.直接运行.py文件 在Windows上是不行的,但是在Mac和Linux上是可以的,方法是在.py文件的第一行加上一个特殊的注释: #!/usr/bin/env python3 print('he ...

  5. linux常规网卡配置正确,但是出不了路由的解决方法

    netstat -rn #查看是网关  route add default gw 192.168.128.2 dev eth0  # 手动加入网关地址   此类情况容易出现在双网卡配置后

  6. 聚类-DBSCAN基于密度的空间聚类

    1.DBSCAN介绍 DBSCAN(Density-Based Spatial Clustering of Applications with Noise,具有噪声的基于密度的聚类方法)是一种基于密度 ...

  7. 08-kubernetes 存储卷

    目录 存储卷 emptyDir 测试及使用 Pod测试挂在共享NFS 写测试清单 测试 pv, pvc 创建几个PV 创建测试的Pod 和 PVC 存储卷 分为四种: 有状态,需要存储 有状态,无需存 ...

  8. Linux进阶文档丨阿里架构师十年Linux心得,全在这份文档里面

    Linux是什么 Linux就是个操作系统: 它和Windows XP.Windows 7.Windows 10什么的一样就是一个操作系统而已! Linux能干什么: 它能当服务器,服务器上安装者各种 ...

  9. Django REST framework(DRF)

    Django REST framework(DRF) FBV与CBV CBV源码分析 Restful接口规范 DRF简单配置使用和源码解析 DRF序列化器 DRF10大接口 DRF视图集 三大组件 j ...

  10. vue中的$EventBus.$emit、$on 遇到的问题

    今天在项目中遇到的一个需求: 在一个选项卡功能的页面,出现的问题是,当点击选项卡的某个选项时,会同时加载整个选项卡的数据,本身产品就很大,数据很多,所以这个问题无法忽略: 仔细研究下发现,当刚进入页面 ...