Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
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
/*********************************************************************/
题意:定义Almost Sorted Array是一个[去掉序列中的一个数,剩下的数满足单调非递减或者单调非递增的]序列,给出一个序列问它是否是Almost Sorted Array。
解题思路:纯粹的模板题,只要你有O(nlogn)复杂度的LIS(最长上升子序列)模板在手,那就不用愁了,没有的还是尽早弄一个吧
至于非递增嘛,把输入的序列反过来保存不就又是求LIS了^_^,机智如我
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
#include
#include
#include
#include
#include
#include
#include
菜鸟成长记