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


php

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 }
相關文章
相關標籤/搜索