标签:ati max line contains sub inpu seq nts longest
Avin is studying series. A series is called "wave" if the following conditions are satisfied:
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;
}
标签:ati max line contains sub inpu seq nts longest
原文地址:https://www.cnblogs.com/lipoicyclic/p/14692587.html