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 ...
随机推荐
- stl遍历map
MapInfo* CGameConfig::getMapInfoById( int nId ) { for (map<int, MapInfo>::iterator it = m_mapM ...
- Nginx设置expires设定页面缓存时间 不缓存或一直使用缓存
配置expires expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求 要配置expires,可以在http段中或者server段中或者location段中加入 l ...
- [转]C艹中的各种const总结
Ps: 难免碰到C家族的代码 ,各种const直接搞晕,搜集各种资料备用.... ----------------------------------------------------------- ...
- 关于Context
Context字面意思是上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄.很多方法需要通过 C ...
- spring配置:context:property-placeholder 读取配置文件信息 在配置文件中使用el表达式填充值
spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里. 原本使用PropertyPlaceholderConfigurer类进行文件信息配置.Prope ...
- git学习(二):git config命令
不同的git config操作不同的参数文件 git config --global // 配置用户目录下的.gitconfig那文件 git config --system // 配置系统级配置文件 ...
- beaglebone black ubuntu display x11 server的配置
Change default resolution on BeagleBone modesetting vs fbdev digiteltlc May 7th, 2014, 03:28 PM Hi ...
- Spring.Net框架一:Spring.Net简介
一.Spring.Net简介 Spring.NET为建立企业级应用提供了一套轻量级的解决方案.通过Spring.NET,我们可以用统一且透明的方式来配置应用程序.Spring.NET的重点是为中间层提 ...
- Differential Geometry之第七章曲面的若干整体性质
1.曲面的整体描述 2.整体的Gauss-Bonnet公式 2.1.曲面的三角剖分 2.2.Gauss-Bonnet公式 = 2.3.Gauss-Bonnet定理的应用 2.3.1.切向量场的指数定理 ...
- orm工具的基本思想
orm工具的基本思想无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.2. 由sessionf ...