题意:
求类似于波的序列最大长度,任意两个数的差为q(常数)。
题解:
离散化下,然后dp,dp[a[i]][b[j]]表示到以a[i]前面到以b[j]为结尾的波形序列长度。
dp[a][b]=dp[b][a]+1;好好理解这方程。
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
typedef long long lld;
const int oo=0x3f3f3f3f;
const lld OO=1e18;
const int Mod=1000000007 ;
const int maxn=4000+5;
int dp[maxn][maxn];///前i个点以a[j]为结尾的最大波动长度
int a[maxn],b[1000005],c[1000005];
int main()
{
int n,x,y,cnt=0,ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(!b[a[i]]) b[a[i]]=++cnt;
c[a[i]]++;
ans=max(ans,c[a[i]]);
}
ans--;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
x=b[a[j]];y=b[a[i]];
if(x==y)continue;
dp[x][y]=max(dp[x][y],dp[y][x]+1);
ans=max(ans,dp[x][y]);
}
}
printf("%d\n",ans+1);
return 0;
}