Skip to content

Commit 66d47ba

Browse files
authored
Merge pull request #1319 from AlgorithmWithGod/khj20006
[20251105] BOJ / P4 / 방어선 / 권혁준
2 parents 7709255 + 600e2f2 commit 66d47ba

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int T, N;
6+
vector<int> seg, arr, cnt, cnt2;
7+
vector<pair<int, int>> arr2;
8+
9+
void upt(int s, int e, int i, int v, int n) {
10+
if (s == e) {
11+
seg[n] = v;
12+
return;
13+
}
14+
int m = (s + e) >> 1;
15+
if (i <= m) upt(s, m, i, v, n << 1);
16+
else upt(m + 1, e, i, v, n << 1 | 1);
17+
seg[n] = max(seg[n << 1], seg[n << 1 | 1]);
18+
}
19+
20+
int find(int s, int e, int l, int r, int n) {
21+
if (l > r || l > e || r < s) return 0;
22+
if (l <= s && e <= r) return seg[n];
23+
int m = (s + e) >> 1;
24+
return max(find(s, m, l, r, n << 1), find(m + 1, e, l, r, n << 1 | 1));
25+
}
26+
27+
int main() {
28+
cin.tie(0)->sync_with_stdio(0);
29+
30+
for (cin >> T; T--;) {
31+
cin >> N;
32+
seg = vector<int>(4 * N);
33+
arr = vector<int>(N + 1);
34+
cnt = vector<int>(N + 1);
35+
cnt2 = vector<int>(N + 1);
36+
arr2 = vector<pair<int, int>>();
37+
for (int i = 1; i <= N; i++) {
38+
cin >> arr[i];
39+
arr2.emplace_back(arr[i], i);
40+
cnt[i] = arr[i - 1] < arr[i] ? cnt[i - 1] + 1 : 1;
41+
}
42+
sort(arr2.begin(), arr2.end(), [](auto a, auto b) -> bool {
43+
if (a.first == b.first) return a.second > b.second;
44+
return a.first < b.first;
45+
});
46+
47+
for (auto [x, i] : arr2) {
48+
cnt2[i] = find(1, N, 1, i - 1, 1) + 1;
49+
upt(1, N, i, cnt[i], 1);
50+
}
51+
int ans = 0;
52+
for (int i = 1; i <= N; i++) {
53+
if (arr[i - 1] < arr[i]) cnt2[i] = max(cnt2[i], cnt2[i - 1] + 1);
54+
ans = max(ans, cnt2[i]);
55+
}
56+
cout << ans << '\n';
57+
}
58+
59+
}
60+
```

0 commit comments

Comments
 (0)