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>
#define endl '\n'
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
const int MAXA = 1e9 + 10;
const int MAXX = 1e9 + 10;
struct update
{
int idx;
int val;
int old;
update()
{
idx = 0;
val = old = -1;
}
update(int _idx, int _val, int _old)
{
idx = _idx;
val = _val;
old = _old;
}
};
int n, x;
int a[MAXN];
vector < update > updates;
int lis[MAXN];
int lis_suff[MAXN];
int ans;
void read()
{
cin >> n >> x;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
}
void find_lis_suff()
{
int len = 1;
lis_suff[0] = -MAXA;
lis_suff[1] = -a[n - 1];
updates.push_back(update(1, a[n - 1], 0));
for(int i = n - 2; i >= 0; i--)
{
int j = lower_bound(lis_suff, lis_suff + len + 1, -a[i]) - lis_suff;
updates.push_back(update(j, a[i], -lis_suff[j]));
lis_suff[j] = -a[i];
len = max(len, j);
}
ans = len;
/*
for(update u : updates)
{
cout << "Change at position " << u.idx << " : from " << u.old << " to " << u.val << endl;
cout << "idx : " << u.idx << endl;
cout << "old : " << u.old << endl;
cout << "val : " << u.val << endl;
}
*/
for(int i = 1; i <= n; i++)
{
lis_suff[i] *= -1;
}
}
void rollback()
{
assert(!updates.empty());
update u = updates.back();
lis_suff[u.idx] = u.old;
updates.pop_back();
}
void check(int len)
{
int val = lis[len] - x;
int pos = -1;
for(int i = 1; i <= ans; i++)
{
if(lis_suff[i] == 0 || lis_suff[i] <= val)
break;
pos = i;
}
ans = max(ans, len + pos);
}
void print()
{
cout << "Reversed LIS : " << endl;
for(int i = 1; i <= n; i++)
{
cout << lis_suff[i] << " ";
}
cout << endl << endl;
cout << "Normal LIS : " << endl;
for(int i = 1; i <= n; i++)
{
cout << lis[i] << " ";
}
cout << endl << endl;
}
void solve()
{
int len = 1;
lis[0] = -MAXA;
lis[1] = a[0];
rollback();
/// print();
check(len);
for(int i = 1; i < n; i++)
{
int j = lower_bound(lis, lis + len + 1, a[i]) - lis;
lis[j] = a[i];
len = max(len, j);
rollback();
///print();
check(len);
}
ans = max(ans, len);
/// cout << "Answer : " << ans << endl;
cout << ans << endl;
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
find_lis_suff();
solve();
return 0;
}
# | 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... |