-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1335E1.cpp
executable file
·48 lines (41 loc) · 1.02 KB
/
1335E1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Problem Code: 1335E1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int three_blocks(int n, vector<int>& a) {
int max_l, mn, mx;
max_l = 0;
mn = *min_element(a.begin(), a.end());
mx = *max_element(a.begin(), a.end());
vector<vector<int>> prefix(mx + 1, vector<int>(n + 1));
// compute prefix sum array
for (int x = mn; x <= mx; x++)
for (int i = 0; i < n; i++)
prefix[x][i + 1] = prefix[x][i] + (a[i] == x);
// brute-force all ranges
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++) {
int max_x, max_y;
max_x = max_y = 0;
for (int y = mn; y <= mx; y++)
max_y = max(prefix[y][r] - prefix[y][l - 1], max_y);
for (int x = mn; x <= mx; x++)
max_x = max(min(prefix[x][l - 1], prefix[x][n] - prefix[x][r]), max_x);
max_l = max(2 * max_x + max_y, max_l);
}
return max_l;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
cout << three_blocks(n, a) << endl;
}
return 0;
}