Test instructions: give you a sequence of numbers, and ask you what the longest long-wave-shaped subsequence (a,b,a,b,a,b) is.
Problem-Solving ideas: Find Pre[i][j], is in front of I and equals a[i] and from I nearest value, dp[i][j] = Dp[j][pre[j][i]] + 1;
Problem Solving Code:
1 //File name:255c.cpp2 //Author:darkdream3 //Created time:2015 March 10 Tuesday 16:26 53 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - #defineMAXN 4005 - using namespacestd; - intDP[MAXN][MAXN]; - intPRE[MAXN][MAXN]; in intA[MAXN]; - inths[1000005]; to intMain () { + intN; -scanf"%d",&n); the for(inti =1; I <= N;i + +) * { $scanf"%d",&a[i]);Panax Notoginseng } - for(inti =1; I <= N;i + +) the { + for(intj = i+1; J <= N; j + +) A { thePRE[I][J] =Hs[a[j]]; + } -Hs[a[i]] =i; $ } $Memset (DP,0,sizeof(DP)); - intAns =1 ; - for(inti =1; I<=n;i + +) the { -dp[i][0] =1; Wuyi for(intj =1; J < I;j + +) the { -DP[I][J] = Dp[j][pre[j][i]] +1; WuAns =Max (ans,dp[i][j]); - } About } $printf"%d\n", ans); - return 0; -}View Code
Codeforces 156C almost arithmetical progression