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
思路:由于c的范围只有100,我们分别枚举奇偶位置的数值a、b,更新出最大长度ans。为了减少每组a、b查找子串长度的复杂度,我们预处理一个v[i][j]表示 数值i的第j个值的位置在哪里。
#include<iostream>
#include<cstdio>
#include<vector>
#define ll long long
using namespace std;
vector<int>v[200];
int a[1008611];
int main(){
int n,c,ans=0;
cin>>n>>c;
for(int i=1;i<=n;i++){
cin>>a[i];
v[a[i]].push_back(i);
}
for(int i=1;i<=c;i++){
for(int j=1;j<=c;j++){
if(i==j)continue;
int p1=0,p2=0,flag=0,len=0,fir=0,sec=0;
while(p1<v[i].size()&&p2<v[j].size()){
if(flag==0)
{
while(p1<v[i].size()&&v[i][p1]<sec)p1++;
if(p1<v[i].size()){
fir=v[i][p1];
len++;
}
flag=1;
}
else if(flag==1)
{
while(p2<v[j].size()&&v[j][p2]<fir)p2++;
if(p2<v[j].size()){
len++;
sec=v[j][p2];
}
flag=0;
}
}
ans=max(ans,len);
}
}
cout<<ans<<endl;
return 0;
}