#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5+1;
const LL MOD = 1e9+7;
int a[N],dp[N][2],tree[N*4*3],b[N];
int n,k;
map<int,int>mp;
set<int>st;
void update(int v,int tl,int tr,int pos,int val){
if(tl==tr){
tree[v] = val;
return ;
}
int mid = (tl+tr)/2;
if(pos<=mid)update(v*2,tl,mid,pos,val);
else update(v*2+1,mid+1,tr,pos,val);
tree[v] = max(tree[v*2],tree[v*2+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(tl>r||tr<l)
return 0;
if(tl>=l&&tr<=r)
return tree[v];
int mid = (tl+tr)/2;
return max(get(v*2,tl,mid,l,r),get(v*2+1,mid+1,tr,l,r));
}
void calc(){
for(int i=1;i<=n;i++){
dp[i][0] = get(1,1,3*n,1,b[i]-1)+1;
if(get(1,1,3*n,b[i],b[i])<dp[i][0])update(1,1,3*n,b[i],dp[i][0]);
}
for(int i=1;i<=n;i++)
update(1,1,3*n,b[i],0);
for(int i=n;i>=1;i--){
dp[i][1] = get(1,1,3*n,b[i]+1,3*n)+1;
if(get(1,1,3*n,b[i],b[i])<dp[i][1])update(1,1,3*n,b[i],dp[i][1]);
}
for(int i=1;i<=n;i++)
update(1,1,3*n,b[i],0);
}
void op(int l,int r,int x){
for(int i=l;i<=r;i++)
a[i] += x;
}
struct Vertex {
int left, right;
int sum = 0;
Vertex *left_child = nullptr, *right_child = nullptr;
Vertex(int lb, int rb) {
left = lb;
right = rb;
}
void extend() {
if (!left_child && left + 1 < right) {
int t = (left + right) / 2;
left_child = new Vertex(left, t);
right_child = new Vertex(t, right);
}
}
void add(int k, int x) {
extend();
sum = max(sum,x);
if (left_child) {
if (k < left_child->right)
left_child->add(k, x);
else
right_child->add(k, x);
}
}
int get_sum(int lq, int rq) {
if (lq <= left && right <= rq)
return sum;
if (max(left, lq) >= min(right, rq))
return 0;
extend();
return max(left_child->get_sum(lq, rq),right_child->get_sum(lq, rq));
}
};
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
st.insert(a[i]);
st.insert(max(0,a[i]-k+1));
st.insert(min((int)1e9,a[i]+k-1));
}
int cnt = 1;
for(auto x:st)
mp[x] = cnt++;
for(int i=1;i<=n;i++)
b[i] = mp[a[i]];
calc();
int ans = 0;
for(int i=n;i>=1;i--){
ans = max(ans,dp[i][0]+get(1,1,n*3,mp[max(0,a[i]-k+1)],mp[min(a[i]+k-1,(int)1e9)]));
if(get(1,1,3*n,b[i],b[i])<dp[i][1])
update(1,1,3*n,b[i],dp[i][1]);
}
for(int i=1;i<=n;i++)
update(1,1,3*n,b[i],0);
for(int i=1;i<=n;i++){
ans = max(ans,dp[i][1]+get(1,1,3*n,mp[max(0,a[i]-k+1)],mp[min(a[i]+k-1,(int)1e9)]));
if(get(1,1,3*n,b[i],b[i])<dp[i][0])
update(1,1,3*n,b[i],dp[i][0]);
}
printf("%d\n",ans);
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
glo.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 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 |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
7 ms |
644 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
4 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1693 ms |
67916 KB |
Output is correct |
2 |
Correct |
1767 ms |
67956 KB |
Output is correct |
3 |
Correct |
1804 ms |
67960 KB |
Output is correct |
4 |
Correct |
1649 ms |
67956 KB |
Output is correct |
5 |
Correct |
584 ms |
35748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
310 ms |
17148 KB |
Output is correct |
2 |
Correct |
278 ms |
17128 KB |
Output is correct |
3 |
Correct |
341 ms |
17220 KB |
Output is correct |
4 |
Correct |
148 ms |
9188 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
160 ms |
3820 KB |
Output is correct |
7 |
Correct |
254 ms |
13676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
12676 KB |
Output is correct |
2 |
Correct |
384 ms |
13644 KB |
Output is correct |
3 |
Correct |
855 ms |
26916 KB |
Output is correct |
4 |
Correct |
507 ms |
15400 KB |
Output is correct |
5 |
Correct |
297 ms |
13056 KB |
Output is correct |
6 |
Correct |
622 ms |
24768 KB |
Output is correct |
7 |
Correct |
562 ms |
25396 KB |
Output is correct |
8 |
Correct |
314 ms |
13380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
7 ms |
644 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
4 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
3 ms |
460 KB |
Output is correct |
27 |
Correct |
1693 ms |
67916 KB |
Output is correct |
28 |
Correct |
1767 ms |
67956 KB |
Output is correct |
29 |
Correct |
1804 ms |
67960 KB |
Output is correct |
30 |
Correct |
1649 ms |
67956 KB |
Output is correct |
31 |
Correct |
584 ms |
35748 KB |
Output is correct |
32 |
Correct |
310 ms |
17148 KB |
Output is correct |
33 |
Correct |
278 ms |
17128 KB |
Output is correct |
34 |
Correct |
341 ms |
17220 KB |
Output is correct |
35 |
Correct |
148 ms |
9188 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
160 ms |
3820 KB |
Output is correct |
38 |
Correct |
254 ms |
13676 KB |
Output is correct |
39 |
Correct |
366 ms |
12676 KB |
Output is correct |
40 |
Correct |
384 ms |
13644 KB |
Output is correct |
41 |
Correct |
855 ms |
26916 KB |
Output is correct |
42 |
Correct |
507 ms |
15400 KB |
Output is correct |
43 |
Correct |
297 ms |
13056 KB |
Output is correct |
44 |
Correct |
622 ms |
24768 KB |
Output is correct |
45 |
Correct |
562 ms |
25396 KB |
Output is correct |
46 |
Correct |
314 ms |
13380 KB |
Output is correct |
47 |
Correct |
639 ms |
34844 KB |
Output is correct |
48 |
Correct |
647 ms |
34768 KB |
Output is correct |
49 |
Correct |
1733 ms |
69484 KB |
Output is correct |
50 |
Correct |
598 ms |
36676 KB |
Output is correct |
51 |
Correct |
413 ms |
18192 KB |
Output is correct |
52 |
Correct |
671 ms |
26052 KB |
Output is correct |
53 |
Correct |
597 ms |
25924 KB |
Output is correct |
54 |
Correct |
582 ms |
26728 KB |
Output is correct |
55 |
Correct |
1036 ms |
55488 KB |
Output is correct |