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 ...
随机推荐
- 2018最新Web前端经典面试试题及答案
javascript: JavaScript中如何检测一个变量是一个String类型?请写出函数实现 typeof(obj) === "string" typeof obj === ...
- Read N Characters Given Read4 II - Call multiple times
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- Qt中 QString 转 char*
Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...
- 【开发工具IDE】eclipse的SVN提交忽略target等多余文件
这个build失败的解决方案就是不要把你项目的 target目录放在src repository 里面,还有 .project 和 .classpath最好也别放到src repository 里. ...
- Pentaho的Mondrian对Hive的支持
需求描述 考虑直接在Hive或者Impala等Big Data方案,能够支持MDX查询,现调研一下Mondrian对hive的支持情况. 环境准备 hive环境,采用hive-0.10-cdh4.2. ...
- number类型转化为string类型
toString 方法 string = toString(num) 缺点: 不能转化 underfind 和 null 2 String 方法 string = String(num) 可以转化 u ...
- 【BZOJ2655】Calc(拉格朗日插值,动态规划)
[BZOJ2655]Calc(多项式插值,动态规划) 题面 BZOJ 题解 考虑如何\(dp\) 设\(f[i][j]\)表示选择了\(i\)个数并且值域在\([1,j]\)的答案. \(f[i][j ...
- 51nod 1376 最长上升子序列的数量 | DP | vector怒刷存在感!
51nod 1376 最长上升子序列的数量 题解 我们设lis[i]为以位置i结尾的最长上升子序列长度,dp[i]为以位置i结尾的最长上升子序列数量. 显然,dp[i]要从前面的一些位置(设为位置j) ...
- 【bzoj4520】 Cqoi2016—K远点对
http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...
- 【DP】【P4539】 [SCOI2006]zh_tree
Description 张老师根据自己工作的需要,设计了一种特殊的二叉搜索树. 他把这种二叉树起名为zh_tree,对于具有n个结点的zh_tree,其中序遍历恰好为(1,2,3,-,n),其中数字1 ...