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. 重读es6, 正确了解promise中catch的用法

    前言 在最近的项目中,用到了es6的promise语法,发现promise.prototype.catch 并不只是单单reject抛出的回调函数,所以今天做一些笔录,防止以后在项目中又碰到这样的问题 ...

  2. 通过 python 处理 email - Email via Python

    Email via Python 1 MIME - Multipurpose Internet Mail Extensions SMTP - Simple Message Transport Prot ...

  3. Redis中RDB和AOF持久化区别和联系

    RDB和AOF持久化   ​RDB持久化 RDB是什么? 原理是redis会单独创建(fork) 一个与当前进程一模一 样的子进程来进行持久化,这个子进程的所有数据(变量.环境变量,程序程序计数器等) ...

  4. Mysql 导入导出备份

    恢复MySQL服务器上面的txt格式文件(需要FILE权限,各数据值之间用"制表符"分隔)   1.导入数据库服务器上的txt文件 mysql>load data infil ...

  5. php-fpm.conf.default配置文件

    ;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative paths in this confi ...

  6. for循环嵌套练习题or99乘法表

    //输出1-10之间的和 public static void whileTest(){ //定义变量用于存储不断变化的和 int sum = 0; //定义变量,用于记录不断变化的被加数 int x ...

  7. Kafka消费者没有收到通知的分析

    今天遇到两位三方人员跟我反馈,某微服务的异步接口功能不正常了,由于该异步接口采用Kafka异步消息的方案,对方说没有收到Kafka给消费者的通知,根据此问题,联系了相关人员进行了分析: (一)明确环境 ...

  8. Galactic Collegiate Programming Contest Gym - 101572G 模拟

    #include<bits/stdc++.h> using namespace std; int n,m; struct node { int id; int slove; int pen ...

  9. Pycharm每次新建工程都要重新安装相关库的解决办法

    之前自己每次重建工程时,都不厌其烦的重新安装了第三方的库,直接在pycharm的terminal中利用pip安装,或者鼠标放在所需库的红色波浪线上 直接点击Install Package XXX 后面 ...

  10. Enityt模型特性

    数据验证相关的数据注解: 特性 解释 Remote 使用 jQuery 验证插件远程验证程序的特性 FileExtension 验证文件扩展名 Compare 比较两个属性的值 RegularExpr ...