HDU - 5532 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 (最长非严格单调子序列)的更多相关文章
- hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- hdu 5532 Almost Sorted Array(模拟)
Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, ...
- HDU 5532——Almost Sorted Array——————【技巧】
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 Almost Sorted Array
http://acm.hdu.edu.cn/showproblem.php?pid=5532 题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...
- 【HDU 5532 Almost Sorted Array】水题,模拟
给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...
- 最长非降/下降子序列问题(DP)(待续...)
注意:抽象成以下描述即为最长非降/下降子序列问题(一维状态) 问题描述:在一个无序的序列a1,a2,a3,a4…an里,找到一个最长的序列满足:(不要求连续) ai<=aj<=ak…< ...
- 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 ...
随机推荐
- 云服务器InfluxDB & Chronograf配置
环境: 阿里云服务器 Ubuntu 18.04.3 LTS InfluxDB 1.7.10 (截至2020.2.20最新版) chonograf 1.8.0 (2020.2.19推出) 配置建议: 不 ...
- Yandex Big Data Essentials Week1 Unix Command Line Interface File System exploration
File System Function In computing, a file system or filesystem is used to control how data is stored ...
- vue 过渡 & 动画
过渡 & 动画 过渡动画 用css先定义好动画效果 .a-enter-active, .a-leave-active { transition: all 1.5s; } .a-enter, . ...
- qt creator源码全方面分析(2-3)
目录 External Tool Specification Files 文件名 位置 文件格式 主要标签 描述标签 可执行规范标签 示例 External Tool Specification Fi ...
- Doug Hennig的自定义 DataEnvironment 和 CursorAdapter 类文件 -- SFDataClasses
Doug Hennig的自定义 DataEnvironment 和 CursorAdapter 类文件 -- SFDataClasses.vcx,其中包括:SFCursorAdapter 和 SFDa ...
- 记一次MySQL中Waiting for table metadata lock的解决方法
最近项目中的数据库查询经常挂起,应用程序启动后也报操作超时.测试人员就说数据库又挂了(貌似他们眼中的连接失败,查询无果都是挂了),通过 show processlist 一看,满屏都是 Waiting ...
- tensorflow开发环境版本组合
记录下各模块的版本 tensorflow 1.15.0 print tf.__version__ cuda 10.0.130 nvcc -v cudnn 7.6.4 ...
- B树(B-树) 、B+树
B树(B-树) 1.B-树(B树)的基本概念B-树中所有结点中孩子结点个数的最大值成为B-树的阶,通常用m表示,从查找效率考虑,一般要求m>=3.一棵m阶B-树或者是一棵空树,或者是满足以下条件 ...
- 复制表结构创建分表 再设置自增ID
CREATE TABLE table_name1 LIKE table_name ALTER TABLE test AUTO_INCREMENT=x
- Re:萌娘百科上的黑幕实现
Re:萌娘百科上的黑幕 说明 本文所有的代码均来自萌娘百科.萌娘百科打钱! 第零段话(我想说的) 这方面不是我的专长,所以有的地方说的不对也请纠正! 我可不是萌娘百科的员工或者管理员或者收了钱 我只是 ...