Codeforces Round #634 (Div. 3) E —Three Blocks Palindrome

补题
参考:https://blog.csdn.net/weixin_45750972/article/details/105523046

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 210, M = 2e5+10;
//a[j,i]
//在前i个数字中,数字j出现在了多少次
int a[N][M];
void solve()
{
	//存每个数字 出现的位置
	vector<int>p[210];
	int n;
	int x;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> x;
		for(int j = 1; j <= 200; j++)
			a[j][i] = a[j][i-1] + (x == j);
		p[x].push_back(i);
	}
	int ans = 0;
	//枚举每个数字 
	for(int k = 1; k <= 200; k++)
	{
		//只取一个相同的 
		ans = max(ans, (int)p[k].size()); 
		//把当前数字分到左右两边 
		for(int i = 1; i <= (p[k].size() / 2); i++)
		{
			//从左边数,数字k出现的第i次的位置
			//从右边数,数字k出现的第i次的位置 
			int l = p[k][i-1] + 1, r = p[k][p[k].size() - i] - 1;
			//找这中间出现次数最多的数字的次数 
			for(int j = 1; j <= 200; j++)
			{
				int x = a[j][r] - a[j][l-1];
				ans = max(ans, i*2 + x);
			}
		}
	}
	cout << ans << endl;
}
int main()
{
	int t;
	cin >> t;
	while(t --)
		solve();
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/QingyuYYYYY/p/12712339.html
今日推荐