思路:

解法一:

新的认识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. TN2151:崩溃报告

    understanding and analyzing ios application crashreports 这个TN涉及了与崩溃相关的 内存耗尽信息,堆栈信息 以及 异常编号 等信息 内存耗尽 ...

  2. 重叠I/O之事件通知

      在 Winsock 中,重叠 I/O(Overlapped I/O)模型能达到更佳的系统性能,高于select模型.异步选择和事件选择三种.重叠模型的基本设计原理便是让应用程序使 用一个重叠的数据 ...

  3. Winform Textbox实现滚动条始终在最下面

    在用textbox时,实现一些信息追加时,要使滚动条始终呆在最下面的实现方法. 以textbox1为例,事件TextChanged中执行以下代码即可 private void textBox1_Tex ...

  4. 监听视图树 OnGlobalLayoutListener

    背景 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例: 首先我们写一个控件 public class MyImageView extends ImageView ...

  5. hdu 5124

    bc上的题目,很水,有很多方法做吧,题意大概就是给定你票数,然后让你求出票数最多的那个下标...... 之前我用两个for循环分开写,一个是读入,然后是判断,提交就wa,后来网上看了别人的,就是不能分 ...

  6. 使用DOM进行xml文档的crud(增删改查)操作<操作详解>

    很多朋友对DOM有感冒,这里我花了一些时间写了一个小小的教程,这个能看懂,会操作了,我相信基于DOM的其它API(如JDOM,DOM4J等)一般不会有什么问题. 后附java代码,也可以下载(可点击这 ...

  7. C#基础学习第二天(.net菜鸟的成长之路-零基础到精通)

    1.加号的使用  在我们c#当中,如果想要两个字符串相连接,那么我们可以使用+号连接.  加号两边如果有一边是字符串,那么此时字符串起到了一个连接的作用.  如果加号两遍都是数字,那么加号起到一个相加 ...

  8. QT5-控件-QLabel和QLCDNumber-标签显示图片或者视频,LCD用于上位机不错

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #incl ...

  9. UML中聚合和组合的区别

    聚合 聚合其实是关联的一个特例,表示了两个对象之间“part of(部分-整体)”的关系,是一种弱关联,部分的生命周期可以超越整体.在UML中,用整体端有空心菱形箭头的双向关联修饰聚合.聚合的示例如下 ...

  10. 【转载】Express、Koa、Hapi框架对比

    中文翻译:http://ourjs.com/detail/5490db1c8a34fa320400000e 英文原文:https://www.airpair.com/node.js/posts/nod ...