HDU6570 Wave(DP)

Avin is studying series. A series is called "wave" if the following conditions are satisfied:

  1. It contains at least two elements;
  2. All elements at odd positions are the same;
  3. All elements at even positions are the same;
  4. Elements at odd positions are NOT the same as the elements at even positions.
    You are given a series with length n. Avin asks you to find the longest "wave" subseries. A subseries is a subsequence of a series.

Input

The first line contains two numbers n, c (1 ≤ n ≤ 100, 000, 1 ≤ c ≤ 100). The second line contains n integers whose range is [1, c], which represents the series. It is guaranteed that there is always a "wave" subseries.

Output

Print the length of the longest "wave" subseries.

Sample Input

5 3
1 2 1 3 2

Sample Output

4

dp,设dp[i, j]代表当前为i且上一个为j的最长子序列长度,当遍历到第i个数时有转移方程:

(dp[a[i]][j] = max(dp[a[i]][ j], dp[j][a[i]] + 1))

#include <bits/stdc++.h>
using namespace std;
int n, c, a[100005], dp[105][105];//dp[i][j]表示当前为i 前一项为j的最长长度
int main() {
	cin >> n >> c;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= c; j++) {
			if(j == a[i]) continue;
			dp[a[i]][j] = max(dp[a[i]][j], dp[j][a[i]] + 1);
			ans = max(ans, dp[a[i]][j]);
		}
	}
	cout << ans << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/lipoicyclic/p/14692587.html