[hdu6570]Wave

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:

  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 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 ) n, c (1 ≤ n ≤ 100, 000, 1 ≤ c ≤ 100) . The second line contains n integers whose range is [ 1 , c ] [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

题意:
给定一个数列,定义wave为:长度大于等于2的数列,且奇数位上只有一种数字,偶数位上也只有一种数字,奇数位数字跟偶数位数字不同。
题解:
DP
f [ i ] [ j ] f[i][j] 表示奇数位为 i i ,偶数位为 j j 的最长长度
顺着递推。
对于每个 a [ i ] a[i] ,你可以更新所有 f [ a [ i ] ] [ j ] f[a[i]][j] 为偶数的情况,和所有 f [ j ] [ a [ i ] ] f[j][a[i]] 为奇数的情况。

#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;
}
发布了302 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/99197762
今日推荐