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;
typedef long long ll;
#define FOR(i,a,b) for (int i = a; i <= b; ++i)
#define FORd(i,a,b) for (int i = a; i >= b; --i)
#define FastRead ios_base::sync_with_stdio(0); cin.tie(nullptr);
#define II pair <ll,ll>
const int N = 2e5;
const ll oo = 1e18;
const int maxN = 2e9;
const int MOD = 1e9 + 7;
ll a[N + 10], dp[N + 10];
int main()
{
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif //ONLINE_JUDGE */
FastRead
int n,x;
ll k = 0;
cin >> n >> x;
for (int i = 0; i < n; ++i) cin >> a[i];
vector <ll> lis;
//Longest increasing subsequence ending at pos i
for (int i = 0; i < n; ++i) {
int pos = lower_bound(lis.begin(),lis.end(),a[i]) - lis.begin();
if (pos == lis.size()) lis.push_back(a[i]);
else lis[pos] = a[i];
dp[i] = pos + 1;
k = max(k, dp[i]);
}
lis.clear();
//Longest increasing subsequence starting at pos i and decrease all [1,i] by x
//= Longest increase minus subsequence ending at pos i with reverse array and plus x.
for (int i = n - 1; i >= 0; --i) {
ll p = lower_bound(lis.begin(),lis.end(), -a[i] + x) - lis.begin();
int pos = lower_bound(lis.begin(),lis.end(),-a[i]) - lis.begin();
if (pos == lis.size()) lis.push_back(-a[i]);
else lis[pos] = -a[i];
k = max(k,dp[i] + p);
}
cout << k;
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:34:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | if (pos == lis.size()) lis.push_back(a[i]);
| ~~~~^~~~~~~~~~~~~
glo.cpp:45:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if (pos == lis.size()) lis.push_back(-a[i]);
| ~~~~^~~~~~~~~~~~~
# | 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... |