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 FOR(i,x,n) for(int i=x; i<n; i++)
#define F0R(i,n) FOR(i,0,n)
#define ROF(i,x,n) for(int i=n-1; i>=x; i--)
#define R0F(i,n) ROF(i,0,n)
#define WTF cout << "WTF" << endl
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 2e5 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
#define lc now << 1
#define rc now << 1 | 1
int n, x;
int ns[N], in[N];
int bk[N], fd[N], lis[N];
int tree[N << 2];
PII st[N];
inline void init() {
cin >> n >> x;
int mx = 0;
F0R(i, n) {
cin >> ns[i];
mx = max(mx, ns[i]);
}
F0R(i, n) in[i] = mx + 1 - ns[i];
return;
}
inline void backwards() {
fill(lis, lis + n + 1, INF);
lis[0] = -1;
R0F(i, n) {
int pl = lower_bound(lis, lis + n, in[i]) - lis;
lis[pl] = in[i];
bk[i] = pl;
}
return;
}
inline void forwards() {
fill(lis, lis + n + 1, INF);
lis[0] = -1;
F0R(i, n) {
int pl = lower_bound(lis, lis + n, ns[i]) - lis;
lis[pl] = ns[i];
fd[i] = pl;
}
return;
}
void change(int id, int val, int now = 1, int ls = 0, int rs = n - 1) {
if (ls == rs) {
tree[now] = val;
return;
}
int mid = (ls + rs) >> 1;
if (id <= mid) change(id, val, lc, ls, mid);
else change(id, val, rc, mid + 1, rs);
tree[now] = max(tree[lc], tree[rc]);
return;
}
int get(int lq, int rq, int now = 1, int ls = 0, int rs = n - 1) {
if (rq < lq || rq < ls || rs < lq) return 0;
if (lq <= ls && rs <= rq) return tree[now];
int mid = (ls + rs) >> 1;
return max(get(lq, rq, lc, ls, mid), get(lq, rq, rc, mid + 1, rs));
}
int main() {
IOS;
init();
forwards();
backwards();
F0R(i, n) st[i] = {ns[i], i};
sort(st, st + n, greater<PII>());
int ptr = 0, ans = 0;
F0R(i, n) {
while(ptr < n && st[ptr].F + x > st[i].F) {
change(st[ptr].S, bk[ st[ptr].S ]);
ptr++;
}
ans = max(ans, fd[ st[i].S ] + get(st[i].S + 1, n - 1));
}
/*F0R(i, n) cout << fd[i] << ' ';
cout << endl;
F0R(i, n) cout << bk[i] << ' ';
cout << endl;*/
cout << ans;
}
# | 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... |