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

Problem Description
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an 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,…,an, is it almost sorted?

 
Input
The first line contains an integer T indicating the total number of test cases. Each test case starts with an integer n in one line, then one line with n integers a1,a2,…,an.

1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.

 
Output
For 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
 
Source
 
  • 要求是去掉一个元素使得新数列为非严格单调数列
  • 那就正逆跑一遍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的更多相关文章

  1. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. [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 ...

  4. [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. 这道 ...

  5. [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 ...

  6. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  7. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. [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 ...

  9. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. CCFollow和ActionCallFunc

    CCFollow动作,可以让一个节点跟随另一个节点做位移. CCFollow经常用来设置layer跟随sprite,可以实现类似摄像机跟拍的效果 效果是精灵在地图上移动,地图也会跟着移动,但是精灵仍然 ...

  2. linux下挂载win7的共享文件夹

    由于跨平台开发的需要,需要在Linux和windows之间共享文件夹,所以找了一下方法,我试验了两种都可以使用. 首先声明一下我使用的是VMware10.CentOS6.2 一.手动操作 1.按照下图 ...

  3. sql 记录

    INSERT INTO B([name],[info]) SELECT [name,'10'] FROM A 级联更新1:update tb1, tb2 set tb1.a=tb2.a,tb1.b=t ...

  4. LinQ的简单使用

    1.LinQ to Sql类(NET Language Integrated Query (LINQ) ) LINQ定义了大约40个查询操作符,如select.from.in.where以及order ...

  5. [Kernel]理解System call系统调用

    转自:http://os.51cto.com/art/200512/13510.htm 现在,您或许正在查看设备驱动程序,并感到奇怪:“函数 foo_read() 是如何被调用的?”或者可能疑惑: “ ...

  6. Mybatis Batch 批量操作

    Mybatis Batch 批量操作 http://www.blogjava.net/diggbag/articles/mybatis.html

  7. elasticsearch安装与使用(2)-- centos7 安装测试的集群工具elasticsearch head

    elasticsearch-head是elasticsearch(下面称ES)比较普遍使用的可监控.测试等功能的集群管理工具,是由H5编写的单独的网页程序.使用方法网上很多,这里教大家一个超简单安装h ...

  8. css实现圆角三角形例子(无图片)

    css实现圆角三角形例子(无图片) 以前我们做圆角时都会要用到图片来实现,今天我给大家整理的这款css圆角效果是不需要图片的纯css实现的哦,下面我们一起来看看吧. 效果如下图所示 三角形所对方向&q ...

  9. hadoop杂记-为什么会有Map-reduce v2 (Yarn)

    转自:http://www.cnblogs.com/LeftNotEasy/archive/2012/02/18/why-yarn.html 前言: 有一段时间没有写博客了(发现这是我博客最常见的开头 ...

  10. linux -- 查看ip,路由,dns

    查看ip地址:ifconfig 查看gateway:route 查看dns:nm-tool