码迷,mamicode.com
首页 > 其他好文 > 详细

HDU6570 Wave(DP)

时间:2021-04-23 12:27:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ati   max   line   contains   sub   inpu   seq   nts   longest   

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;
}

HDU6570 Wave(DP)

标签:ati   max   line   contains   sub   inpu   seq   nts   longest   

原文地址:https://www.cnblogs.com/lipoicyclic/p/14692587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!