Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
Problem Description
Avin is studying series. A series is called “wave” if the following conditions are satisfied:
- It contains at least two elements;
- All elements at odd positions are the same;
- All elements at even positions are the same;
- Elements at odd positions are NOT the same as the elements at even positions.
You are given a series with length . Avin asks you to find the longest “wave” subseries. A subseries is a subsequence of a series.
Input
The first line contains two numbers
. The second line contains n integers whose range is
, 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
题意:
给定一个数列,定义wave为:长度大于等于2的数列,且奇数位上只有一种数字,偶数位上也只有一种数字,奇数位数字跟偶数位数字不同。
题解:
DP
表示奇数位为
,偶数位为
的最长长度
顺着递推。
对于每个
,你可以更新所有
为偶数的情况,和所有
为奇数的情况。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,c,a[100004];
int f[104][104];
int w33ha(){
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++){
for(int j=1;j<=c;j++){
if(a[i]==j)continue;
if(f[a[i]][j]%2==0){
f[a[i]][j]++;
}
if(f[j][a[i]]%2==1){
f[j][a[i]]++;
}
}
}
int ans=0;
for(int i=1;i<=c;i++){
for(int j=1;j<=c;j++){
ans=max(ans,f[i][j]);
}
}
printf("%d\n",ans);
return 0;
}
int main(){
while(scanf("%d%d",&n,&c)!=EOF)w33ha();
return 0;
}