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,…,ana1,a2,…,an, is it almost sorted?

InputThe first line contains an integer TT indicating the total number of test cases. Each test case starts with an integer nn in one line, then one line with nn integers a1,a2,…,ana1,a2,…,an.

1≤T≤20001≤T≤2000 
2≤n≤1052≤n≤105 
1≤ai≤1051≤ai≤105 
There are at most 20 test cases with n>1000n>1000.OutputFor 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
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
const int inf=1e9+;
const int maxn=1e5+;
using namespace std;
int a[maxn],b[maxn],c[maxn];
int n;
int binary_search(int x){ //二分搜索
int l=,r=n;
while(l<r){
int mid=(l+r)>>;
if(b[mid]>x) r=mid;
else l=mid+;
}
return l;
}
bool LIS(const int *a){ //LIS
memset(b,inf,sizeof(b));
b[]=;
int len=;
for(int i=; i<=n; ++i){
int k=binary_search(a[i]);//i位置的LIS为idx
len=max(k,len);
b[k]=a[i];
}
if(len>=n-) return true; //满足条件即可
return false;
}
int main(){
int t;scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=; i<=n; ++i){
scanf("%d",&a[i]);
c[n-i+]=a[i];
}
if(LIS(a)||LIS(c)) puts("YES");
else puts("NO");
}
return ;
}

另一种:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=;
const int N=+;
ull seed=; int dp[N],ans[N],a[N];
int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
int n;scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int len=,flag1=,flag2=;
ans[]=a[];
for(int i=;i<=n;i++)
{
if(a[i]>=ans[len]) {len++;ans[len]=a[i];}
else{
int pos=upper_bound(ans+,ans+len,a[i])-ans;
ans[pos]=a[i];
}
}
if(len>=n-) flag1=;
len=;ans[]=a[n];
for(int i=n-;i>=;i--)
{
if(a[i]>=ans[len]) {len++;ans[len]=a[i];}
else{
int pos=upper_bound(ans+,ans+len,a[i])-ans;
ans[pos]=a[i];
}
}
if(len>=n-) flag2=;
if(flag1||flag2) printf("YES\n");
else printf("NO\n");
}
return ;
}

分析:严格单调递增子序列就是lower_bound(),直接进行替换;非严格单调子序列就是upper_bound();

 if(a[i]>=ans[len]) {
len++;
ans[len]=a[i];
}
else{
int pos=upper_bound(ans+,ans+len,a[i])-ans;//非严格单调
ans[pos]=a[i];
}

HDU - 5532 Almost Sorted Array (最长非严格单调子序列)的更多相关文章

  1. hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  2. HDU 5532 Almost Sorted Array (最长非递减子序列)

    题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...

  3. hdu 5532 Almost Sorted Array(模拟)

    Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, ...

  4. HDU 5532——Almost Sorted Array——————【技巧】

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  5. hdu 5532 Almost Sorted Array (水题)

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  6. hdu 5532 Almost Sorted Array

    http://acm.hdu.edu.cn/showproblem.php?pid=5532  题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...

  7. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  8. 最长非降/下降子序列问题(DP)(待续...)

    注意:抽象成以下描述即为最长非降/下降子序列问题(一维状态) 问题描述:在一个无序的序列a1,a2,a3,a4…an里,找到一个最长的序列满足:(不要求连续) ai<=aj<=ak…< ...

  9. 2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

随机推荐

  1. ELF文件之八——使用链接脚本-2个函数-data-bss-temp-call-debug信息

    gcc编译选项可以设置生成调试信息, debug信息格式有stabs,coff,xcoff,dwarf. 常用的有两种格式,stab和dwarf,stab较早,dwarf较新.两种格式介绍:https ...

  2. vuex知识笔记,及与localStorage和sessionStorage的区别

    菜单快捷导航 Vuex是什么东东,有什么应用场景?localStorage和sessionStorage能否替代它? Vuex知识点State.Getter.Mutaion.Action Vuex模块 ...

  3. Day2前端学习之路——HTML基本知识

    课程目标: 通过制作自己的简历,更加清楚地了解HTML是什么,HTML5是什么.学习基本的HTML标签,理解HTML语义化概念 任务一:回答问题 1.HTML是什么,HTML5是什么? HTML是一种 ...

  4. SpringBoot安全管理--(二)基于数据库的认证

    简介: 上篇文章向读者介绍的认证数据都是定义在内存中的,在真实项目中,用户的基本信息以及角色等都存储在数据库中,因此需要从数据库中获取数据进行认证. 开始: 首先建表并且插入数据: pom.xml & ...

  5. RequestFacade对象获取请求头时忽略大小写

    也许在Controller层 在RequestFacde文件中getHeader函数逻辑实现如下所示: public String getHeader(String name) { if(this.r ...

  6. Blazor初体验之寻找存储client-side jwt token的方法

    https://www.cnblogs.com/chen8854/p/securing-your-blazor-apps-authentication-with-clientside-blazor-u ...

  7. 拍摄UFP 单一职责原则

    3.1 新手机 3.2 拍摄 3.3 没用的东西 3.4 单一职责原则 就一个类而言,应该仅有一个引起它变化的原因, 3.5 方块游戏的设计 如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个 ...

  8. 【一起刷LeetCode】整数反转

    前言&絮叨 别人都忙着参加年会晒奖品,我却忙着写代码.每逢年底都要安排几个紧急项目,我什么时候能摆脱这种宿命. 在忙也不能忘记刷LeetCode,毛毛向前冲!!! 题目描述 给出一个 32 位 ...

  9. C#MVC实现为雇员配置角色(完整详细+数据库)

    数据库创建“用户表”“角色表”“用户角色关系表” create table roles ( RId int identity, RName varchar(), Remark varchar() ) ...

  10. spring cloud微服务快速教程之(八) Spring Cloud Alibaba--nacos(二)、配置中心

    0-前言 上一篇我们介绍了nacos作为服务注册发现组件的功能,nacos还具有配置中心的功能,而且支持热加载: 在此之前,配置中心有Spring Cloud Config,实际上,用这个有很多风险和 ...