/**
* tăng giảm 1 đoạn bất kì 1 lượng |d| <= x để lis đạt max
* - nhận xét: kmttq, giả sử tăng 1 đoạn [i,j] lên k đơn vị thì tăng cả ở prefix lẫn suffix
* + điều này vô lý vì xét case hi[1]-lo-hi[2], lo vốn đã match với hi[2], nếu tăng lo thêm thì lis chỉ có thể giảm đi
* => ta update lại hết prefix hoặc suffix, đồng thời nhận xét là ta luôn tăng hoặc giảm hết mức (tăng giảm x đơn vị)
* => chỉ xét giảm prefix đi x (tăng suffix lên x có cấu hình sau khi nén số tương tự)
**/
#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 8;
const int N2 = N << 1;
int n, x;
int t[N];
vector<int> a;
unordered_map<int, int> mp;
void build_map() {
for (int i = 1; i <= n; ++i)
a.push_back(t[i]), a.push_back(t[i] - x);
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
int cnt(0);
for (int x : a) mp[x] = ++cnt;
}
int dp[2][N]; // prefix, suffix, max suffix
int b[N];
int bit[N2];
void upd(int p, int v) { for (; p <= a.size(); p += p & -p) bit[p] = max(bit[p], v); }
int get(int p) { int ans(0); for (; p; p ^= p & -p) ans = max(ans, bit[p]); return ans; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> x;
for (int i = 1; i <= n; ++i)
cin >> t[i];
build_map();
int cnt(0);
for (int i = 1, p; i <= n; ++i) {
dp[0][i] = p = lower_bound(b + 1, b + cnt + 1, mp[t[i]]) - b;
if (cnt < p) cnt = p;
b[p] = mp[t[i]];
}
cnt = 0;
for (int i = n, p; i > 0; --i) {
dp[1][i] = p = lower_bound(b + 1, b + cnt + 1, mp[t[i]], greater<int>()) - b;
if (cnt < p) cnt = p;
b[p] = mp[t[i]];
}
int ans(0);
for (int i = 1; i <= n; ++i) {
ans = max(ans, get(mp[t[i]] - 1) + dp[1][i]);
upd(mp[t[i] - x], dp[0][i]);
}
cout << ans << '\n';
}
Compilation message
glo.cpp: In function 'void upd(int, int)':
glo.cpp:38:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | void upd(int p, int v) { for (; p <= a.size(); p += p & -p) bit[p] = max(bit[p], v); }
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
244 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
0 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
244 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
0 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
14024 KB |
Output is correct |
2 |
Correct |
188 ms |
15852 KB |
Output is correct |
3 |
Correct |
186 ms |
15876 KB |
Output is correct |
4 |
Correct |
188 ms |
15864 KB |
Output is correct |
5 |
Correct |
74 ms |
10808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
6168 KB |
Output is correct |
2 |
Correct |
41 ms |
6624 KB |
Output is correct |
3 |
Correct |
43 ms |
6644 KB |
Output is correct |
4 |
Correct |
20 ms |
4088 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
14 ms |
4084 KB |
Output is correct |
7 |
Correct |
32 ms |
5320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
11708 KB |
Output is correct |
2 |
Correct |
152 ms |
12676 KB |
Output is correct |
3 |
Correct |
353 ms |
25008 KB |
Output is correct |
4 |
Correct |
128 ms |
15084 KB |
Output is correct |
5 |
Correct |
45 ms |
12380 KB |
Output is correct |
6 |
Correct |
94 ms |
23856 KB |
Output is correct |
7 |
Correct |
88 ms |
24500 KB |
Output is correct |
8 |
Correct |
98 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
244 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
324 KB |
Output is correct |
17 |
Correct |
0 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
190 ms |
14024 KB |
Output is correct |
28 |
Correct |
188 ms |
15852 KB |
Output is correct |
29 |
Correct |
186 ms |
15876 KB |
Output is correct |
30 |
Correct |
188 ms |
15864 KB |
Output is correct |
31 |
Correct |
74 ms |
10808 KB |
Output is correct |
32 |
Correct |
46 ms |
6168 KB |
Output is correct |
33 |
Correct |
41 ms |
6624 KB |
Output is correct |
34 |
Correct |
43 ms |
6644 KB |
Output is correct |
35 |
Correct |
20 ms |
4088 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
14 ms |
4084 KB |
Output is correct |
38 |
Correct |
32 ms |
5320 KB |
Output is correct |
39 |
Correct |
126 ms |
11708 KB |
Output is correct |
40 |
Correct |
152 ms |
12676 KB |
Output is correct |
41 |
Correct |
353 ms |
25008 KB |
Output is correct |
42 |
Correct |
128 ms |
15084 KB |
Output is correct |
43 |
Correct |
45 ms |
12380 KB |
Output is correct |
44 |
Correct |
94 ms |
23856 KB |
Output is correct |
45 |
Correct |
88 ms |
24500 KB |
Output is correct |
46 |
Correct |
98 ms |
12636 KB |
Output is correct |
47 |
Correct |
115 ms |
13004 KB |
Output is correct |
48 |
Correct |
119 ms |
13008 KB |
Output is correct |
49 |
Correct |
340 ms |
25920 KB |
Output is correct |
50 |
Correct |
113 ms |
15576 KB |
Output is correct |
51 |
Correct |
51 ms |
10988 KB |
Output is correct |
52 |
Correct |
65 ms |
15712 KB |
Output is correct |
53 |
Correct |
97 ms |
24804 KB |
Output is correct |
54 |
Correct |
90 ms |
25396 KB |
Output is correct |
55 |
Correct |
207 ms |
20660 KB |
Output is correct |