HDU_5532_Almost Sorted Array

2018-06-17 21:48:57来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
 
  • 要求是去掉一个元素使得新数列为非严格单调数列
  • 那就正逆跑一遍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 }

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:hdu 4055--Number String(DP)

下一篇:线段树题解(3题)