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;
template<typename T> bool chkmin(T &a, T b){return b < a ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return b > a ? a = b, 1 : 0;}
typedef long long ll;
const int MAXN = 2e5 + 10;
int n, x, a[MAXN], b[MAXN], t, d[MAXN], f[MAXN], g[MAXN];
// fenwick tree
struct BIT{
int c[MAXN];
void update(int i, int k){
for(; i <= t; i += i & -i)
chkmax(c[i], k);
}
int qry(int i){
int ans = 0;
for(; i; i -= i & -i)
chkmax(ans, c[i]);
return ans;
}
}c1, c2;
int main(){
// input
scanf("%d %d", &n, &x);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
// disc
for(int i = 1; i <= n; i++)
d[i] = a[i];
sort(d + 1, d + 1 + n);
t = unique(d + 1, d + 1 + n) - (d + 1);
for(int i = 1; i <= n; i++)
b[i] = lower_bound(d + 1, d + 1 + t, a[i]) - d;
// dp
for(int i = 1; i <= n; i++){
// update f
f[i] = c1.qry(b[i] - 1) + 1;
// update g
chkmax(g[i], c2.qry(b[i] - 1) + 1);
int m = lower_bound(d + 1, d + 1 + t, a[i] + x) - d;
chkmax(g[i], c1.qry(m - 1) + 1);
// update c1, c2 to [1, i]
c1.update(b[i], f[i]);
c2.update(b[i], g[i]);
}
int ans = 0;
for(int i = 1; i <= n; i++)
chkmax(ans, g[i]);
printf("%d\n", ans);
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d %d", &n, &x);
| ~~~~~^~~~~~~~~~~~~~~~~
glo.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d", &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... |