#include <bits/stdc++.h>
using namespace std;
struct change
{
long long p, lval;
change(long long p, long long lval)
{
this->p = p;
this->lval = lval;
}
};
long long n,k,ans;
long long a[200005];
long long dpl[200005];
long long dpr[200005];
vector<change> changes;
void init()
{
ans = 0;
dpl[0] = -1e18;
for(int i=1;i<=n;i++)
dpl[i] = 1e18;
dpr[0] = 1e18;
for(int i=1;i<=n;i++)
dpr[i] = -1e18;
}
void addR(long long x)
{
long long st=1, dr=n, mij, last=1;
while(st <= dr)
{
mij = (st + dr)/2;
if(dpr[mij] <= x)
{
last = mij;
dr = mij-1;
}
else
st = mij+1;
}
long long p = last;
changes.push_back(change(p, dpr[p]));
dpr[p] = x;
}
void delR()
{
long long p = changes.back().p;
long long lval = changes.back().lval;
changes.pop_back();
dpr[p] = lval;
}
long long queryR(long long x)
{
long long st=0, dr=n, mij, last=0;
while(st <= dr)
{
mij = (st+dr)/2;
if(dpr[mij] > x)
{
last = mij;
st = mij+1;
}
else
dr = mij-1;
}
return last;
}
void addL(long long x)
{
long long st=1, dr=n, mij, last=1;
while(st <= dr)
{
mij = (st+dr)/2;
if(dpl[mij] >= x)
{
last = mij;
dr = mij-1;
}
else
st = mij+1;
}
long long p = last;
//cout<<p<<' '<<queryR(x-k)<<"\n\n";
dpl[p] = x;
ans = max(ans, (long long)p + queryR(x-k));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
init();
for(int i=n;i>=1;i--)
{
addR(a[i]);
/*
cout<<"at pos "<<i<<'\n';
for(int j=0; j<=n; j++)
{
if(dpr[j] == 0)
break;
cout<<dpr[j]<<' ';
}
cout<<"\n\n";
*/
}
for(int i=1;i<=n;i++)
{
//cout<<"at pos "<<i<<'\n';
delR();
addL(a[i]);
/*
cout<<"left is\n";
for(int j=0;j<=n;j++)
{
if(dpl[j] == 1e18)
break;
cout<<dpl[j]<<' ';
}
cout<<"\n\n";
cout<<"right is\n";
for(int j=0;j<=n;j++)
{
if(dpr[j] == -1e18)
break;
cout<<dpr[j]<<' ';
}
cout<<"\n\n";
*/
}
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
11216 KB |
Output is correct |
2 |
Correct |
64 ms |
11160 KB |
Output is correct |
3 |
Correct |
65 ms |
11076 KB |
Output is correct |
4 |
Correct |
65 ms |
11076 KB |
Output is correct |
5 |
Correct |
48 ms |
10352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
3084 KB |
Output is correct |
2 |
Correct |
15 ms |
3156 KB |
Output is correct |
3 |
Correct |
16 ms |
3084 KB |
Output is correct |
4 |
Correct |
13 ms |
2892 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
12 ms |
2960 KB |
Output is correct |
7 |
Correct |
13 ms |
3080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5832 KB |
Output is correct |
2 |
Correct |
28 ms |
5820 KB |
Output is correct |
3 |
Correct |
55 ms |
11112 KB |
Output is correct |
4 |
Correct |
50 ms |
10316 KB |
Output is correct |
5 |
Correct |
23 ms |
5456 KB |
Output is correct |
6 |
Correct |
41 ms |
10204 KB |
Output is correct |
7 |
Correct |
47 ms |
10824 KB |
Output is correct |
8 |
Correct |
25 ms |
5772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
64 ms |
11216 KB |
Output is correct |
28 |
Correct |
64 ms |
11160 KB |
Output is correct |
29 |
Correct |
65 ms |
11076 KB |
Output is correct |
30 |
Correct |
65 ms |
11076 KB |
Output is correct |
31 |
Correct |
48 ms |
10352 KB |
Output is correct |
32 |
Correct |
17 ms |
3084 KB |
Output is correct |
33 |
Correct |
15 ms |
3156 KB |
Output is correct |
34 |
Correct |
16 ms |
3084 KB |
Output is correct |
35 |
Correct |
13 ms |
2892 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
12 ms |
2960 KB |
Output is correct |
38 |
Correct |
13 ms |
3080 KB |
Output is correct |
39 |
Correct |
28 ms |
5832 KB |
Output is correct |
40 |
Correct |
28 ms |
5820 KB |
Output is correct |
41 |
Correct |
55 ms |
11112 KB |
Output is correct |
42 |
Correct |
50 ms |
10316 KB |
Output is correct |
43 |
Correct |
23 ms |
5456 KB |
Output is correct |
44 |
Correct |
41 ms |
10204 KB |
Output is correct |
45 |
Correct |
47 ms |
10824 KB |
Output is correct |
46 |
Correct |
25 ms |
5772 KB |
Output is correct |
47 |
Correct |
31 ms |
5804 KB |
Output is correct |
48 |
Correct |
31 ms |
5840 KB |
Output is correct |
49 |
Correct |
63 ms |
11096 KB |
Output is correct |
50 |
Correct |
50 ms |
10356 KB |
Output is correct |
51 |
Correct |
39 ms |
8868 KB |
Output is correct |
52 |
Correct |
47 ms |
10428 KB |
Output is correct |
53 |
Correct |
43 ms |
10444 KB |
Output is correct |
54 |
Correct |
47 ms |
11204 KB |
Output is correct |
55 |
Correct |
57 ms |
11204 KB |
Output is correct |