HDU 5532 Almost Sorted Array (最长非递减子序列)
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
分析:
给出n个数,问如果去掉其中一个数,能否构成不上升或者不下降的序列。
比赛的时候直接暴力写的,代码写了不到一百行,最组要需要考虑的情况有点复杂,这里使用非递减子序列的思想。
可以将数组正序进行一次最长非递减子序列,再将数组逆序进行一次最长非递减子序列,最终只要其中又有个满足子序列的长度等于n(本身就是满足情况的),或则等于n-1(去掉其中的一个之后满足情况)即可。
数组逆序求非递减,也就相当于对于原数组求非递增。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100005],ans[100005];
//a数组是原始的输进去的序列,ans数组是保存的a数组中的非递减子序列,ans数组中的元素一定是有序的
int main()
{
int T,n,len;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
ans[1]=a[1];//首先只有一个元素的话肯定就是有序的了。
len=1;
for(int i=2; i<=n; i++)
{
if(a[i]>=ans[len])//如果大于等于当前的ans数组中的最后一个元素(最大的),那么这个数可以直接加到ans数组中
ans[++len]=a[i];
else
{
/*
当前的这个数没有办法直接加入到ans数组的最后面,但是我们可以将ans数组中的比他大并且距离他最近的那个数给替换掉
这样子序列的长度肯定会减少1,并且保证子序列里面的元素尽可能的小
*/
int pos=upper_bound(ans+1,ans+len+1,a[i])-ans;
ans[pos]=a[i];
}
}
if(len==n-1||len==n)//子序列的长度为n或则n-1的话,计算满足条件的
{
printf("YES\n");
continue;
}
///再反过来进行一遍,寻找反过来的非递减,也就相当于原序列的非递增
ans[1]=a[n];
len=1;
for(int i=n-1; i>=1; i--)
{
if(a[i]>=ans[len])
ans[++len]=a[i];
else
{
int pos=upper_bound(ans+1,ans+len+1,a[i])-ans;
ans[pos]=a[i];
}
}
if(len==n-1||len==n)
printf("YES\n");
else printf("NO\n");
}
return 0;
}
HDU 5532 Almost Sorted Array (最长非递减子序列)的更多相关文章
- HDU 6357.Hills And Valleys-字符串非严格递增子序列(LIS最长非下降子序列)+动态规划(区间翻转l,r找最长非递减子序列),好题哇 (2018 Multi-University Training Contest 5 1008)
6357. Hills And Valleys 自己感觉这是个好题,应该是经典题目,所以半路选手补了这道字符串的动态规划题目. 题意就是给你一个串,翻转任意区间一次,求最长的非下降子序列. 一看题面写 ...
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
D. Once Again... You a ...
- HDU 6357.Hills And Valleys-动态规划(区间翻转l,r找最长非递减子序列)
题意:给一串由n个数字组成的字符串,选择其中一个区间进行翻转,要求翻转后该字符串的最长非降子序列长度最长,输出这个最长非降子序列的长度以及翻转的区间的左右端点 #include<bits/std ...
- 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(模拟)
Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, ...
- 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 1025 Constructing Roads In JGShining's Kingdom[动态规划/nlogn求最长非递减子序列]
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- sqlserver 对比数据库表是否完全一致的简单方法
1. 使用数据库的工具进行处理 tablediff.exe 工具目录 C:\Program Files\Microsoft SQL Server\\COM 工具使用说明 tablediff.exe - ...
- [转帖] Linux buffer 和 cache相关内容
Linux中Buffer/Cache清理 Lentil2018年9月6日 Linux中的buff/cache可以被手动释放,释放缓存的代码如下: https://lentil1016.cn/linux ...
- Anaconda多版本Python管理以及TensorFlow版本的选择安装
Anaconda是一个集成python及包管理的软件,记得最早使用时在2014年,那时候网上还没有什么资料,需要同时使用py2和py3的时候,当时的做法是同时安装Anaconda2和Anaconda3 ...
- Windows 配置Reids集群 Redis Cluster
一 .所需软件:Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb 二 .安装配置redis redis下载地 ...
- JAXB java类与xml互转
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...
- 出错:ORA-20000: ORU-10028: line length overflow, limit of 255 bytes per line?
起因:DBMS_OUTPUT.put(V_SQL) 提示太长错误,设置了buffer=>null 和set 长度都不行 解决方法如下: testxx.debug_print(V_SQL); -- ...
- php的数组转为对象
有时候数组要转为对象操作,用对象的指向操作符,有两种方法 方法一: $arr=['a'=>10,'b'=>100,'c'=>'Hello']; $obj=(Object)$arr; ...
- 【Learning】容斥原理
PPT在这里 https://files.cnblogs.com/files/RogerDTZ/%E5%AE%B9%E6%96%A5%E5%8E%9F%E7%90%86.pdf
- 20135319zl字符集报告
字符集实验 ASCII 首先,查找ZHULI五个字符对应的ASCII码,5a 48 55 4c 49. 然后,用vim打开一个空文档,按下":",输入%!xxd 然后,输入 000 ...
- 20165218 《网络对抗技术》Exp1 逆向及Bof基础
Exp1 逆向及Bof基础 基础知识 1. NOP, JNE, JE, JMP, CMP汇编指令的机器码 指令 机器码 NOP NOP指令即"空指令",在x86的CPU中机器码为0 ...