Title Link: https://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493
Test instructions: To give a series of numbers, the longest wave sequence is obtained. The definition of volatility is that a number of adjacent two numbers are at the same time larger than he or at the same time smaller than his image as a wave of triangular function bar.
Define DP (i) to the number of the longest fluctuation sequence length, we found that only one dimension of the array is not in the state, but also should maintain a quantity, that is the first I number when he is the crest or trough. The DP array is then extended to DP (I,K), and K can take 0 or 1, respectively, to indicate whether it is a crest or a trough. When updating, enumerate the number of J (1~i-1), if the number I and J number is not the same update, when the update to the I and J two numbers respectively as a different pole.
1 /*2 ━━━━━┒ギリギリ♂eye! 3 ┓┏┓┏┓┃キリキリ♂mind! 4 ┛┗┛┗┛┃\0/5 ┓┏┓┏┓┃/6 ┛┗┛┗┛┃ノ)7 ┓┏┓┏┓┃8 ┛┗┛┗┛┃9 ┓┏┓┏┓┃Ten ┛┗┛┗┛┃ One ┓┏┓┏┓┃ A ┛┗┛┗┛┃ - ┓┏┓┏┓┃ - ┃┃┃┃┃┃ the ┻┻┻┻┻┻ - */ -#include <algorithm> -#include <iostream> +#include <iomanip> -#include <cstring> +#include <climits> A#include <complex> at#include <fstream> -#include <cassert> -#include <cstdio> -#include <bitset> -#include <vector> -#include <deque> in#include <queue> -#include <stack> to#include <ctime> +#include <Set> -#include <map> the#include <cmath> * using namespacestd; $ #defineFr FirstPanax Notoginseng #defineSC Second - #defineCL Clear the #defineBUG puts ("Here!!!") + #defineW (a) while (a--) A #definePB (a) push_back (a) the #defineRint (a) scanf ("%d", &a) + #defineRll (a) scanf ("%i64d", &a) - #defineRs (a) scanf ("%s", a) $ #defineCIN (a) CIN >> a $ #defineFRead () freopen ("in", "R", stdin) - #defineFWrite () freopen ("Out", "w", stdout) - #defineRep (i, Len) for (LL i = 0; i < (len); i++) the #defineFor (I, A, Len) for (LL i = (a); I < (len); i++) - #defineCls (a) memset ((a), 0, sizeof (a))Wuyi #defineCLR (A, X) memset ((a), (x), sizeof (a)) the #defineFuint (a) memset ((a), 0x7f7f, sizeof (a)) - #defineLRT RT << 1 Wu #defineRRT RT << 1 | 1 - #definePi 3.14159265359 About #defineRT return $ #defineLowbit (x) x & (-X) - #defineOnenum (x) __builtin_popcount (x) -typedefLong LongLL; -typedefLong DoubleLD; Atypedef unsignedLong LongUint; +typedef PAIR<LL, Ll>PII; thetypedef pair<string, ll>psi; -typedef map<string, ll>MSI; $typedef vector<ll>VI; thetypedef vector<ll>VL; thetypedef vector<vl>VVL; thetypedef vector<BOOL>vb; the - Const intMAXN =101010; in intdp[maxn][2]; the intN; the intA[MAXN]; About the intMain () { the FRead (); the while(~Rint (n)) { +for (I,1, n+1) Rint (A[i]); - intRET =0; thefor (I,1, n+1) {Bayidp[i][0] = dp[i][1] =1; theRep (k,2) { theFor (J,1, I) { - if(A[i] = = A[j])Continue; - if(k = =0) { the if(A[i] < a[j]) dp[i][k] = max (Dp[i][k], dp[j][!k]+1); the } the Else { the if(A[i] > A[j]) dp[i][k] = max (Dp[i][k], dp[j][!k]+1); - } the } theRET = max (ret, Max (dp[i][0], dp[i][1])); the }94 } theprintf"%d\n", ret); the } the Cls (DP);98Rt0; About}
[Topcoder] ZigZag (DP)