题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604
因为deque最后的数列是单调不降的,因此,我们可以枚举数列中的某个中间数Ai,如果从中间数Ai开始,如果后面的要和这个中间数形成单调不降的序列,那么后面的数必须是单调不降或者单调不升的序列,才能进入deque中,因此为两者长度的和,这就是一个LIS的DP。然后枚举的时候从后往前枚举,复杂度O( n*log n)。这里要注意一点,存在相同元素,因此需要减去两个里面出现Ai次数的最小值!
1 //STATUS:C++_AC_281MS_3768KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 using namespace std; 24 //using namespace __gnu_cxx; 25 //define 26 #define pii pair<int,int> 27 #define mem(a,b) memset(a,b,sizeof(a)) 28 #define lson l,mid,rt<<1 29 #define rson mid+1,r,rt<<1|1 30 #define PI acos(-1.0) 31 //typedef 32 typedef __int64 LL; 33 typedef unsigned __int64 ULL; 34 //const 35 const int N=100010; 36 //const LL INF=0x3f3f3f3f; 37 //const int MOD=1000000007,STA=8000010; 38 const LL LNF=1LL<<60; 39 const double EPS=1e-8; 40 const double OO=1e15; 41 const int dx[4]={-1,0,1,0}; 42 const int dy[4]={0,1,0,-1}; 43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 44 //Daily Use ... 45 inline int sign(double x){return (x>EPS)-(x<-EPS);} 46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 49 template<class T> inline T Min(T a,T b){return a<b?a:b;} 50 template<class T> inline T Max(T a,T b){return a>b?a:b;} 51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 55 //End 56 57 int num[N]; 58 int fup[N],fdown[N],dup[N],ddown[N]; 59 int wup[N],wdown[N],cntup[N],cntdown[N]; 60 int upk,downk; 61 int T,n; 62 63 int search_up(int *d,int l,int r,int tar) 64 { 65 int mid; 66 while(l<r){ 67 mid=(l+r)>>1; 68 if(d[mid]<=tar)l=mid+1; 69 else r=mid; 70 } 71 return l; 72 } 73 74 int search_down(int *d,int l,int r,int tar) 75 { 76 int mid; 77 while(l<r){ 78 mid=(l+r)>>1; 79 if(d[mid]>=tar)l=mid+1; 80 else r=mid; 81 } 82 return l; 83 } 84 85 int main() 86 { 87 // freopen("in.txt","r",stdin); 88 int i,j,hig,w; 89 scanf("%d",&T); 90 while(T--) 91 { 92 scanf("%d",&n); 93 for(i=1;i<=n;i++){ 94 scanf("%d",&num[i]); 95 } 96 upk=downk=2; 97 dup[1]=0x7fffffff,ddown[1]=0x80000000; 98 hig=1; 99 for(i=n;i>=1;i--){ 100 w=search_up(dup,1,upk,num[i]); 101 if(w==1 || num[i]!=dup[w-1])cntup[i]=1; 102 else cntup[i]=cntup[wup[w-1]]+1; 103 dup[w]=num[i];wup[w]=i; 104 fup[i]=w; 105 upk=Max(upk,w+1); 106 w=search_down(ddown,1,downk,num[i]); 107 if(w==1 || num[i]!=ddown[w-1])cntdown[i]=1; 108 else cntdown[i]=cntdown[wdown[w-1]]+1; 109 ddown[w]=num[i];wdown[w]=i; 110 fdown[i]=w; 111 downk=Max(downk,w+1); 112 113 hig=Max(hig,fup[i]+fdown[i]-Min(cntup[i],cntdown[i])); 114 } 115 116 printf("%d ",hig); 117 } 118 return 0; 119 }