Almost Sorted Array


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
 
题意:给你一个n个数的序列,让你删除一个数后,这个序列是严格不下降或者不上升的序列
题解:最先想到就是LIS,可做
        还有一种是O(N) 对于每一个数,要么呈现V形或者^形,或者单调,记录上升下降次数,我们对删除某个数所造成影响记录下来,假如删除它致使最后上升次数或者下降次数满足n-2次就行了
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 1000000007
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//**************************************** #define maxn 100000+5
int a[maxn],n;
int main(){
int T=read();
while(T--){
scanf("%d",&n);int flag=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int res=,fall=;
for(int i=;i<=n;i++){
if(i->=&&a[i]>a[i-])res++;
else if(i->=&&a[i]<a[i-])fall++;
}
// cout<<fall<<" "<<res<<endl;
for(int i=;i<=n;i++){
int t1=res,t2=fall;
if(i->=&&a[i]>a[i-]){
res--;
}
else if(i->=&&a[i]<a[i-]){
fall--;
}
if(i+<=n&&a[i]>a[i+]){
fall--;
}
else if(i+<=n&&a[i]<a[i+]){
res--;
}
if(i->=&&i+<=n){
if(a[i-]<a[i+]){
res++;
}else if(a[i-]>a[i+])fall++;
}
if(fall==||res==){
flag=;break;
}
res=t1;fall=t2;
}
if(flag){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl; }
return ;
}

代码

最新文章

  1. Emoji选项列表
  2. 嵌入式Linux驱动学习之路(十九)触摸屏驱动、tslib测试
  3. WPF项目中所遇到的一些问题集
  4. Python中的魔法方法
  5. Java集合源码分析(一)
  6. android listview多视图嵌套多视图
  7. 判断Laravel Eloquent获取数据结果集是否为空
  8. HDU4008 Parent and son(树形DP LCA)
  9. [Javascript] Lodash: Refactoring Simple For Loops (_.find, _.findLast, _.filter)
  10. Java getResourceAsStream返回为空的问题
  11. Shark集群搭建配置
  12. 对JDBC的优化,BeanUtils和DBUtils
  13. Spring AOP分析(2) -- JdkDynamicAopProxy实现AOP
  14. javascript 模板
  15. Linux编程 13 (系统环境变量位置, 环境变量持久化)
  16. Ajax入门例子
  17. THEPYTHONCHALLENG闯关记录
  18. import logging报错raise notimplementederror &#39;emit must be implemented &#39; ^
  19. eclipse.ini 修改默认编码为 UTF-8
  20. mysql中 where in 用法详解

热门文章

  1. linux,apache,mysql,php常用查看版本信息的方法
  2. WEB开发模式浅析
  3. SSL协议提供的服务
  4. day07补充-数据类型总结及拷贝
  5. 扩增子图表解读6韦恩图:比较组间共有和特有OTU或分类单元
  6. spring IOC bean中注入集合
  7. CAD绘制一个对齐标注(com接口VB语言)
  8. CAD处理键盘按钮被释放(com接口VB语言)
  9. Python 连接数据库 day5
  10. [SQL Service] 时间处理:获取今天的00:00:00/获取今天的23:59:59