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
49
50
51
52
53
54
55
56
57
|
struct Mid {
vi cnt;
int res = 0;
int cnt2 = 0;
int l = 0;
Mid(int x) : cnt(x, 0) {}
void insert(int x) {
l++;
cnt[x]++;
if (l == 1) {
res = x;
cnt2 = 1;
return;
}
if (x <= res) cnt2++;
int tem = (l + 1) / 2;
while (res > 0 && cnt2 - cnt[res] >= tem) {
cnt2 -= cnt[res];
res--;
}
while (res < sz(cnt) - 1 && cnt2 < tem) {
res++;
cnt2 += cnt[res];
}
}
int get() { return res; }
};
void solve() {
int n;
cin >> n;
vl a(n);
rep(i, 0, n - 1) cin >> a[i];
int ans = 1;
auto sorted = a;
ranges::sort(sorted);
sorted.erase(unique(all(sorted)), sorted.end());
rep(i, 0, n - 1) {
auto x = ranges::lower_bound(sorted, a[i]);
a[i] = x - sorted.begin();
}
int m = sz(sorted);
vvi dp(n + 1, vi(m, -1));
rep(i, 0, m - 1) dp[0][i] = 0;
rep(i, 0, n - 1) {
Mid tem(m);
rep(j, i, n - 1) {
tem.insert(a[j]);
if ((j - i + 1) % 2 == 0) continue;
int mid = tem.get();
if (dp[i][mid] != -1) dp[j + 1][mid] = max(dp[j + 1][mid], dp[i][mid] + 1);
}
}
rep(i, 0, m - 1) { ans = max(ans, dp[n][i]); }
cout << ans << endl;
return;
}
|