hdu6570---暴力枚举

题意是:给一个长度为n的数组,且数组内的每一个数字不超过m(n最大为1e5,m最大为100),然后求这个数组最长的一个符合以下要求的子序列长度:1.选出的子序列奇数下标位置都是一样的数字,偶数下标位置都是一样的数字,且奇数下标的数和偶数下标的数不能一样

思路:因为m足够小只有100,用vector存下每一个数字出现过的下标,然后暴力枚举以哪两个数作为wave序列就好了

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
const int N= 105;
vector<int> a[N];//存放每个数字的位置
int n,m;
int main(){
	scanf("%d%d",&n,&m);
	int ans = 0;
	for(int i = 1;i <= n;i++){
		int x;scanf("%d",&x);
		a[x].push_back(i);	
	}
	for(int i = 1;i <= m;i++){
		for(int j = 1;j <= m;j++){
			if(i == j) continue;
			int len1 = a[i].size(),len2 = a[j].size();
			int sum = 0,x1 = 0,x2 = 0,now = 0;//以i,j为这两个数的最大wave值/在a[i]的下标/在a[j]的下标/现在的下标
			while(1){
				while(a[i][x1]<now&&x1<len1) x1++;
				if(x1 >= len1) break;
				now = a[i][x1];
				sum++;
				while(a[j][x2]<now&&x2<len2) x2++;
				if(x2 >= len2) break;
				now = a[j][x2];
				sum++;
			} 
			ans = max(ans,sum);
		}
	}
	printf("%d\n",ans);
	return 0;
} 
发布了27 篇原创文章 · 获赞 0 · 访问量 334

猜你喜欢

转载自blog.csdn.net/weixin_44083561/article/details/104467814
今日推荐