思路:

解法一:

新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个关系的同时也会出现一个新的关系

解法二:找到非递减和非递增LIS中数量较大的一个,只要它大于等于n-1,答案就是YES,不然就是NO,由于卡时间,故要用nlogn算法


方法一:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int T;
int n,num[];
int big,small;
int flag; int judge(int x)
{
int tsmall = small;
int tbig = big;
if(x == ) {
if(num[x] < num[x+]) tsmall--;
if(num[x] > num[x+]) tbig--;
if(!tbig||!tsmall) return ;
else return ;
}
else if(x == n) {
if(num[x-] < num[x]) tsmall--;
if(num[x-] > num[x]) tbig--;
if(!tbig||!tsmall) return ;
else return ;
}
else {
if(num[x-]>num[x]&&num[x]>num[x+]) tbig -= ;
if(num[x-]<num[x]&&num[x]<num[x+]) tsmall -= ;
if(num[x-]>num[x]&&num[x]<num[x+]||num[x-]<num[x]&&num[x]>num[x+]) {
tbig --;
tsmall --;
}
if(num[x-] < num[x+]) tsmall++;
if(num[x-] > num[x+]) tbig++;
if(!tbig||!tsmall) returl ;
else return ;
}
} int main()
{
scanf("%d",&T);
while(T--)
{
big = small = ;
flag = ;
scanf("%d",&n);
for(int i = ;i <= n;i++) {
scanf("%d",&num[i]);
if(i >= ) {
if(num[i] > num[i-]) small++;
if(num[i] < num[i-]) big++;
}
}
if(n==||!big||!small) {
printf("YES\n");
continue;
}
else {
for(int i = ;i <= n;i++)
if(judge(i)){
flag = ;
break;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
}
return ;
}

方法二:

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x7fffffff
using namespace std; int T;
int n,num[];
int dp1[],dp2[];
int B1[],B2[];
int flag; int max(int a,int b) {
return a>b?a:b;
} int find(int* B,int goal,int r)
{
int l = ;
while(l <= r)
{
int mid = (l+r)/;
if(B[mid] < goal)
l = mid+;
else if(B[mid] > goal)
r = mid-;
else return mid;
}
return l;
} int main()
{
scanf("%d",&T);
while(T--)
{
int len1,len2;
int ans1 = ,ans = ,ans2 = ;
scanf("%d",&n);
for(int i = ;i <= n;i++) {
scanf("%d",&num[i]);
dp1[i] = dp2[i] = ;
}
B1[] = B2[] = num[];
len1 = len2 = ;
for(int i = ;i <= n;i++) {
if(num[i] >= B1[len1]) B1[++len1] = num[i];
else {
int pos = find(B1,num[i],len1);
B1[pos] = num[i];
}
if(num[i] <= B2[len2]) B2[++len2] = num[i];
else {
int pos = find(B2,num[i],len2);
B2[pos] = num[i];
}
}
ans = max(len1,len2);
if(ans >= n-) printf("YES\n");
else printf("NO\n");
}
return ;
}
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

HDU-5532(LIS-nlogn)的更多相关文章

  1. hdu 5532 (LIS) Almost Sorted Array

    http://acm.hdu.edu.cn/showproblem.php?pid=5532 题意大致是一组数中去掉一个数后问剩下的数是否构成非严格单调序列 正反各跑一遍最长非严格连续子序列,存在长度 ...

  2. HDU 1950 LIS(nlogn)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

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

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

  5. HDU 1025 LIS二分优化

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...

  6. LIS(nlogn) POJ 3903 Stock Exchange

    题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...

  7. HDU 1025 (LIS+二分) Constructing Roads In JGShining's Kingdom

    这是最大上升子序列的变形,可并没有LIS那么简单. 需要用到二分查找来优化. 看了别人的代码,给人一种虽不明但觉厉的赶脚 直接复制粘贴了,嘿嘿 原文链接: http://blog.csdn.net/i ...

  8. hdu 5532 Almost Sorted Array

    http://acm.hdu.edu.cn/showproblem.php?pid=5532  题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...

  9. Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. HDU 1950(LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1950 Bridging signals Time Limit: 5000/1000 MS (Java ...

随机推荐

  1. Com原理及應用——Com對象和接口

    1.COM对象的理解 COM对象类似于C++语言中类的概念,类的每个实例代表一个COM对象,它也包括属性(即状态)和方法(即操作),状态反映对象的存在,方法就是接口. 2.COM对象的标识-CLSID ...

  2. Android项目实战--手机卫士18--读取用户的短信内容以及短信备份

    我们今天要说的就是我们手机卫士里面的高级工具里面的短信备份功能啦,其实这个软件备份的功能也很简单,就是把用户的短信读出来,然后写到一个xml或者数据库里面, 但我们这里的是读取到xml里面的. 首先我 ...

  3. struts2,hibernate,spring整合笔记(4)--struts与spring的整合

    饭要一口一口吃,程序也要一步一步写, 很多看起来很复杂的东西最初都是很简单的 下面要整合struts和spring spring就是我们的管家,原来我们费事费神的问题统统扔给她就好了 先写一个测试方法 ...

  4. javascript加载图片获取图片尺寸信息方法

    如果你遇到不方便从服务器取图片尺寸信息的话,用下面代码就很方便了. // 更新: // 05.27: 1.保证回调执行顺序:error > ready > load:2.回调函数this指 ...

  5. Android学习手记(3) Activity间传递数据

    1. 简单数据传递 建立两个Activity,名称分别为MainActivity和TheAty,在MainActivity中新建一个Button,id为btnStartAty.在TheAty中新建一个 ...

  6. Ubuntu桌面版与服务器版有什么不同?

         提到安装Linux,Ubuntu可谓是最受欢迎的.为了满足每个人的需求,出现了不少版本或风格的Ubuntu;其中两项便是桌面版与服务器版.只要发布版本号一致,这两者从核心来说也就是相同的,唯 ...

  7. MySQL 慢查询配置

    MYSQL慢查询 1. 慢查询有什么用? 它能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? 首先 ...

  8. 查看oracle数据库下面的所有的表,执行某个sql脚本:

    查看oracle数据库下面的所有的表: select * from user_tables;//user为用户名 执行某个sql脚本: SQL>@e 文件名.sql

  9. Swift - 36 - 结尾闭包(Trailing closure)和捕获数值(Capturing Values)的简单介绍

    //: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...

  10. jvm参数设置

    -Xss: 栈大小 -Xms:堆初始化大小-Xmx:堆最大大小-XX:NewSize=n:设置伊甸区大小-XX:NewRatio=n:年轻代与年老代比值.如:为3,表示年轻代与年老代比值是1:3,   ...