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 ...
随机推荐
- k8s系列---资源指标API及自定义指标API
不得不说千万不要随意更改版本,我用的1.13的版本,然后学到这一步时,还因yaml文件不同,卡住了很久,然后各种google才找到解决办法 https://www.linuxea.com/2112. ...
- QT学习之路-QT服务器-mysql数据库相关问题集锦(1)
时间:2017-04-07 异常信息: Error - RtlWerpReportException failed with status code :-1073741823. Will try to ...
- Solr系列4-SolrJ开发应用
1: Solr导入 1.1导入POM # Base Code Java org.apache.solr solr-solrj 8.4.0 # spring boot org.springframewo ...
- 如何用apply实现一个bind?
面试题:如何用apply实现一个bind? Function.prototype._bind = function(target) { // 保留调用_bind方法的对象 let _this = th ...
- HA: Chanakya Vulnhub Walkthrough
靶机链接: https://www.vulnhub.com/entry/ha-chanakya,395/ 网络主机探测: 主机端口扫描: nmap -p- -sC -sV 10.10.202.136 ...
- SDMask(iOS蒙层遮罩弹出引导)
SDMask介绍 地址 针对iOS项目,大部分弹出视图三方都把弹出内容作为了项目的一部分,这种耦合局限性较大.该项目对此解耦,围绕我何时需要使用蒙层而展开设计.将弹出内容和动画和事件完全分离出去让co ...
- Blazor client-side + webapi (.net core 3.1) 添加jwt验证流程(非host)第三章 客户端存储及验证
准备工作: 安装Nuget包:Blazored.LocalStorge. 这是一个client-side 浏览器存储库,找了非常久. 官方文档中也有一款Microsoft.AspNetCore.Pro ...
- textarea输入文字限制个数
说明: w-count固定为数字部分的class textarea-active为超出最大输入文字个数报错信息的class html 部分: <div class="wrap wrap ...
- Android中动态改变Listview中字体的颜色
效果如下: 账目显示用的是Listview,要实现的功能为使其根据所在Item是“收入”还是“支出”来把数字设置成绿色或红色 方法是自定义适配器,并重写其中getView()函数,实现如下: //自定 ...
- opencv —— Laplacian 拉普拉斯算子、二阶导数用于边缘检测
Laplacian 算子简介 求多元函数的二阶导数的映射又称为 Laplacian 算子: 计算拉普拉斯变换:Laplacian 函数 void Laplacian(InputArray src, ...