思路:

解法一:

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

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


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

方法二:

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

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

转载于:https://www.cnblogs.com/immortal-worm/p/4960153.html

HDU-5532(LIS-nlogn)相关推荐

  1. HDU 4352 数位dp + LIS(nlogn) + 状态压缩

    #define xhxj (Xin Hang senior sister(学姐))  If you do not know xhxj, then carefully reading the entir ...

  2. Almost Sorted Array HDU - 5532

    Almost Sorted Array HDU - 5532 题意: 如果一个数组的元素"按非递减或非递增顺序排列",那么称这个数组有序.现在,LYD给了你一个数组a,他让你从中选 ...

  3. 最长上升子序列(LIS) nlogn解法

    文章目录 经典DP解法O(n^2) dp+二分法(O(nlogn)) 最长上升子序列LIS:Longest increasing subsequence 题目链接:Leetcode300. 最长递增子 ...

  4. hdu 5087(LIS变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 解题思路:这道题其实仔细想想很简单,次长LIS只有两种可能,一种就是等于LIS-1,一种就是LI ...

  5. hdu 5256 LIS变形

    给一个数列,问最少修改多少个元素使数列严格递增.如果不是要求"严格"递增,那就是求最长不降子序列LIS,然后n-LIS就是答案.要严格递增也好办,输入的时候用每个数减去其下标处理一 ...

  6. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  7. hdu 5748(LIS) Bellovin

    hdu 5748 Peter有一个序列a1,a2,...,ana_1,a_2,...,a_na​1​​,a​2​​,...,a​n​​. 定义F(a1,a2,...,an)=(f1,f2,...,fn ...

  8. LIS (nlogn)的算法

    len[ i ] 代表长度为i的子串的最小结尾的数 #include<stdio.h> #include<algorithm> using namespace std; int ...

  9. HDU 4352 XHXJ's LIS(*数位DP 记忆化搜索 待整理)

    XHXJ's LIS HDU - 4352 #define xhxj (Xin Hang senior sister(学姐))  If you do not know xhxj, then caref ...

  10. 区域赛铜牌专题(一)

    区域赛铜牌专题 题号 题目 知识点 HDU 5532 Almost Sorted Array 贪心,LIS HDU 5533 Dancing Stars on Me HDU 5536 Chip Fac ...

最新文章

  1. Android Dialog 弹出的时候标题栏闪烁一下的处理方法
  2. Python中输出字体的颜色设置
  3. linux远程安装本机软件,我的电脑怎样让对方远程装软件?
  4. MATLAB从入门到精通-APP调用simulink中的参数,并且修改,将结果返回到APP中
  5. 图解机房空调制冷系统
  6. Linux基础——查看IP及port的简单实现
  7. java has a 关系,Java组成(has-a)关系澄清
  8. Mysql Workbench导入Access数据库
  9. 微信头像卡片制作,听说很火?
  10. 计算机平板传输软件,如何在iPad和电脑之间无线传输文件
  11. spring中 allowBeanDefinitionOverriding(spring.main.allow-bean-definition-overriding)原因分析、解决办法
  12. 台式机局域网电脑通过笔记本的无线网络上网
  13. 【科研导向】Outer Product-based Neural Collaborative Filtering (ConvNCF)基于外积的神经协同过滤<论文理解代码分析>
  14. 纯干货!15000 字语法手册分享给你,看完搞懂,再也不担心SQL写不好了
  15. 光纤收发器测试方法大全
  16. 主流视频编码压缩技术基本概念(一)
  17. 备战金九银十,腾讯T4梳理2022年最全999道Java岗必备面试题答案
  18. 投中5G+IoT时代的操作系统,TCL凭什么?
  19. 电信宽带免费提速到200M!不用安装小翼管家!
  20. Bosun安装、配置、部署

热门文章

  1. matlab 罗盘图与羽毛图
  2. 序列信号产生器的verilog HDL 设计
  3. idea 端口被占用
  4. PyCharm入门教程——在编辑器中使用拖放
  5. Exchange/Office365 自动处理脚本:环境准备篇(一)
  6. keepalived介绍和配置
  7. 比特币源码研读(4)数据结构-交易池TransactionPool
  8. Lesson2 Hello,GLSL
  9. 洛谷P1265 公路修建
  10. CXF发布restful WebService的入门例子(服务器端)