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. set -ex

    #!/bin/bash set -x echo "Hello World !" 执行效果为 + echo Hello World !Hello World ! - 其实效果和sh ...

  2. java知识查漏补缺

    一.重写(override)和重载(overload)的区别 二者除了名字相似,其实没什么联系 范围不同:重载发生在同一个类的不同方法之间.重写发生在父类和子类自荐. 前提: 重载要求:方法名相同,参 ...

  3. STS热部署方法(springboot)

    sts热部署,即是在项目中修改代码不用重新启动服务,提高效率.   方法如下: 1.在pom文件中引入  devtools  依赖: <dependency> <groupId> ...

  4. kotlin 简单处理 回调参数 加?

    Kotlin Parameter specified as non-null is null 2017年10月18日 17:21:49 amiko_ 阅读数:9017    版权声明:本文为博主原创文 ...

  5. C++入门经典-例5.7-调用自定义函数交换两变量值,传入指针

    1:代码如下: // 5.7.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  6. 桥接模式下,主机能ping通虚拟机,虚拟机ping不通主机

    好像是防火墙阻止了什么东西而导致的无法ping通! 1.打开WIN7防火墙 2.选择高级设置 3.入站规则 4.找到配置文件类型为“公用”的“文件和打印共享(回显请求 – ICMPv4-In)”规则, ...

  7. Netem参数说明

    Netem参数说明 本文主要内容来自Linux基金会Wiki网站Netem文档,点击这里访问原文 netem通过模拟广域网的特性为测试协议提供网络仿真功能.当前版本模拟可变延迟,丢失,重复和重新排序. ...

  8. oracle数据库面试相关

    1.实现分页 rownum: select * from (select aa.* rownum rn from (select * from student )aa where rownum < ...

  9. 四十八:数据库之alembic常用命令和经典错误的解决办法

    常用命令:1.init:创建一个alembic仓库2.reversion:创建一个新的版本3.--autogenerate:自动将当前模型的修改,生成迁移脚本4.-m:message,可以记录本次迁移 ...

  10. Java学习之==>常用字符串方法

    1.定义字符串 // 定义, 为初始化 String str1; // 定义, 并初始化为null String str2 = null; // 定义, 并初始化为空串 String str3 = & ...