CF1335E1 Three Blocks Palindrome (easy version)
题目链接:https://codeforces.com/contest/1335/problem/E1
题目大意:
想法:
最简单的想法就是我们考虑 [1,l] [l+1,r] [r+1,n] 这三个区间分别代表 x 和 y
我们可以先预处理sum[i][j] 代表前 i 个 值为 j 的数的个数
这样我们就可以在 O(26*n*n) 的复杂度完成
具体的还是看代码吧。
#pragma GCC optimize(3,"Ofast","inline")//O3优化 #pragma GCC optimize(2)//O2优化 #include <algorithm> #include <string> #include <cstring> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <cmath> #include <cstdio> #include <iomanip> #include <ctime> #include <bitset> #include <cmath> #include <sstream> #include <iostream> #define LL long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f #define max(a,b) (a>b?a:b) #define min(a,b) (a<b?a:b) const double eps = 1e-10; const int maxn = 2e3 + 10; const int mod = 998244353; int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;} using namespace std; int a[maxn]; int sum[maxn][27]; int vis1[27],vis2[27],vis3[27]; int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { memset(sum,0, sizeof(sum)); int n; cin >> n; for (int i = 1;i <= n;i++) { cin >> a[i]; for (int j = 1;j <= 26;j++) sum[i][j] = sum[i-1][j]; sum[i][a[i]]++; } int Max = 0; for (int l = 1;l <= n;l++) { for (int r = l;r <= n;r++) { int cnt = 0,cmt = 0; for (int i = 1;i <= 26;i++) { cnt = max(cnt,min(sum[l-1][i],sum[n][i]-sum[r][i])); // 【1,l-1】区间以及 【r+1,n】区间 cmt = max(cmt,sum[r][i]-sum[l-1][i]); // 【l,r】 区间 } Max = max(Max,2*cnt+cmt); } } cout << Max << endl; } return 0; }