Wave hdu-6570

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

先把每个数的位置用一个vector存下来,然后从1-c里面找两个数进行双重循环,
x1是vv[i]的size,x2是vv[j]的size,now是当前已经进行到了那个位置
while(vv[i][x1]<now)是找到第一个大于now的位置,如果找到了sum++;

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int n,c;int x;int res=0;
	vector<int> vv[1234];
	cin>>n>>c;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		vv[x].push_back(i);
	}
	for(int i=1;i<=c;i++)
	{
		for(int j=1;j<=c;j++)
		{
			if(i==j) continue;
			int x2=0;int x1=0;int now=0;int sum=0;
			for( ; ;)
			{
				while(vv[i][x1]<now&&x1<vv[i].size()) x1++;
				now=vv[i][x1];
				if(x1==vv[i].size()) break;
				sum++;
				while(vv[j][x2]<now&&x2<vv[j].size()) x2++;
				now=vv[j][x2];
				if(x2==vv[j].size()) break;
				sum++;
			}
			res=max(res,sum);
		}
	}
	cout<<res<<endl;
	return 0;
}

发布了31 篇原创文章 · 获赞 0 · 访问量 336

猜你喜欢

转载自blog.csdn.net/weixin_44828107/article/details/103000627