Almost Sorted Array

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 10562    Accepted Submission(s): 2449

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
 
Source
 
Recommend
hujie
onlgn求解LIS的基本思想是,用dp[i]保存长度为i的最长子序列的最大值的最小值。
遍历数组,如果a >= dp[len],接到后面,否则,在dp中寻找第一个大于这a的数,把他替换掉,原因很好想,要想使得序列足够长,那么dp[i]越小越好。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ; int n, val[maxn];
int cnt1, cnt2, dp[maxn]; int main() {
int t;
int a, temp;
scanf("%d", &t);
while(t --) {
cnt1 = , cnt2 = ;
memset(dp, , sizeof dp);
scanf("%d", &n);
for(int i = ; i < n; i ++) scanf("%d", &val[i]);
for(int i = ; i < n; i ++) {
if(val[i] >= dp[cnt1]) dp[++ cnt1] = val[i];
else {
temp = upper_bound(dp + , dp + + cnt1, val[i]) - dp;
dp[temp] = val[i];
}
}
memset(dp, , sizeof dp);
for(int i = n - ; i >= ; i --) {
if(val[i] >= dp[cnt2]) dp[++ cnt2] = val[i];
else {
temp = upper_bound(dp + , dp + + cnt2, val[i]) - dp;
dp[temp] = val[i];
}
}
if(cnt1 >= n - || cnt2 >= n - ) printf("YES\n");
else printf("NO\n");
}
return ;
}

Almost Sorted Array(o(nlgn)求解LIS)的更多相关文章

  1. hdoj--5532--Almost Sorted Array(正反LIS)

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  2. HDU_5532_Almost Sorted Array

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  3. HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

    Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...

  4. HDU - 5532 Almost Sorted Array (最长非严格单调子序列)

    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...

  5. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  6. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  7. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  9. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

随机推荐

  1. .Net笔试考题

    .NET试题 1.列举ASP.NET页面之间传递值的几种方式 2.请写出 override 与重载的区别 3.请编程实现一个冒泡排序算法 4.什么是装箱和拆箱 5.ADO.net中常用的对象有哪些?分 ...

  2. try捕获SQL异常

  3. 51 Nod 1020 逆序排列

    1020 逆序排列  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么 ...

  4. HGOI 20190709 题解

    Problem A 紫色激情 一个序列$\{a_n\}$,求出方差最大的子序列. 其中方差 [l,r] 的定义是$S^2 = \frac{1}{n} \sum\limits_{i=l}^{r} (x_ ...

  5. python类装饰器即__call__方法

    上一篇中我对学习过程中的装饰器进行了总结和整理,这一节简单整理下类装饰器 1.类中的__call__方法: 我们在定义好一个类后,实例化出一个对象,如果对这个对象以直接在后边加括号的方式进行调用,程序 ...

  6. python学习之路(4)

    使用list和tuple Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >& ...

  7. python3笔记十一:python数据类型-List列表

    一:学习内容 列表概念 列表创建:创建空列表.创建带有元素的列表 列表访问:取值 列表修改:替换元素.追加元素.追加列表.插入元素 列表删除:移除列表中指定下标处的元素.移除匹配条件的第一个元素.移除 ...

  8. moment 时间格式化

    使用方法: 引入 moment 的包 moment(要处理的时间).format(时间的格式): vue 的过滤器为例: str 代表要处理的时间  orf 代表 时间的格式

  9. synchronized 读写同步

    synchronized 读写同步 这是一道面试题,很多人也遇到了. 要求:1.读-读 不用线程同步.2.读-写 要求线程同步,写的时候不能读.3.写-写同步.写的时候不能写. java lock读写 ...

  10. java 中的多态

    多态是面向对象的重要特性,简单点说:“一个接口,多种实现”,就是同一种事物表现出的多种形态. 下面利用代码进行简单概述: 实现多态必须要满足三个条件 1必须有继承 2必须重写 3必须父类引用子类对象 ...