#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define DIMN 200010
#define INF 1000000000
using namespace std;
int n;
int v[DIMN],aint[4*DIMN],v2[DIMN],prefix[DIMN],sufix[DIMN],w[DIMN];
int findd (int nr,int ok){ /// primul > sau ult <=
int st,dr,mid;
st = 1;
dr = n;
while (st<=dr){
mid = (st + dr)/2;
if (v2[mid]<=nr)
st = mid + 1;
else dr = mid - 1;
}
if (ok == 0)
return dr;
return st;
}
void update (int nod,int st,int dr,int p,int val){
int mid = (st + dr)/2;
if (st == dr){
aint[nod] = max(aint[nod] , val);
return;
}
if (p<=mid)
update(2*nod,st,mid,p,val);
else update(2*nod+1,mid+1,dr,p,val);
aint[nod] = max (aint[2*nod] , aint[2*nod+1]);
}
int query (int nod,int st,int dr,int l, int r){
int mid = (st + dr)/2;
int sol = -INF;
if (l > r)
return -INF;
if (l<=st && dr<=r){
return aint[nod];
}
if (l<=mid)
sol = query(2*nod,st,mid,l,r);
if (mid+1<=r)
sol = max(sol , query(2*nod+1,mid+1,dr,l,r));
return sol;
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int x,i,dist,elem,sol,st,dr,mid;
fscanf (fin,"%d%d",&n,&x);
for (i=1;i<=n;i++){
fscanf (fin,"%d",&v[i]);
v2[i] = v[i];
}
sort (v2 + 1, v2 + n + 1);
dist = 0;
for (i=1;i<=n;i++){
if (i==1 || v2[i]!=v2[i-1])
v2[++dist] = v2[i];
}
v2[0] = -2000000000;
v2[n+1] = 2000000000;
elem = 0;
for (i=1;i<=n;i++){
st = 1;
dr = elem;
while (st<=dr){
mid = (st + dr)/2;
if (v[w[mid]] < v[i])
st = mid + 1;
else dr = mid - 1;
}
if (st == elem + 1){
w[++elem] = i;
prefix[i] = elem;
}
else {
if (v[i] < v[w[st]])
w[st] = i;
prefix[i] = st;
}
}
/// reverse v
st = 1;
dr = n;
while (st<dr){
swap(v[st] , v[dr]);
st++;
dr--;
}
/// gata reverse , calcul sufix
elem = 0;
for (i=1;i<=n;i++){
st = 1;
dr = elem;
while (st<=dr){
mid = (st + dr)/2;
if (v[w[mid]] > v[i])
st = mid + 1;
else dr = mid - 1;
}
if (st == elem + 1){
w[++elem] = i;
sufix[n-i+1] = elem;
}
else {
if (v[i] > v[w[st]])
w[st] = i;
sufix[n-i+1] = st;
}
}
/// reverse v
st = 1;
dr = n;
while (st<dr){
swap(v[st] , v[dr]);
st++;
dr--;
}
/// gata reverse
/// caz 1 : aduni x la un sufix
sol = prefix[n];
for (i=1;i<=n;i++){
/// ce obtinem daca adaugam de la i la n o valoare
/// vreau sa gasesc care e prefix[j] maxim , j<i a.i v[j] < v[i] + x
if (i!=1)
sol = max (sol , sufix[i] + query(1,1,n,1,findd(v[i]+x-1,0))); /// ult <=
update (1,1,n,findd(v[i],0) , prefix[i]);
}
/// caz 2 : scazi x de la un prefix
memset (aint,0,sizeof(aint));
for (i=n;i;i--){
/// ce obtinem daca adunam -x de la 1 la i
/// vreau sa gasesc care e sufix[j] maxim , j > i, v[i] - x < v[j]
if (i!=n){
sol = max (sol , prefix[i] + query(1,1,n,findd(v[i]-x,1),n)); /// prm >
}
update (1,1,n,findd(v[i],0) , sufix[i]);
}
fprintf (fout,"%d",sol);
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:54:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d",&n,&x);
~~~~~~~^~~~~~~~~~~~~~~~~~
glo.cpp:56:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d",&v[i]);
~~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3704 KB |
Output is correct |
2 |
Correct |
4 ms |
3448 KB |
Output is correct |
3 |
Correct |
4 ms |
3448 KB |
Output is correct |
4 |
Correct |
5 ms |
3448 KB |
Output is correct |
5 |
Correct |
4 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3576 KB |
Output is correct |
7 |
Correct |
5 ms |
3448 KB |
Output is correct |
8 |
Correct |
5 ms |
3448 KB |
Output is correct |
9 |
Correct |
5 ms |
3448 KB |
Output is correct |
10 |
Correct |
5 ms |
3452 KB |
Output is correct |
11 |
Correct |
5 ms |
3448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3704 KB |
Output is correct |
2 |
Correct |
4 ms |
3448 KB |
Output is correct |
3 |
Correct |
4 ms |
3448 KB |
Output is correct |
4 |
Correct |
5 ms |
3448 KB |
Output is correct |
5 |
Correct |
4 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3576 KB |
Output is correct |
7 |
Correct |
5 ms |
3448 KB |
Output is correct |
8 |
Correct |
5 ms |
3448 KB |
Output is correct |
9 |
Correct |
5 ms |
3448 KB |
Output is correct |
10 |
Correct |
5 ms |
3452 KB |
Output is correct |
11 |
Correct |
5 ms |
3448 KB |
Output is correct |
12 |
Correct |
5 ms |
3448 KB |
Output is correct |
13 |
Correct |
5 ms |
3448 KB |
Output is correct |
14 |
Correct |
5 ms |
3452 KB |
Output is correct |
15 |
Correct |
5 ms |
3448 KB |
Output is correct |
16 |
Correct |
5 ms |
3448 KB |
Output is correct |
17 |
Correct |
5 ms |
3448 KB |
Output is correct |
18 |
Correct |
5 ms |
3448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3704 KB |
Output is correct |
2 |
Correct |
4 ms |
3448 KB |
Output is correct |
3 |
Correct |
4 ms |
3448 KB |
Output is correct |
4 |
Correct |
5 ms |
3448 KB |
Output is correct |
5 |
Correct |
4 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3576 KB |
Output is correct |
7 |
Correct |
5 ms |
3448 KB |
Output is correct |
8 |
Correct |
5 ms |
3448 KB |
Output is correct |
9 |
Correct |
5 ms |
3448 KB |
Output is correct |
10 |
Correct |
5 ms |
3452 KB |
Output is correct |
11 |
Correct |
5 ms |
3448 KB |
Output is correct |
12 |
Correct |
5 ms |
3448 KB |
Output is correct |
13 |
Correct |
5 ms |
3448 KB |
Output is correct |
14 |
Correct |
5 ms |
3452 KB |
Output is correct |
15 |
Correct |
5 ms |
3448 KB |
Output is correct |
16 |
Correct |
5 ms |
3448 KB |
Output is correct |
17 |
Correct |
5 ms |
3448 KB |
Output is correct |
18 |
Correct |
5 ms |
3448 KB |
Output is correct |
19 |
Correct |
6 ms |
3448 KB |
Output is correct |
20 |
Correct |
6 ms |
3448 KB |
Output is correct |
21 |
Correct |
6 ms |
3476 KB |
Output is correct |
22 |
Correct |
6 ms |
3452 KB |
Output is correct |
23 |
Correct |
5 ms |
3448 KB |
Output is correct |
24 |
Correct |
5 ms |
3576 KB |
Output is correct |
25 |
Correct |
5 ms |
3520 KB |
Output is correct |
26 |
Correct |
5 ms |
3576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
400 ms |
8560 KB |
Output is correct |
2 |
Correct |
403 ms |
8588 KB |
Output is correct |
3 |
Correct |
404 ms |
8568 KB |
Output is correct |
4 |
Correct |
402 ms |
8652 KB |
Output is correct |
5 |
Correct |
202 ms |
8136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
4736 KB |
Output is correct |
2 |
Correct |
92 ms |
4728 KB |
Output is correct |
3 |
Correct |
88 ms |
4904 KB |
Output is correct |
4 |
Correct |
47 ms |
4724 KB |
Output is correct |
5 |
Correct |
5 ms |
3448 KB |
Output is correct |
6 |
Correct |
52 ms |
4596 KB |
Output is correct |
7 |
Correct |
75 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
6052 KB |
Output is correct |
2 |
Correct |
129 ms |
6124 KB |
Output is correct |
3 |
Correct |
273 ms |
8568 KB |
Output is correct |
4 |
Correct |
147 ms |
8056 KB |
Output is correct |
5 |
Correct |
79 ms |
5852 KB |
Output is correct |
6 |
Correct |
140 ms |
7928 KB |
Output is correct |
7 |
Correct |
144 ms |
8824 KB |
Output is correct |
8 |
Correct |
103 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3704 KB |
Output is correct |
2 |
Correct |
4 ms |
3448 KB |
Output is correct |
3 |
Correct |
4 ms |
3448 KB |
Output is correct |
4 |
Correct |
5 ms |
3448 KB |
Output is correct |
5 |
Correct |
4 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3576 KB |
Output is correct |
7 |
Correct |
5 ms |
3448 KB |
Output is correct |
8 |
Correct |
5 ms |
3448 KB |
Output is correct |
9 |
Correct |
5 ms |
3448 KB |
Output is correct |
10 |
Correct |
5 ms |
3452 KB |
Output is correct |
11 |
Correct |
5 ms |
3448 KB |
Output is correct |
12 |
Correct |
5 ms |
3448 KB |
Output is correct |
13 |
Correct |
5 ms |
3448 KB |
Output is correct |
14 |
Correct |
5 ms |
3452 KB |
Output is correct |
15 |
Correct |
5 ms |
3448 KB |
Output is correct |
16 |
Correct |
5 ms |
3448 KB |
Output is correct |
17 |
Correct |
5 ms |
3448 KB |
Output is correct |
18 |
Correct |
5 ms |
3448 KB |
Output is correct |
19 |
Correct |
6 ms |
3448 KB |
Output is correct |
20 |
Correct |
6 ms |
3448 KB |
Output is correct |
21 |
Correct |
6 ms |
3476 KB |
Output is correct |
22 |
Correct |
6 ms |
3452 KB |
Output is correct |
23 |
Correct |
5 ms |
3448 KB |
Output is correct |
24 |
Correct |
5 ms |
3576 KB |
Output is correct |
25 |
Correct |
5 ms |
3520 KB |
Output is correct |
26 |
Correct |
5 ms |
3576 KB |
Output is correct |
27 |
Correct |
400 ms |
8560 KB |
Output is correct |
28 |
Correct |
403 ms |
8588 KB |
Output is correct |
29 |
Correct |
404 ms |
8568 KB |
Output is correct |
30 |
Correct |
402 ms |
8652 KB |
Output is correct |
31 |
Correct |
202 ms |
8136 KB |
Output is correct |
32 |
Correct |
87 ms |
4736 KB |
Output is correct |
33 |
Correct |
92 ms |
4728 KB |
Output is correct |
34 |
Correct |
88 ms |
4904 KB |
Output is correct |
35 |
Correct |
47 ms |
4724 KB |
Output is correct |
36 |
Correct |
5 ms |
3448 KB |
Output is correct |
37 |
Correct |
52 ms |
4596 KB |
Output is correct |
38 |
Correct |
75 ms |
4728 KB |
Output is correct |
39 |
Correct |
128 ms |
6052 KB |
Output is correct |
40 |
Correct |
129 ms |
6124 KB |
Output is correct |
41 |
Correct |
273 ms |
8568 KB |
Output is correct |
42 |
Correct |
147 ms |
8056 KB |
Output is correct |
43 |
Correct |
79 ms |
5852 KB |
Output is correct |
44 |
Correct |
140 ms |
7928 KB |
Output is correct |
45 |
Correct |
144 ms |
8824 KB |
Output is correct |
46 |
Correct |
103 ms |
6008 KB |
Output is correct |
47 |
Correct |
185 ms |
6000 KB |
Output is correct |
48 |
Correct |
183 ms |
6008 KB |
Output is correct |
49 |
Correct |
400 ms |
8668 KB |
Output is correct |
50 |
Correct |
184 ms |
8184 KB |
Output is correct |
51 |
Correct |
164 ms |
7076 KB |
Output is correct |
52 |
Correct |
249 ms |
8316 KB |
Output is correct |
53 |
Correct |
148 ms |
8312 KB |
Output is correct |
54 |
Correct |
153 ms |
8972 KB |
Output is correct |
55 |
Correct |
324 ms |
8608 KB |
Output is correct |