标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 236 Accepted Submission(s): 113
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; const int maxn = 100010; const int INF = 0x3f3f3f3f; int a[maxn]; bool jud1(int n){ int tmp = a[2]; for(int i = 2; i <=n; i++){ if(tmp > a[i]){ return false; }else{ tmp = a[i]; } } return true; } bool jud2(int n){ int tmp = a[2]; for(int i = 2; i <= n; i++){ if(tmp < a[i]){ return false; }else{ tmp = a[i]; } } return true; } bool jud3(int n){ a[0] = 0; a[n+1] = INF; int tim = 0; int fir = 0; for(int i = 2; i <= n; i++){ if(a[i]<a[i-1]){ if(fir == 1) return false; fir++; if(a[i+1]>=a[i-1]||a[i]>=a[i-2]){ continue; }else{ return false; } } } return true; } bool jud4(int n){ a[0] = INF; a[n+1] = 0; int tim = 0; int fir = 0; for(int i = 2; i <= n; i++){ if(a[i]>a[i-1]){ if(fir == 1) return false; fir++; if(a[i+1]<=a[i-1]||a[i]<=a[i-2]){ continue; }else{ return false; } } } return true; } int main(){ int T,n,m; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); } if(jud1(n)){ puts("YES"); continue; } if(jud2(n)){ puts("YES"); continue; } if(jud3(n)){ puts("YES"); continue; } if(jud4(n)){ puts("YES"); continue; } puts("NO"); } return 0; } /* 555 4 8 6 7 8 4 8 6 7 7 5 1 2 1000 7 8 5 1 2 100 2 3 4 1 1 1 1 6 7 8 1 1 1 10 */
HDU 5532——Almost Sorted Array——————【技巧】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4930092.html