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

题意:给你一组数,能否删除一个后使他成为升序或者降序

思路:

正反分别求一次最长上升子序列,只要长度有一次≥n-1即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5;
int b[maxn];
int n;
int Search(int num,int low,int high)
{
int mid;
while(low <= high)
{
mid = (low+high)/2;
if(num >= b[mid])
low= mid+1;
else
high = mid-1;
}
return low;
} int fin(int *a)
{
int len,pos;
b[1] = a[1];
len = 1;
for(int i = 2;i <= n;i++)
{
if(a[i] >= b[len])
{
len = len+1;
b[len] = a[i];
}
else
{
pos = Search(a[i],1,len);
b[pos ] = a[i];
}
}
if(len >= n-1)
return true;
else
return false;
} int t[maxn],tt[maxn]; int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%d",t+i);
for(int i = 1;i <= n;i++)
tt[n+1-i] = t[i];
bool flag = fin(t);
flag |= fin(tt);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}

  

hdu 5532(最长上升子序列)的更多相关文章

  1. hdu 1950 最长上升子序列(lis) nlogn算法【dp】

    这个博客说的已经很好了.http://blog.csdn.net/shuangde800/article/details/7474903 简单记录一下自己学的: 问题就是求一个数列最长上升子序列的长度 ...

  2. HDU 5773 最长上升子序列

    题意 给出一个序列 问它的最长严格上升子序列多长 这个序列中的0可以被替代为任何数 n的范围给出了1e5 所以平常的O(n*n)lis不能用了 在kuangbin的模板里有O(nlogn)的模板 套上 ...

  3. hdu 1950 最长上升子序列

    //Accepted 3540 KB 62 ms //dp 最长上升子序列 #include <cstdio> #include <cstring> #include < ...

  4. HDU 5748 最长上升子序列的长度nlogn(固定尾部)

    Bellovin Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  5. HDU 1025(最长上升子序列)

    题意是要在两条平行线间连点,要在线不交叉的前提下尽可能多的连线,问最多能连多少条线. 现假定题中所给的是 9 组点,分别是:1—3,2—8,3—5,4—9,5—2,6—4,7—6,8—7,9—1,如图 ...

  6. HDU 1159 最长公共子序列(n*m)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. HDU 1159 最长公共子序列

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. hdu 5773 最长递增子序列 (nlogn)+贪心

    The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. HDU - 1503 最长公共子序列记录路径

    题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...

  10. HDU - 1160 最长上升子序列以及记录路径

    题意:第一列,给出老鼠的重量,第二列,给出老鼠的速度,要证明老鼠的重量越大,速度越小,给出最多老鼠的数量,并说明第几只. 思路:先将老鼠按照重量从大到小排序,然后速度是从小到大,求最长上升子序列,学习 ...

随机推荐

  1. 20145237 《Java程序设计》第2周学习总结

    教材学习内容总结 本周我学习了java的基础语法.分为类型.变量与运算符,流程控制. 一.类型:1.Java可以区分为基本类型和类类型.类类型也称作参考类型.2.Java中基本类型主要是整数.字节.浮 ...

  2. 【iOS】swift 74个Swift标准库函数

    本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swift standard li ...

  3. SpringMVC 无法访问到指定jsp页面可能的原因

    当出现你的程序可以访问到对应的controller层.但是却无法访问对应的jsp文件时.你首先做的不是检查web.xml等配置文件,而是打开的服务器根文件检查对应路径下的文件是否存在.命名是否正确.命 ...

  4. php后台的在控制器中就可以实现阅读数增加

    $smodel=M('Sswz');$smodel->where($map)->setInc('view' ,1);php后台的在控制器中就可以实现阅读数增加前台不需要传值

  5. 1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    解决问题:windows下:修改my.ini 在[mysqld]内加入secure_file_priv = linux下:修改my.cnf 在[mysqld]内加入secure_file_priv = ...

  6. AssemblyExecuteAdapter

    BizTalk custom adapter AssemblyExecuteAdapter 功能 更为方便的扩展BizTalk custom adapter 的交互方式,只需要实现IAssemblyE ...

  7. Let's Encrypt,站点加密之旅

    HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入 ...

  8. fedora27安装DB2 Express-C 11

    首先在官网下载对应的安装包和语言包两个文件. 然后通过tar -zxvf命令将下载的两个文件解压. 其中一个文件解压后是名为expc的文件,进入这个文件.里面有一个名为db2setup的文件. 在命令 ...

  9. iOS之AFSecurityPolicy

    AFSecurityPolicy是AFNetworking中负责对https请求进行证书验证的模块,本文主要是要搞清楚它是如何工作的. 在介绍AFSecurityPolicy之前,我们先来了解一下ht ...

  10. python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)、多态、封装的特性property

    一.面向对象绑定方法 一.类中定义的函数分成两大类 1.绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 ...