题目大意:给一个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. JS随鼠标坐标移动,显示浮动层内容

    在表单等项目中往往会遇到类似于“备注”.“说明”等100个字内的内容需要显示. 这些内容如果全部呈现开,会影响布局和美观,确又没有必要设计一个层或是一个页面. 那么,我们可以把这些内容放到浮动层中,鼠 ...

  2. git——学习笔记(二)远程仓库

    GIT杀手锏之一——远程仓库 拥有远程仓库的两个办法 1:搭一个Git服务器  2:在GitHub上免费托管的Git仓库 本地仓库   远程仓库 一.在GitHub上免费托管的Git仓库 电脑: 1. ...

  3. OkHttp+Stetho+Chrome调试android网络部分

    如果要达到上面的效果,你需要改造你的网络请求模块,使用Chrome浏览器和android程序之间的中间件来连接,这就是本篇要介绍的主题:OkHttp+Stetho+Chrome进行网络调试. okht ...

  4. Bochs使用说明

    简介 关于Bochs的介绍:http://en.wikipedia.org/wiki/Bochs Bochs的官网:http://bochs.sourceforge.net/ 这里记录如何在Windo ...

  5. 关于Android Studio里的Gradle,你所需要知道的都在这里了

    Gradle介绍 Gradle是一个先进的build toolkit,可以方便的管理依赖包和定义自己的build逻辑.到底有多先进,Android Studio官方集成Gradle,Google还专门 ...

  6. [DP]数位DP总结

     数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step   http://blog.csdn.net/dslovemz/article/details/ ...

  7. C#静态类和静态成员

    1. 静态类 1.1 简介  静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数. 静态类成员可用于分离独立于任何对象标识的数据和行为:无论对象发生什么更改,这些数据和函数都不会随之变化. ...

  8. exit(-1)或者return(-1)为什么shell得到的退出码是255?

    写一段hello world: // filename: main.c #include <stdio.h> int main(void) { printf("hello wol ...

  9. Ubuntu安装文泉驿-微米黑字体

    sudo apt-get install ttf-wqy-microhei #文泉驿-微米黑

  10. HDU 1002 A + B Problem II(AC代码)

    #include <stdio.h> #include <string.h> #define MAX 1009 int main() { },b[MAX]={}; ,z=,r= ...