Title: Click here
Test instructions: The length of the longest continuous ascending subsequence after a given sequence changes one of the elements
Analysis: The longest continuous subsequence has 2 kinds, one is the length of the strict ascent (without changing the element) plus 1, one is the combination of two periods of strict rise. 、
1#include <bits/stdc++.h>2 using namespacestd;3 #defineF First4 #defineS Second5 #definePB Push_back6 #definePower (a) ((a) * (a))7 #defineENTER printf ("\ n");8typedef unsignedLong Longll;9 Const intINF =0x3f3f3f3f;Ten Const DoubleEPS = 1e-8; One Const intM = 1e5+5; A - intN; - intA[m]; the intDPS[M];//Dps[i] Save longest continuous ascending subsequence starting with I - intDPE[M];//Dpe[i] Save longest continuous ascending subsequence ending with I - voidsolve () { -dpe[0] =1; +dps[n-1] =1; - intRET =1; + for(intI=1; i<n; i++ ) { A if(A[i] > a[i-1]) Dpe[i] = dpe[i-1] +1; at ElseDpe[i] =1; -RET =Max (ret, dps[i]); - } - for(inti=n-2; i>=0; i-- ) { - if(A[i] < a[i+1]) Dps[i] = dps[i+1] +1; - ElseDps[i] =1; inRET =Max (ret, dps[i]); - } to for(intI=0; i<n; i++ ) { + if(i = =0 ) -RET = max (ret, dps[i]+1 ); the Else if(i = = n1 ) *RET = max (ret, dpe[i]+1 ); $ Else {Panax Notoginseng if(a[i+1]-a[i-1] >=2 ) -RET = max (ret, dps[i+1]+dpe[i-1]+1 ); the Else +RET = max (ret, Max (dps[i+1]+1, dpe[i-1]+1 ) ); A } the } +printf"%d\n", Ret>n?n:ret);//The maximum result is n - } $ intMain () { $ while(~SCANF ("%d", &N)) { - for(intI=0; i<n; i++ ) -scanf"%d", A +i); the solve (); - }Wuyi return 0; the}
Codeforces 447C dzy Loves sequences DP