题意:给定一个n个数的数列,问删掉一个数之后剩余部分是否可以单调不增或单调不减
n<=1e5,a[i]<=1e5
思路:预处理一下前后缀是否合法
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 210000 21 #define M 1100 22 #define MOD 2147493647 23 #define eps 1e-8 24 #define pi acos(-1) 25 26 int a[N],b[N],c[N],k; 27 28 int main() 29 { 30 //freopen("F.in","r",stdin); 31 //freopen("F.out","w",stdout); 32 int cas; 33 scanf("%d",&cas); 34 for(int v=1;v<=cas;v++) 35 { 36 k=0; 37 int n; 38 scanf("%d",&n); 39 for(int i=1;i<=n;i++) 40 { 41 scanf("%d",&a[i]); 42 b[i]=c[i]=0; 43 } 44 b[1]=1; 45 for(int i=2;i<=n;i++) 46 if(b[i-1]==1&&a[i]>=a[i-1]) b[i]=1; 47 else c[i]=0; 48 c[n]=1; 49 for(int i=n-1;i>=1;i--) 50 if(c[i+1]==1&&a[i]<=a[i+1]) c[i]=1; 51 else c[i]=0; 52 int flag=0; 53 //for(int i=1;i<=n;i++) printf("%d ",b[i]); 54 // printf(" "); 55 //for(int i=1;i<=n;i++) printf("%d ",c[i]); 56 // printf(" "); 57 for(int i=1;i<=n;i++) 58 { 59 if(i>1&&b[i-1]==0) continue; 60 if(i<n&&c[i+1]==0) continue; 61 if(i>1&&i<n&&a[i-1]>a[i+1]) continue; 62 flag=1; k=i; 63 break; 64 } 65 // printf("%d ",k); 66 for(int i=1;i<=n;i++) b[i]=c[i]=0; 67 b[1]=1; 68 for(int i=2;i<=n;i++) 69 if(b[i-1]==1&&a[i]<=a[i-1]) b[i]=1; 70 else break; 71 c[n]=1; 72 for(int i=n-1;i>=1;i--) 73 if(c[i+1]==1&&a[i]>=a[i+1]) c[i]=1; 74 else break; 75 for(int i=1;i<=n;i++) 76 { 77 if(i>1&&b[i-1]==0) continue; 78 if(i<n&&c[i+1]==0) continue; 79 if(i>1&&i<n&&a[i-1]<a[i+1]) continue; 80 flag=1; k=i; 81 break; 82 } 83 // printf("%d ",k); 84 if(flag) printf("YES "); 85 else printf("NO "); 86 } 87 return 0; 88 }