HDU - 5532 Almost Sorted Array (最长非严格单调子序列)
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,…,ana1,a2,…,an, is it almost sorted?
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,…,ana1,a2,…,an, is it almost sorted?
InputThe first line contains an integer TT indicating the total number of test cases. Each test case starts with an integer nn in one line, then one line with nn integers a1,a2,…,ana1,a2,…,an.
1≤T≤20001≤T≤2000
2≤n≤1052≤n≤105
1≤ai≤1051≤ai≤105
There are at most 20 test cases with n>1000n>1000.OutputFor 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
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 const int inf=1e9+7; 7 const int maxn=1e5+10; 8 using namespace std; 9 int a[maxn],b[maxn],c[maxn]; 10 int n; 11 int binary_search(int x){ //二分搜索 12 int l=1,r=n; 13 while(l<r){ 14 int mid=(l+r)>>1; 15 if(b[mid]>x) r=mid; 16 else l=mid+1; 17 } 18 return l; 19 } 20 bool LIS(const int *a){ //LIS 21 memset(b,inf,sizeof(b)); 22 b[0]=0; 23 int len=0; 24 for(int i=1; i<=n; ++i){ 25 int k=binary_search(a[i]);//i位置的LIS为idx 26 len=max(k,len); 27 b[k]=a[i]; 28 } 29 if(len>=n-1) return true; //满足条件即可 30 return false; 31 } 32 int main(){ 33 int t;scanf("%d",&t); 34 while(t--){ 35 scanf("%d",&n); 36 for(int i=1; i<=n; ++i){ 37 scanf("%d",&a[i]); 38 c[n-i+1]=a[i]; 39 } 40 if(LIS(a)||LIS(c)) puts("YES"); 41 else puts("NO"); 42 } 43 return 0; 44 }
另一种:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cmath> 5 #include <vector> 6 #include <queue> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 typedef long long ll; 12 typedef unsigned long long ull; 13 #define MM(a,b) memset(a,b,sizeof(a)); 14 #define inf 0x7f7f7f7f 15 #define FOR(i,n) for(int i=1;i<=n;i++) 16 #define CT continue; 17 #define PF printf 18 #define SC scanf 19 const int mod=1000000007; 20 const int N=100000+10; 21 ull seed=13331; 22 23 int dp[N],ans[N],a[N]; 24 int main() 25 { 26 int cas; 27 scanf("%d",&cas); 28 while(cas--){ 29 int n;scanf("%d",&n); 30 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 31 int len=1,flag1=0,flag2=0; 32 ans[1]=a[1]; 33 for(int i=2;i<=n;i++) 34 { 35 if(a[i]>=ans[len]) {len++;ans[len]=a[i];} 36 else{ 37 int pos=upper_bound(ans+1,ans+len,a[i])-ans; 38 ans[pos]=a[i]; 39 } 40 } 41 if(len>=n-1) flag1=1; 42 len=1;ans[1]=a[n]; 43 for(int i=n-1;i>=1;i--) 44 { 45 if(a[i]>=ans[len]) {len++;ans[len]=a[i];} 46 else{ 47 int pos=upper_bound(ans+1,ans+len,a[i])-ans; 48 ans[pos]=a[i]; 49 } 50 } 51 if(len>=n-1) flag2=1; 52 if(flag1||flag2) printf("YES\n"); 53 else printf("NO\n"); 54 } 55 return 0; 56 }
分析:严格单调递增子序列就是lower_bound(),直接进行替换;非严格单调子序列就是upper_bound();
1 if(a[i]>=ans[len]) { 2 len++; 3 ans[len]=a[i]; 4 } 5 else{ 6 int pos=upper_bound(ans+1,ans+len,a[i])-ans;//非严格单调 7 ans[pos]=a[i]; 8 }