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 ...
随机推荐
- IDEA2018 license
2018-06-01更新 更新了webstorm 3.2之后发现居然又不能用了,现用 http://idea.congm.in 可以激活 新增一个 http://idea.toocruel.net
- Linux中Apache+Tomcat+JK实现负载均衡和群集的完整过程
人原创,一个字一个字地码的,尊重版权,转载请注明出处! http://blog.csdn.net/chaijunkun/article/details/6987443 最近在开发的项目需要承受很高的并 ...
- linux 关机、重启
一.重启命令:1.reboot2.shutdown -r now 立刻重启(root用户使用)3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.shutdown -r 20 ...
- Alpha,Beta,RC,RTM,EVAL,CTP,OEM,RTL,VOL
微软的一个系统(如Win 7)或开发工具(VS系列),往往会对应很多种版本,下面就介绍一下这些版本的含义: Alpha (阿尔法,希腊字母的第一位'α',代表最初的版本) Alpha是内部测试版, ...
- CentOS6.7的安装
VMware9的安装请阅读: http://www.cnblogs.com/duanji/p/yueding.html CentOS6.7在VMware9中安装 1.启动VMware的画面 2.点击 ...
- BZOJ3267/3272 KC采花/Zgg吃东西(线段树)
直接维护选k个子段时的最优解似乎也可以做,然而复杂度是O(nk2logn),显然跑不过. 考虑一种费用流做法.序列里每个点拆成入点和出点,源连入汇连出,入点和出点间连流量1费用ai的边,相邻点出点向入 ...
- 查看MySQL最近执行的语句
首先登入MySQL. Reading table information for completion of table and column names You can turn off this ...
- 左连接,右连接和等值连接(left join,right join和inner join)
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- mac 使用tree命令
下载软件包: http://mama.indstate.edu/users/ice/tree/ 安装说明: http://www.qiansw.com/mac-os-x-install-tree-co ...
- 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密
我们在前一篇文章中实现了cas4.2.x登录使用mongodb验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也学习参考了cas5.0.x版 ...