This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* 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 (stderr)
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); }
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |