F - Almost Sorted Array

 
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,…,ana1,a2,…,an, is it almost sorted?

InputThe first line contains an integer TT indicating the total number of test cases. Each test case starts with an integer nn in one line, then one line with nn integers a1,a2,…,ana1,a2,…,an.

1≤T≤20001≤T≤2000 
2≤n≤1052≤n≤105 
1≤ai≤1051≤ai≤105 
There are at most 20 test cases with n>1000n>1000.OutputFor 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
//需要用到的算法:最长递增子序列

#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm> #define INF 0x3f3f3f using namespace std; const int MAXN = 1e5 + ; int dp[MAXN];
int a[MAXN];
int b[MAXN]; int main() {
int T;
cin >> T;
while(T--) {
int n;
scanf("%d", &n);
memset(dp, INF, sizeof dp);
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
b[n - i - ] = a[i];
}
//递增
//依次遍历,将当前元素插入dp数字中适当的位置
for(int i = ; i < n; i++) {
*upper_bound(dp, dp + n, a[i]) = a[i];
}
//求出最长递增子序列的长度
int cnt1 = lower_bound(dp, dp + n, INF) - dp;
//递减
memset(dp, INF, sizeof dp);
for(int i = ; i < n; i++) {
*upper_bound(dp, dp + n, b[i]) = b[i];
}
int cnt2 = lower_bound(dp, dp + n, INF) - dp;
//如果最后递增和递减的长度其中有一个大于或等于(n-1)的话,就输出YES,否则输出NO
if(cnt1 >= (n - ) || cnt2 >= (n - )) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return ;
}
												

F - Almost Sorted Array的更多相关文章

  1. HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~

    F - Almost Sorted Array Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

    Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...

  3. 2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

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

  4. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  5. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

  6. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Codeforces 1201D. Treasure Hunting

    传送门 看一眼感觉就是 $dp$,但是似乎状态太多了 考虑推推性质 首先每到一行都要把所有宝藏都走到,那么一定会走到最左边的和最右边的宝藏 注意到一旦走完所有宝藏时肯定是在最左边或者最右边的宝藏位置 ...

  2. JsonObject、JsonArray操作json的个人总结

    介绍 JsonObject.JsonArray之前,先介绍下JsonConfig JsonConfig: setClassMap(Map classMap)设置json属性类型,上json里的其中值为 ...

  3. 从0开始入门ssm-crm系统实战

    喜欢就点个赞呗! GitHub项目ssm-learn-crm show me the code and take to me,做的出来更要说的明白 1.1 克隆 git clone https://g ...

  4. leetcode 1051. Height Checker

    Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minim ...

  5. LVS、Nginx及HAProxy

    本文转载自 linkedkeeper.com   当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务,这些集群可以是 Web 应用服务器集群, ...

  6. 微信小程序wx.showActionSheet调用客服信息功能

    微信小程序wx.showActionSheet调用客服消息功能 官方文档的代码: wx.showActionSheet({ itemList: ['A', 'B', 'C'], success (re ...

  7. 转载Google TPU论文

    选自 Google Drive 作者:Norman P. Jouppi 等 痴笑@矽说 编译 该论文将正式发表于 ISCA 2017 从去年七月起,Google就号称了其面向深度学习的专用集成电路(A ...

  8. python删除数组中元素

    有数组a,要求去掉a所有为0的元素 a = [2,4,0,8,9,10,100,0,9,7] Filter a= filter(None, a) Lambada a = filter(lambda x ...

  9. JS实现table表格在鼠标移动出现一行变色或者十字叉变色

    1,一行变色 <script> function trBg(){ var tab=document.getElementById("table"); var tr=ta ...

  10. 18、远程管理卡、戴尔划RAid

    1.配置远程管理卡: 第一个iDRAC6是远程控制卡名称 第三个LAN ...是配置远程控制卡的 下图配置结束按Esc退出 最后按Esc保存退出 2.windows配置远程管理卡: 把下载下来的软件安 ...