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. C# => 写法

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder ...

  2. 我不熟悉的string类

    我不常用的string函数 多的不说,直接上: assign函数 string& assign(const char *s); //把字符串s赋给当前的字符串 string& assi ...

  3. 谷歌浏览器安装 socketLog

    第一步(本地浏览器安装调试扩展) 下载扩展包并解压 链接:https://pan.baidu.com/s/14df0ewl_3wjRHc8H1jsrWQ提取码:yyu1 打开谷歌浏览器,地址栏输入 c ...

  4. Linux下运行scala语言的jar包

    1.新建project 2.打包 3.linux下运行jar包 #First.jar为jar包名,Test为主类名 [root@FI-2 Desktop]# spark-submit First.ja ...

  5. LinkedList类源码浅析(二)

    1.上一节介绍了LinkedList的几个基本的方法,其他方法类似,就不一一介绍: 现在再来看一个删除的方法:remove(Object o) remove方法接受一个Object参数,这里需要对参数 ...

  6. 【Java基础】谈谈集合.List

    摘自:https://www.cnblogs.com/54chensongxia/p/11722828.html 目录 1. ArrayList 1.1 ArrayList的构造 1.2 add方法 ...

  7. C++入门经典-例6.12-使用数组地址将二维数组输出

    1:以a[4][3]为例 a代表二维数组的地址,通过指针运算符可以获取数组中的元素 (1)a+n代表第n行的首地址 (2)&a[0][0]既可以看作第0行0列的首地址,同样也可以被看作是二维数 ...

  8. 第八周课程总结 & 实验报告(六)

    第八周课程总结 一.包装类 介绍 装箱与拆箱 应用 二.异常 基本概念 基本格式 异常类的继承结构 throws关键字 throw关键字 Exception类和RuntimeException类 自定 ...

  9. 排序算法(C++)

    冒泡排序.选择排序.快速排序.插入排序.希尔排序.归并排序.基数排序.堆排序. 推荐网址1:https://www.cnblogs.com/onepixel/articles/7674659.html ...

  10. C++构造函数调用虚函数的后果

    #include <iostream> class cx { public: virtual void func() { std::cout << "func&quo ...