HDU_5532_Almost Sorted Array

Posted edward108

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU_5532_Almost Sorted Array相关的知识,希望对你有一定的参考价值。

Almost Sorted Array

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 6019    Accepted Submission(s): 1446


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.

1T2000
2n105
1ai105
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
 

 

Source
 
  • 要求是去掉一个元素使得新数列为非严格单调数列
  • 那就正逆跑一遍nlogn的LIS,把判定里的小于号改成小于等于号就可以做到对于非严格单调的要求
  • 然后原来算法里面的lower_bound改成upper_bound就行了

 

 

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <cmath>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long           LL ;
15 typedef unsigned long long ULL ;
16 const int    maxn = 1e5 + 10   ;
17 const int    inf  = 0x3f3f3f3f ;
18 const int    npos = -1         ;
19 const int    mod  = 1e9 + 7    ;
20 const int    mxx  = 100 + 5    ;
21 const double eps  = 1e-6       ;
22 const double PI   = acos(-1.0) ;
23 
24 int T, n, ans;
25 int a[maxn], c[maxn];
26 int main(){
27     // freopen("in.txt","r",stdin);
28     // freopen("out.txt","w",stdout);
29     while(~scanf("%d",&T)){
30         while(T--){
31             ans=1;
32             scanf("%d",&n);
33             scanf("%d",&a[1]);
34             c[0]=a[1];
35             for(int i=2;i<=n;i++){
36                 scanf("%d",&a[i]);
37                 if(a[i]>=c[ans-1]){
38                     c[ans++]=a[i];
39                 }else{
40                     c[upper_bound(c,c+ans,a[i])-c]=a[i];
41                 }
42             }
43             if(n-ans==1 || n==ans){
44                 puts("YES");
45             }else{
46                 ans=1;
47                 c[0]=a[n];
48                 for(int i=n-1;i>0;i--)
49                     if(a[i]>=c[ans-1]){
50                         c[ans++]=a[i];
51                     }else{
52                         c[upper_bound(c,c+ans,a[i])-c]=a[i];
53                     }
54                 if(n-ans==1 || n==ans)
55                     puts("YES");
56                 else
57                     puts("NO");
58             }
59         }
60     }
61     return 0;
62 }

 

 

 

以上是关于HDU_5532_Almost Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

hdu 5532 Almost Sorted Array (水题)

HDU 5532 Almost Sorted Array

Almost Sorted Array HDU - 5532

Almost Sorted Array---hdu5532(j简单dp)

HDU 5532 Almost Sorted Array(最长不下降或不上升子序列)

HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)