Almost Sorted Array(o(nlgn)求解LIS)
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
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?
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
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)的更多相关文章
- hdoj--5532--Almost Sorted Array(正反LIS)
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 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array
Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...
- HDU - 5532 Almost Sorted Array (最长非严格单调子序列)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- [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. 这道 ...
- [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 ...
随机推荐
- Spring MVC遭遇checkbox的问题解决方案
转:http://lavasoft.blog.51cto.com/62575/1407213 Spring MVC遭遇checkbox的问题是:当checkbox全不选时候,则该checkbox域的变 ...
- jquery attribute选择器 语法
jquery attribute选择器 语法 作用:[attribute] 选择每个带有指定属性的元素.可以选取带有任何属性的元素(对于指定的属性没有限制). 语法:$("[attribut ...
- 单元测试 Junit
- [模板] 计算几何2: 自适应Simpson/凸包/半平面交/旋转卡壳/闵可夫斯基和
一些基本的定义在这里: [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算 自适应Simpson Simpson's Rule: \[ \int ^b_a f(x)dx\approx ...
- TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs
题意:见挑战230页 #include <iostream> #include <cstdio> #include <cstring> #include <c ...
- Latex生成的.pdf 公式之间隔了几行空白
如题, 解决办法: \vspace{-1.5cm},这个数值根据需要来设置.
- @vue/cli 3.x 版本配置productionGzip提高性能
第一步:安装插件 npm i -D compression-webpack-plugin 第二步:引入.在文件vue.config.js里导入compression-webpack-plugin,并添 ...
- docker—tomcat 报错:Failed to get D-Bus connection: Operation not permitted
docker search centos 查系统镜像 docker pull docker.io/centos 进入容器 [root@git opt]# docker images REPOSIT ...
- redis high available solution/ redis 高可用方案
http://developers.linecorp.com/blog/?p=1420 http://engineering.docusign.com/articles/redis-sentinel- ...
- nginx location正则
nginx location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # ...