//Dost SEFEROĞLU
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int MOD = 1e9+7,inf = 2e12;
const int N = 5001;
vi vv;
int idx(int x) {
return upper_bound(all(vv),x)-vv.begin();
}
struct FT{
int n;
vi bit;
FT(int nn) {
n = nn;
bit.assign(nn+1,0);
}
void put(int p,int v) {
for (int i=p;i<=n;i+=i&-i) bit[i]=max(bit[i],v);
}
int get(int p) {
int ans = 0;
for (int i=p;i>0;i-=i&-i) ans=max(ans,bit[i]);
return ans;
}
void reset() {
bit.assign(n+1,0);
}
};
struct ST {
vector<multiset<int,greater<int>>> t;
vector<int> ans;
ST(int nn) {
t.resize(4*nn+1);
ans.resize(4*nn+1);
}
void insert(int node,int l,int r,int p,int v) {
if (l == r) {
t[node].insert(v);
ans[node] = *t[node].begin();
return;
}
int m = (l+r) >> 1;
if (p<=m) insert(2*node,l,m,p,v);
else insert(2*node+1,m+1,r,p,v);
ans[node] = max(ans[2*node],ans[2*node+1]);
}
void erase(int node,int l,int r,int p,int v) {
if (l == r) {
t[node].erase(t[node].find(v));
if (t[node].empty()) ans[node] = 0;
else ans[node] = *t[node].begin();
return;
}
int m = (l+r) >> 1;
if (p<=m) erase(2*node,l,m,p,v);
else erase(2*node+1,m+1,r,p,v);
ans[node] = max(ans[2*node],ans[2*node+1]);
}
int query(int node,int l,int r,int L,int R) {
if (l > R || r < L) return 0;
if (l >= L && r<=R) return ans[node];
int m = (l+r) >> 1;
return max(query(2*node,l,m,L,R),query(2*node+1,m+1,r,L,R));
}
};
void solve() {
int n,x;
cin >> n >> x;
//corollary: always decrease some prefix
//corollary: if you decrease 1..i you MUST use i.
vi a(n+1);
for (int i=1;i<=n;i++) cin >> a[i];
for (int i=1;i<=n;i++) {
vv.push_back(a[i]);
vv.push_back(a[i]-x);
}
sort(all(vv));
vv.erase(unique(all(vv)),vv.end());
int sz = vv.size();
FT ft(sz);
vi p(n+1),s(n+1);
for (int i=1;i<=n;i++) {
p[i] = ft.get(idx(a[i])-1)+1;
ft.put(idx(a[i]),p[i]);
}
ft.reset();
for (int i=n;i>=1;i--) {
s[i] = ft.get(sz-idx(a[i]))+1;
ft.put(sz-idx(a[i])+1,s[i]);
}
ft.reset();
int ans = 0;
ST st(sz);
for (int i=1;i<=n;i++) st.insert(1,1,sz,idx(a[i]),s[i]);
for (int i=1;i<=n;i++) {
st.erase(1,1,sz,idx(a[i]),s[i]);
ans = max(ans,p[i]+st.query(1,1,sz,idx(a[i]-x)+1,sz));
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
508 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
353 ms |
63624 KB |
Output is correct |
2 |
Correct |
358 ms |
65216 KB |
Output is correct |
3 |
Correct |
369 ms |
65736 KB |
Output is correct |
4 |
Correct |
350 ms |
64840 KB |
Output is correct |
5 |
Correct |
156 ms |
41408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
27224 KB |
Output is correct |
2 |
Correct |
85 ms |
27340 KB |
Output is correct |
3 |
Correct |
87 ms |
27260 KB |
Output is correct |
4 |
Correct |
37 ms |
16084 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
38 ms |
16088 KB |
Output is correct |
7 |
Correct |
64 ms |
23500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
54464 KB |
Output is correct |
2 |
Correct |
186 ms |
54332 KB |
Output is correct |
3 |
Correct |
419 ms |
110180 KB |
Output is correct |
4 |
Correct |
161 ms |
64064 KB |
Output is correct |
5 |
Correct |
103 ms |
54896 KB |
Output is correct |
6 |
Correct |
178 ms |
104892 KB |
Output is correct |
7 |
Correct |
173 ms |
104716 KB |
Output is correct |
8 |
Correct |
138 ms |
55372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
508 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
353 ms |
63624 KB |
Output is correct |
28 |
Correct |
358 ms |
65216 KB |
Output is correct |
29 |
Correct |
369 ms |
65736 KB |
Output is correct |
30 |
Correct |
350 ms |
64840 KB |
Output is correct |
31 |
Correct |
156 ms |
41408 KB |
Output is correct |
32 |
Correct |
93 ms |
27224 KB |
Output is correct |
33 |
Correct |
85 ms |
27340 KB |
Output is correct |
34 |
Correct |
87 ms |
27260 KB |
Output is correct |
35 |
Correct |
37 ms |
16084 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
38 ms |
16088 KB |
Output is correct |
38 |
Correct |
64 ms |
23500 KB |
Output is correct |
39 |
Correct |
199 ms |
54464 KB |
Output is correct |
40 |
Correct |
186 ms |
54332 KB |
Output is correct |
41 |
Correct |
419 ms |
110180 KB |
Output is correct |
42 |
Correct |
161 ms |
64064 KB |
Output is correct |
43 |
Correct |
103 ms |
54896 KB |
Output is correct |
44 |
Correct |
178 ms |
104892 KB |
Output is correct |
45 |
Correct |
173 ms |
104716 KB |
Output is correct |
46 |
Correct |
138 ms |
55372 KB |
Output is correct |
47 |
Correct |
188 ms |
55240 KB |
Output is correct |
48 |
Correct |
200 ms |
55284 KB |
Output is correct |
49 |
Correct |
412 ms |
110784 KB |
Output is correct |
50 |
Correct |
177 ms |
64972 KB |
Output is correct |
51 |
Correct |
122 ms |
49860 KB |
Output is correct |
52 |
Correct |
183 ms |
64452 KB |
Output is correct |
53 |
Correct |
187 ms |
110472 KB |
Output is correct |
54 |
Correct |
186 ms |
110272 KB |
Output is correct |
55 |
Correct |
297 ms |
95428 KB |
Output is correct |