HDU_5532_Almost Sorted Array
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 6019 Accepted Submission(s): 1446
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,…,an, is it almost sorted?
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
3
2 1 7
3
3 2 1
5
3 1 4 1 5
YES
NO
- 要求是去掉一个元素使得新数列为非严格单调数列
- 那就正逆跑一遍nlogn的LIS,把判定里的小于号改成小于等于号就可以做到对于非严格单调的要求
- 然后原来算法里面的lower_bound改成upper_bound就行了
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int T, n, ans;
int a[maxn], c[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
while(T--){
ans=;
scanf("%d",&n);
scanf("%d",&a[]);
c[]=a[];
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=c[ans-]){
c[ans++]=a[i];
}else{
c[upper_bound(c,c+ans,a[i])-c]=a[i];
}
}
if(n-ans== || n==ans){
puts("YES");
}else{
ans=;
c[]=a[n];
for(int i=n-;i>;i--)
if(a[i]>=c[ans-]){
c[ans++]=a[i];
}else{
c[upper_bound(c,c+ans,a[i])-c]=a[i];
}
if(n-ans== || n==ans)
puts("YES");
else
puts("NO");
}
}
}
return ;
}
HDU_5532_Almost Sorted Array的更多相关文章
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- 为什么 Windows API 使用 stdcall 调用约定?
作者:知乎用户链接:https://www.zhihu.com/question/31453641/answer/52001143来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- jfinal中Interceptor的使用
一.拦截器是用于对action请求的拦截处理,发生在进入action方法体之前的拦截操作,这样方便了对请求实例做一些文章. 二.自定义.系统已有拦截器都需要实现Interceptor接口,这样才能 ...
- 一款纯css3实现的发光屏幕旋转特效
今天给大家带来一款纯css3实现的发光屏幕旋转特效.该屏幕由纯css3实现带发光旋转特效,效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="s ...
- 应有dataGridView控件
using System.Data.SqlClient; namespace UseDataGridView { public partial class Form1 : Form { public ...
- SpringBoot DataSource 配置说明
DataSource 配置说明 属性 说明 spring.dao.exceptiontranslation.enabled 是否开启PersistenceExceptionTranslationPos ...
- spark 系列文章汇总
源码导读 spark 源码导读1 从spark启动脚本开始 spark 源码导读2 进一步窥探Master.Worker启动及通信机制 spark 源码导读3 进一步理解脚本调用关系 spark 源码 ...
- 下面哪些属于JSTL中的表达式操作标签。(选择1项)
A.<c:out> B.<c:if> C.<c:url> D.<c:catch> 解答:A
- cocos2dx --- 富文本的使用 RichText
在实际工作中,有非常多地方会使用 富文本,这里仅仅介绍最简单的富文本用法: 是由cocostudio 提供的 RichText: 直接贴代码,再分析: //这里測试富文本控件 ui::RichText ...
- Mongodb默认开启与关闭
默认启动: $ ./mongodb 默认数据保存路径:/data/db/ 默认端口:27017 修改默认路径: --dbpath $ ./mongdb --dbpath /mongod ...
- ios开发之--键盘的监听
监听键盘的弹出,让整个页面向上移动,比较简单,仅做记录使用: 代码如下: // 监听键盘 [[NSNotificationCenter defaultCenter] addObserver:self ...