Almost Sorted Array
problem DescriptionWe is all familiar with sorting algorithms:quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sor T, etc. But sometimes it was an overkill to use these algorithms for an almost sorted array.
We say an array was sorted if its elements be in non-decreasing order or non-increasing order. We say an array are almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now is given an array a1,a2,... ,an, is it almost sorted?
InputThe first line contains an integerTindicating the total number of test cases. Each test case starts with an integerNIn one line, then one line withNIntegersa1,a2,... ,an .
1≤T≤
2≤n≤5
< Span id= "mathjax-span-57" class= "Mrow" >1 ≤ Ai≤105
There is at max Test cases with n> 1000 .
Outputfor Each test case, please output "' YES '" if it is almost sorted. Otherwise, Output "' NO '" (both without quotes).
Sample Input332 1 733 2 153 1 4 1 5
Sample OutputYesyesno
Test Instructions: gives you a sequence of n numbers that allows you to delete a number after which the sequence is strictly non-descending or not ascending
Solving: The first thought is the LIS, can do another is O (N) for each number, either a V-shaped or ^-shaped, or monotonous, record the rise and fall of the number of records, we have to delete a number of the impact of the record, if deleted it caused the last rise or fall the number of times to meet the n-2.
///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a));#defineINF 1000000007inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){ if(ch=='-') f=-1; ch=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************#defineMAXN 100000+5intA[maxn],n;intMain () {intt=read (); while(t--) {scanf ("%d", &n);intflag=0; for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); } intres=0, fall=0; for(intI=1; i<=n;i++){ if(I-1>=1&&a[i]>a[i-1]) res++; Else if(I-1>=1&&a[i]<a[i-1]) fall++; } //cout<<fall<< "" <<res<<endl; for(intI=1; i<=n;i++){ intT1=res,t2=Fall; if(I-1>=1&&a[i]>a[i-1]) {res--; } Else if(I-1>=1&&a[i]<a[i-1]) {Fall--; } if(i+1<=n&&a[i]>a[i+1]) {Fall--; } Else if(i+1<=n&&a[i]<a[i+1]) {res--; } if(I-1>=1&&i+1<=N) { if(a[i-1]<a[i+1]) {res++; }Else if(a[i-1]>a[i+1]) fall++; } if(fall==0|| res==0) {flag=1; Break; } Res=t1;fall=T2; } if(flag) {cout<<"YES"<<Endl; } Elsecout<<"NO"<<Endl; } return 0;}
Code
HDU 5532/2015ACM/ICPC Asia Changchun Station F.almost Sorted Array