码迷,mamicode.com
首页 > 其他好文 > 详细

HDU_5532_Almost Sorted Array

时间:2017-10-08 13:00:01      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:i++   eps   miss   limits   ons   ber   ini   mos   etc   

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

标签:i++   eps   miss   limits   ons   ber   ini   mos   etc   

原文地址:http://www.cnblogs.com/edward108/p/7636922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!