The main idea: to give a sequence of n numbers, ask if the sequence is ordered by deleting a number.
Title Analysis: Find the longest ascending subsequence and the longest descending sub-sequence, as long as there is a length of not less than n-1 can.
The code is as follows:
# include<iostream># include<cstdio># include<cmath># include<vector># include<list># include<queue># include<map># include<set># include<cstring># include<algorithm> using namespace Std;const int n=100005;int n;int a[n+5];int dp[n+5];int d[n+5];int f (int l,int R,int x) {while (l<r) {int mid=l+ (r-l)/2;if (d[mid]<=x) L=mid+1;elser=mid;} return l;} int F1 (int l,int r,int x) {while (l<r) {int mid=l+ (r-l)/2;if (d[mid]>=x) L=mid+1;elser=mid;} return l;} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (int i=0;i<n;++i) scanf ("%d", a+i);d [0]=a[0 ];int cnt=0;int len=1;for (int i=1;i<n;++i) {if (a[i]>=d[len-1]) D[len++]=a[i];else{int pos=f (0,len,a[i]);d [POS] =a[i];}} if (len>=n-1) ++cnt;d[0]=a[0];len=1;for (int i=1;i<n;++i) {if (a[i]<=d[len-1]) {d[len++]=a[i];} Else{int pos=f1 (0,len,a[i]);d [pos]=a[i];}} if (len>=n-1) ++cnt;if (cnt==0) printf ("no\n"); elseprintf ("yes\n");} return 0;}
HDU-5532 almost Sorted Array (LIS)