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. 作业要求2018092609-2 选题 Scrum立会报告+燃尽图 01

    本组第一次作业 已由组员刘信鹏同学个人博客提交 链接 :  [https://www.cnblogs.com/liuxp775/p/11595227.html]

  2. 【NHOI2018】找素数

    [题目描述] 素数又称质数,是指一个大于 1 的正整数,如果除了 1 和它本身以外,不能再被其它的数整除,例如:2.3.5.97 等都是素数.2 是最小的素数. 现在,给你 n 个数字,请你从中选取一 ...

  3. day 36 初始前端 html语言

    参考博客https://www.cnblogs.com/majj/p/9056951.html进行学习 html标签 特征: .空白折叠现象 .对空格和换行不敏感 .标签要严格封闭 p标签的嵌套 多注 ...

  4. CentOS 7 ETCD集群配置大全

    目录 前言 环境准备 安装 静态集群 配置 node01 配置文件 node02 配置文件 node03 配置文件 启动测试 查看集群状态 生成TLS证书 etcd证书创建 安装cfssl工具集 生成 ...

  5. CentOS 7 Nginx部署.NET Core Web应用

    部署.NET Core运行时 必要前提 在安装.NET Core前,需要注册Microsoft签名秘钥并添加Microsoft产品提要,每台机器只需要注册一次,执行如下命令: sudo rpm -Uv ...

  6. Chapter 05—Advanced data management(Part 2)

    二. 控制流 statement:一个单独的R语句或者是一个复合的R语句: cond:条件表达式,为TRUE或FALSE: expr:数字或字符表达式: seq:数字或字符串的顺序. 1.循环语句:f ...

  7. Nginx动静分离(Nginx+Tomcat)

    第一步:nginx构建 第二步:Tomcat构建 1.Tomcat基础点 (1)Tomcat 是基于java开发的web容器,用来发布java代码和jsp网页. (2)开发人员开发java web网站 ...

  8. nginx项目部署

    1.概念    1.Django项目(Web应用)    2.web服务        1.nginx        2.Apache            LNMP -> Linux Ngin ...

  9. 《Windows内核安全与驱动开发》 4.4 线程与事件

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发> 4.4 线程与事件 一.开辟一个线程,参数为(打印内容+打印次数),利用线程 ...

  10. Swoole 是 PHP 中的 Node.js?

    一想到那些可以使用 Node 的同事,一些 PHP 开发者的脸都嫉妒绿了.异步 Node 系统可以在不同协议间共享代码库,并在代码之外提供服务.这真的想让一个人转 Node 开发.实际上 PHP 中也 ...