Almost Sorted Array

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

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
 
  • 要求是去掉一个元素使得新数列为非严格单调数列
  • 那就正逆跑一遍nlogn的LIS,把判定里的小于号改成小于等于号就可以做到对于非严格单调的要求
  • 然后原来算法里面的lower_bound改成upper_bound就行了
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int T, n, ans;
int a[maxn], c[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
while(T--){
ans=;
scanf("%d",&n);
scanf("%d",&a[]);
c[]=a[];
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=c[ans-]){
c[ans++]=a[i];
}else{
c[upper_bound(c,c+ans,a[i])-c]=a[i];
}
}
if(n-ans== || n==ans){
puts("YES");
}else{
ans=;
c[]=a[n];
for(int i=n-;i>;i--)
if(a[i]>=c[ans-]){
c[ans++]=a[i];
}else{
c[upper_bound(c,c+ans,a[i])-c]=a[i];
}
if(n-ans== || n==ans)
puts("YES");
else
puts("NO");
}
}
}
return ;
}

最新文章

  1. OSI7层模型详解
  2. 修改一个CGRect的值
  3. expect使用demo
  4. Tensorflow学习笔记4:分布式Tensorflow
  5. word 2010自定义快捷键提高工作效率
  6. 论文笔记之:Visual Tracking with Fully Convolutional Networks
  7. unity, 用unity profiler进行真机profile,需要退出360
  8. [Java] SSH框架笔记_框架分析+环境搭建+实例源码下载
  9. Swift字典集合
  10. MySQL Connector/Python 安装、测试
  11. Linux读取文件路径问题
  12. QT5中的pro文件中为何要加入&quot;QT += widgets&quot;
  13. logstash 输出到elasticsearch 自动建立index
  14. 蓝桥杯-第一个数字-java
  15. 安装与配置cacti 0.8.8b
  16. ftok()函数深度解析
  17. xlwt set style making error: More than 4094 XFs (styles)
  18. String()与toString的区别
  19. Java内存泄露处理
  20. 错误:不小心的VS重命名

热门文章

  1. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)
  2. JAVA多线程之Synchronized、wait、notify实例讲解
  3. Jquery弹窗
  4. QT 4.8.5 源代码目录调整
  5. wifi设置
  6. am335x gpio控制
  7. UCOS2系统内核讲述_总体描述
  8. hive组件和执行过程
  9. mac 安装软件提示权限不足的解决的方法
  10. strust2的Action中validateXxx方法的用法