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;
}
posted @   脂环  阅读(62)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-04-23 Codeforces Round #636 E. Weights Distributing(最短路)
点击右上角即可分享
微信分享提示
主题色彩