E1. Three Blocks Palindrome (easy version) (暴力&前缀和)

题意传送门

思路:

AC代码:

#include<bits/stdc++.h>
using namespace std;
int a[27][3000];
int main(){
	int t,n,ans;
	scanf("%d",&t);
	while(t--){
		ans=0;
		scanf("%d",&n);
		for(int i=1,x;i<=n;i++)
		{
			scanf("%d",&x);
			for(int j=1;j<=26;j++) //前缀和 
				a[j][i]=a[j][i-1]+(x==j);
		}
		for(int l=1;l<=n;l++) //暴力枚举[l,r] O(26*n^2) 
			for(int r=l;r<=n;r++)
			{
				int x=0,y=0;
				for(int k=1;k<=26;k++)
				{
					 x=max(x,a[k][r]-a[k][l-1]);
					 y=max(y,min(a[k][l-1],a[k][n]-a[k][r])*2);
				}
				ans=max(ans,x+y);
			}
		printf("%d\n",ans);
	}
	return 0;
}