HDUOJ 6570 Wave
题目链接
Problem Description
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,设 d p [ i ] [ j ] dp[i][j] dp[i][j] 是以 j j j 结尾, i i i 为结尾前一个数字的最长合法 wave,则有状态转移方程:
d p [ i ] [ j ] = d p [ j ] [ i ] + 1 dp[i][j]=dp[j][i]+1 dp[i][j]=dp[j][i]+1
注意一个坑点,答案必须要加换行,AC代码如下:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,c,ans=1,dp[105][105],a[N];
int main(){scanf("%d%d",&n,&c);for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int i=1;i<=c;i++){if(a[1]==i){dp[a[1]][i]=0;}else{dp[a[1]][i]=1;}}for(int i=2;i<=n;i++){for(int j=1;j<=c;j++){if(a[i]==j) dp[a[i]][j]=0;else dp[a[i]][j]=dp[j][a[i]]+1;ans=max(ans,dp[a[i]][j]);}}printf("%d\n",ans);
}