This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int v[200005];
int dp[200005];
int main() {
int n;
int x;
cin >> n >> x;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<int> lis(n, INT_MAX);
int longest = 0;
// longest increasing subsequence ending at i
for (int i = 0; i < n; i++) {
int j = lower_bound(lis.begin(), lis.end(), v[i]) - lis.begin();
lis[j] = v[i];
dp[i] = j + 1;
longest = max(longest, dp[i]);
}
lis = vector<int>(n, INT_MAX);
// longest decreasing subsequence ending at i of reverse
// = longest increasing subsequence starting at i
for (int i = n - 1; i >= 0; i--) {
int j = lower_bound(lis.begin(), lis.end(), -v[i] + x)-lis.begin();
longest = max(longest, dp[i]+j);
int jj = lower_bound(lis.begin(), lis.end(), -v[i]) - lis.begin();
lis[jj] = -v[i];
}
cout << longest << endl;
}
# | 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... |