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"
#define mp make_pair
#define fi first
#define se second
#define lb lower_bound
using namespace std;
int main() {
int n, x;
cin >> n >> x;
int t[n + 1];
for(int i = 1; i <= n; ++i) {
cin >> t[i];
}
// find prefix lis/suffix lis, nanti cari yg max di mana
// suffix lis -> lds dr belakang
// prefix lis -> lis dr depan
int pref[n + 1], suff[n + 1];
set<int> lis, lds;
for(int i = 1; i <= n; ++i) {
if(lis.lower_bound(t[i]) != lis.end()) {
lis.erase(lis.lower_bound(t[i]));
lis.insert(t[i]);
}
else {
lis.insert(t[i]);
}
pref[i] = lis.size();
}
for(int i = n; i >= 1; --i) {
if(lds.lower_bound(-t[i]) != lds.end()) {
lds.erase(lds.lb(-t[i]));
lds.insert(-t[i]);
}
else
lds.insert(-t[i]);
suff[i] = lds.size();
}
// fi -> t
// se -> lis
set<pair<int, int>> s;
int ret = pref[n];
for(int i = n; i >= 1; --i) {
if(s.lb(mp(t[i] - x + 1, 0)) != s.end()) {
//cout << pref[i] << " " << (*s.lb(mp(t[i] - x + 1, 0))).se << endl;
//cout << "DEB " << i << " " << t[i] << " " << (*s.lb(mp(t[i] - x + 1, 0))).fi << " " << (*s.lb(mp(t[i] - x + 1, 0))).se << endl;
ret = max(ret, (*s.lb(mp(t[i] - x + 1, 0))).se + pref[i]);
}
while(s.lb(mp(t[i], 1e9)) != s.begin()) {
pair<int, int> pr = *--s.lb(mp(t[i], 0));
if(pr.se <= pref[i])
s.erase(s.find(pr));
else
break;
}
if(s.lb(mp(t[i], 0)) != s.end() && (*s.lb(mp(t[i], 0))).se >= pref[i]) {
}
else
s.insert(mp(t[i], suff[i]));
}
cout << ret << 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... |