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 ...
随机推荐
- [转帖]从HTTP/0.9到HTTP/2:一文读懂HTTP协议的历史演变和设计思路
从HTTP/0.9到HTTP/2:一文读懂HTTP协议的历史演变和设计思路 http://www.52im.net/thread-1709-1-2.html 本文原作者阮一峰,作者博客:r ...
- java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ClassLoadingException
下载高版本的: hibernate-commons-annotations-5.0.1.Final.jar 低版本缺包
- how install svn client on MacOS
how install svn client on MacOS svn https://www.smartsvn.com/downloads/smartsvn/smartsvn-macosx-11_0 ...
- Delphi : keydown与keypress的区别,组合键
Shift 是一个集合变量. type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDoubl ...
- POJ1430
这个题目初看上去是一个排列组合题,而实际上……也是一个排列组合题. 题目描述是: Description The Stirling number of the second kind S(n, m) ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- KEIL工程中头文件包含的一些小记
@2018-4-25 11:02:27 > 源文件包含自身头文件无须指出其路径
- 我的shell脚本
问题:在ip.lt文件中有600个IP,有3个文档模版,三个文档的名称结构都是“ip+一系列字符串”,要求:1.将600个IP分成3分,以三个模版为基础创建600个文档,名字结构与模版相同:2修改60 ...
- kickstart无人值守安装之实践篇
1.系统环境准备 涉及的服务有: DHCP服务 TFTP服务 PXE客户端 HTTP服务 [root@ks ~]# cat /etc/redhat-release CentOS release 6.9 ...
- CentOS安装oracleJDK
实践证明,oracleJDK比openJDK快不止一星半点,换了之后立马从满负载降到个位数负载. 1.卸载openjdk # rpm -qa | grep -E '^(java|jdk)' # yum ...