#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#define rep(i, x, y) for(int i = x; i <= y; ++i)
#define repi(i,x,y) for(int i = x; i >= y; --i)
#define ci(x) int x; cin>> x
#define TC(t) ci(t); while(t--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define cii(x, y) ci(x); ci(y)
#define ciii(x, y, z) ci(x); ci(y); ci(z)
#define mp make_pair
//#define int long long
typedef long long ll;
typedef vector<int> vi;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int mod1 = 1e9 + 9;
const int pi = 31, pii = 29, piii = 41;
const int inf = 2e9 + 5;
const int block = 330;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
void readfile(){
#ifdef ONLINE_JUDGE
#else
freopen("text.inp", "r", stdin);
#endif // ONLINE_JUDGE
// freopen("cowjog.in", "r", stdin);
// freopen("cowjog.out", "w", stdout);
}
/*
consider this subsequence
a1 < a2 < ... < ai > ai+1 < ai+2 ... < aj
either increase ai+1->aj or decrease a1->ai by x
call s[i] is max lis starting with i
call e[i] is max lis ending with i
*/
int n, x;
int a[N], s[N], e[N];
vector<int> b;
unordered_map<int,int> m;
void inp(){
cin >> n >> x;
rep(i,1,n){
cin >> a[i];
if(m[a[i]]==0){
b.pb(a[i]);
m[a[i]] = 1;
}
}
sort(all(b));
}
struct seg_tree{
int sz = 1;
vector<int> val;
void inp(int n){
while(sz < n)
sz <<= 1;
val.resize(2*sz, 0);
}
void update(int x, int lx, int rx, int i, int v){
if(rx - lx == 1){
val[x] = max(val[x], v);
return;
}
int mid = (lx + rx) >> 1;
if(i < mid)
update(2*x+1,lx,mid,i,v);
else update(2*x+2,mid,rx,i,v);
val[x] = max(val[2*x+1], val[2*x+2]);
}
void update(int i, int v){
update(0,0,sz,i,v);
}
int get(int x, int lx, int rx, int l, int r){
if(r <= lx || rx <= l)
return 0;
if(l <= lx && rx <= r)
return val[x];
int mid = (lx + rx) >> 1;
int left = get(2*x+1,lx,mid,l,r);
int right = get(2*x+2,mid,rx,l,r);
return max(left, right);
}
int get(int l, int r){
return get(0, 0, sz, l, r);
}
} st;
int dp[N];
void process(){
fill(dp + 1, dp + n + 1, inf);
rep(i,1,n){
int pos = lower_bound(dp + 1, dp + n + 1, a[i]) - dp;
dp[pos] = a[i];
e[i] = pos;
}
fill(dp + 1, dp + n + 1, -inf);
repi(i,n,1){
int l = 1, r = n, pos;
while(l <= r){
int mid = (l + r) >> 1;
if(dp[mid] <= a[i]){
pos = mid;
l = mid + 1;
}
else r = mid - 1;
}
dp[pos] = a[i];
s[i] = n + 1 - pos;
}
st.inp(n);
int res = 0;
rep(i,1,n){
int pos = upper_bound(all(b), a[i] - x) - b.begin();
int pos1 = lower_bound(all(b), a[i] + x) - b.begin() - 1;
res = max(res, st.get(pos, pos1 + 1) + s[i]);
int p = lower_bound(all(b), a[i]) - b.begin();
st.update(p, e[i]);
}
cout << res;
}
int main() {
// readfile();
// TC(t){
inp();
process();
// }
return 0;
}
Compilation message
glo.cpp: In function 'void readfile()':
glo.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("text.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp: In function 'void process()':
glo.cpp:127:22: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
127 | s[i] = n + 1 - pos;
| ~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
17124 KB |
Output is correct |
2 |
Correct |
299 ms |
17204 KB |
Output is correct |
3 |
Correct |
290 ms |
17196 KB |
Output is correct |
4 |
Correct |
273 ms |
17200 KB |
Output is correct |
5 |
Correct |
139 ms |
11632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
4404 KB |
Output is correct |
2 |
Correct |
62 ms |
4440 KB |
Output is correct |
3 |
Correct |
62 ms |
4436 KB |
Output is correct |
4 |
Correct |
32 ms |
3108 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
34 ms |
3104 KB |
Output is correct |
7 |
Correct |
55 ms |
4536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
8752 KB |
Output is correct |
2 |
Correct |
129 ms |
8652 KB |
Output is correct |
3 |
Correct |
241 ms |
17076 KB |
Output is correct |
4 |
Correct |
143 ms |
11736 KB |
Output is correct |
5 |
Correct |
68 ms |
8332 KB |
Output is correct |
6 |
Correct |
133 ms |
15960 KB |
Output is correct |
7 |
Correct |
147 ms |
16636 KB |
Output is correct |
8 |
Correct |
95 ms |
8720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
295 ms |
17124 KB |
Output is correct |
28 |
Correct |
299 ms |
17204 KB |
Output is correct |
29 |
Correct |
290 ms |
17196 KB |
Output is correct |
30 |
Correct |
273 ms |
17200 KB |
Output is correct |
31 |
Correct |
139 ms |
11632 KB |
Output is correct |
32 |
Correct |
63 ms |
4404 KB |
Output is correct |
33 |
Correct |
62 ms |
4440 KB |
Output is correct |
34 |
Correct |
62 ms |
4436 KB |
Output is correct |
35 |
Correct |
32 ms |
3108 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
34 ms |
3104 KB |
Output is correct |
38 |
Correct |
55 ms |
4536 KB |
Output is correct |
39 |
Correct |
109 ms |
8752 KB |
Output is correct |
40 |
Correct |
129 ms |
8652 KB |
Output is correct |
41 |
Correct |
241 ms |
17076 KB |
Output is correct |
42 |
Correct |
143 ms |
11736 KB |
Output is correct |
43 |
Correct |
68 ms |
8332 KB |
Output is correct |
44 |
Correct |
133 ms |
15960 KB |
Output is correct |
45 |
Correct |
147 ms |
16636 KB |
Output is correct |
46 |
Correct |
95 ms |
8720 KB |
Output is correct |
47 |
Correct |
135 ms |
8652 KB |
Output is correct |
48 |
Correct |
138 ms |
8704 KB |
Output is correct |
49 |
Correct |
284 ms |
17180 KB |
Output is correct |
50 |
Correct |
144 ms |
11416 KB |
Output is correct |
51 |
Correct |
113 ms |
10444 KB |
Output is correct |
52 |
Correct |
188 ms |
16464 KB |
Output is correct |
53 |
Correct |
128 ms |
16492 KB |
Output is correct |
54 |
Correct |
162 ms |
17280 KB |
Output is correct |
55 |
Correct |
254 ms |
17132 KB |
Output is correct |