题目大意:给一个n个数的序列,问这个序列删掉一个数后是否有序。

题目分析:找最长上升子序列和最长下降子序列,只要有一个的长度不小于n-1即可。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<vector>
# include<list>
# include<queue>
# include<map>
# include<set>
# include<cstring>
# include<algorithm>
using namespace std; const int N=100005; int n;
int a[N+5];
int dp[N+5];
int d[N+5]; int f(int l,int r,int x)
{
while(l<r)
{
int mid=l+(r-l)/2;
if(d[mid]<=x)
l=mid+1;
else
r=mid;
}
return l;
} int f1(int l,int r,int x)
{
while(l<r)
{
int mid=l+(r-l)/2;
if(d[mid]>=x)
l=mid+1;
else
r=mid;
}
return l;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%d",a+i);
d[0]=a[0];
int cnt=0;
int len=1;
for(int i=1;i<n;++i){
if(a[i]>=d[len-1])
d[len++]=a[i];
else{
int pos=f(0,len,a[i]);
d[pos]=a[i];
}
}
if(len>=n-1)
++cnt;
d[0]=a[0];
len=1;
for(int i=1;i<n;++i){
if(a[i]<=d[len-1]){
d[len++]=a[i];
}else{
int pos=f1(0,len,a[i]);
d[pos]=a[i];
}
}
if(len>=n-1)
++cnt;
if(cnt==0)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}

  

HDU-5532 Almost Sorted Array (LIS)的更多相关文章

  1. hdu 5532 Almost Sorted Array(模拟)

    Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, ...

  2. HDU - 5532 Almost Sorted Array (最长非严格单调子序列)

    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...

  3. HDU 5532 Almost Sorted Array (最长非递减子序列)

    题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...

  4. leetcode 之Search in Rotated Sorted Array(四)

    描述 Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?    Would this aff ...

  5. HDU 5532——Almost Sorted Array——————【技巧】

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  6. Remove Duplicates from Sorted Array(参考)

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

  7. hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  8. LeetCode 之 Merge Sorted Array(排序)

    [问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  9. HDU 1257 最少拦截系统 ——(LIS)

    想了一下感觉和lis有关,交了果然AC.想不到很好的证明方法,试做证明如下:lis的每一个点都是一个不上升系统中的一员,设其为a[i],那么a[i-1]<a[i]肯定是成立的(lis的性质),夹 ...

随机推荐

  1. Android 之 数据存储

    在Android操作系统中,提供了5种数据存储方式:SharedPreferences存储,文件存储,SQLite数据库存储,ContentProvider存储和网络存储. 一.SharedPrefe ...

  2. my class 2.0

    www.dropbox.com www.google.com/voice www.prezi.com www.evernote.com

  3. C# Delete Url Cookie

    public static void DeleteCookieFile(Uri url) { string path = Environment.GetFolderPath(Environment.S ...

  4. 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解

    本文主要讲的是<天龙八部>游戏中水面(TerrainLiquid)的具体实现,使用C++,Ogre1.6. 天龙的水面做的比较简单,虽然没有倒影,但动态纹理+深度图做出的效果还行,看着不是 ...

  5. 利用DetachedCriteria实现模糊查询和分页

      分类: Java-Developing  前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单 ...

  6. Iterator之java.util.ConcurrentModificationException

    在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ...

  7. 通过AssetsLibrary框架访问所有相片

    该框架下有几个类,ALAssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation. ALAssetsLibrary ...

  8. Squid Proxy Server 3.1

    Improve the performance of your network using the caching and access control capabilitiess of squid. ...

  9. BZOJ 3531(树链剖分+线段树)

    Problem 旅行 (BZOJ 3531) 题目大意 给定一颗树,树上的每个点有两个权值(x,y). 要求维护4种操作: 操作1:更改某个点的权值x. 操作2:更改某个点的权值y. 操作3:求a-- ...

  10. C#指针操作Marshal实例

    static void Main(string[] args) { ,,,}; ,,,}; IntPtr pt = Marshal.AllocHGlobal(a.Length); //从source数 ...