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 ...
随机推荐
- ubuntu docker中crontab任务不执行的问题
problem of task of crontab in docker of ubuntu do not working! 由于各种原因,要在Ubuntu docker上部署crontab任务,如 ...
- 网络安全初级实战笔记(一):owasp zap 暴力破解
网络安全里装着好多人的侠客梦.但是不能触碰铁律,所以,只小小的自娱自乐. 自己练习,大都会用到DVWA,一个很好的安全测试平台,自己搭建(很简单,傻瓜式搭建),自己设置安全级别,自己验证各种漏洞攻击方 ...
- javascript检测客户端环境是否是pc端
//isPC(): 检测客户端环境是否是pc端 function isPC(){ let userAgent = navigator.userAgent; let agents = ["An ...
- Kubernetes最新版核心命令
#查看所有namespace的pods运行情况 kubectl get pods --all-namespaces #查看具体pods,记得后边跟namespace名字哦 kubectl get po ...
- ansible----sudo
ansible 执行sudo的root命令,参看https://www.cnblogs.com/infaaf/p/10049896.html [nnn]103 ansible_ssh_host=10. ...
- NIO学习笔记,从Linux IO演化模型到Netty—— 究竟如何理解同步、异步、阻塞、非阻塞
我的观点 首先,分开各自理解. 1. 同步:描述两个(或者多个)个体之间的协调关系. 比如,单线程中,methodA调用了methodB,methodB返回后,methodA才往下执行,那么称A同步调 ...
- git报错:fatal: No configured push destination.
本地仓库代码(git push)上传git仓库报错: fatal: No configured push destination. Either specify the URL from the co ...
- 解决React路由URL中hash(#)部分的显示 、browserHistory打包后浏览器刷新页面出现404的问题
摘要 在React项目中,我们需要采用它的路由库React-Router来进行页面跳转,React会根据路由URL来判断是哪个页面.常见的的URL有两种显示方式,一种是hashHistory的形式,形 ...
- RadioButton改写的开关按钮
先上效果图: 这个控件其实是俩个RadioButton,通过样式就可以实现. 样式资源: <Style x:Key="Tong_Yong_RadioButtonStyle" ...
- 教你用python爬虫监控教务系统,查成绩快人一步!
教你用python爬虫监控教务系统,查成绩快人一步!这几天考了大大小小几门课,教务系统又没有成绩通知功能,为了急切想知道自己挂了多少门,于是我写下这个脚本. 设计思路:设计思路很简单,首先对已有的成绩 ...