江西省赛Wave

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
    题意:找最长的序列,这个序列满足奇数位是相同的数,偶数位是相同的数字,且奇数位与偶数位的数字不同。来求最长的序列。
    思路:1.结构体来做记下n个数中重复出现数的个数,然后每两个组合,查找这两个数可以组成的最长子序列;当然也可以用vector数组来做,暴力求解。应该都是很简单方便的。
    2.使用动态规划来做也OK的,看了一个大佬的代码感觉顿时醒悟,这题还可以这么简单么。当然我动态规划还不是用的很熟练。应该用到的含义是dp[i][j]=dp[j][i]+1;这个思想。
在这里插入代码片
#include<iostream>
#include<cstdio>
using namespace std;
int num[100005];
struct node
{
	int number,quanlity;//哪一个数,数的个数 
}d[105];
int cmp(struct node x,struct node y)
{
	return x.quanlity>y.quanlity;
}
int main()
{
	int n,c;
	scanf("%d %d",&n,&c);
	for(int i=0;i<=100;i++)
	{
		d[i].quanlity=0,d[i].number=i;
	}	
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num[i]);
		d[num[i]].quanlity++;
	}
	int maxn=0;
	for(int i=0;i<c-1;i++)
	{
		for(int ii = i+1;ii<c;ii++)
		{
			int s = 1 , x = d[i].number , y = d[ii].number , mm , j;
			for(j=0;j<n;j++)
			{
				if(num[j]==x||num[j]==y) 
				{
					mm=num[j];
					break;
				}
			}
			for(;j<n;j++)
			{
				if(num[j]==x||num[j]==y)
				{
					if(num[j]!=mm) s++,mm=num[j];
				}
			}
			if(s>maxn) 
			{
				maxn=s;
			}
		}
	}
	printf("%d\n",maxn);
	return 0;
}
在这里插入代码片
#include"iostream" 
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
int n,c,a[100004];
int f[104][104];
int main()
{
    while(scanf("%d%d",&n,&c)!=EOF)
    {
	    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;
}



发布了76 篇原创文章 · 获赞 1 · 访问量 2771

猜你喜欢

转载自blog.csdn.net/weixin_44824383/article/details/103128891