//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
struct Tree{
vector<int>tab;
int size = 1;
void prep(int n){
size = 1;
tab.clear();
while (size < n) size*=2;
tab.assign(2*size, 0);
}
void update(int x, int v){
x += size;
tab[x] = max(tab[x], v);
while (x){
x/=2;
tab[x] = max(tab[2*x], tab[2*x+1]);
}
}
int query(int l, int r){
int ans = 0;
for (l += size-1, r += size+1; r-l>1; l/=2, r/=2){
if (!(l&1)) ans = max(ans, tab[l+1]);
if (r&1) ans = max(ans, tab[r-1]);
}
return ans;
}
};
void solve(){
int n, d; cin >> n >> d;
vector<int>a(n+1);
for (int i = 1; i<=n; i++) cin >> a[i];
vector<int>suf(n+1);
auto s = a;
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
auto get = [&](int x){
return (int)(lower_bound(s.begin(), s.end(), x) - s.begin());
};
Tree t; t.prep(n+2);
for (int i = n; i>=1; i--){
suf[i] = 1 + t.query(get(a[i])+1, n+1);
t.update(get(a[i]), suf[i]);
}
for (int i = 1; i<=n; i++){
s.emplace_back(a[i]+d-1);
}
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
int ans = 1;
t.prep((int)s.size()+3);
for (int i = 1; i<=n; i++){
int dp = 1+t.query(0, get(a[i])-1);
ans = max(ans, suf[i] + t.query(0, get(a[i]+d-1)));
t.update(get(a[i]), dp);
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
20908 KB |
Output is correct |
2 |
Correct |
260 ms |
20776 KB |
Output is correct |
3 |
Correct |
271 ms |
20800 KB |
Output is correct |
4 |
Correct |
260 ms |
20932 KB |
Output is correct |
5 |
Correct |
104 ms |
11860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
5580 KB |
Output is correct |
2 |
Correct |
65 ms |
5576 KB |
Output is correct |
3 |
Correct |
64 ms |
5324 KB |
Output is correct |
4 |
Correct |
25 ms |
3220 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
30 ms |
3164 KB |
Output is correct |
7 |
Correct |
48 ms |
5572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
10564 KB |
Output is correct |
2 |
Correct |
120 ms |
10560 KB |
Output is correct |
3 |
Correct |
246 ms |
20792 KB |
Output is correct |
4 |
Correct |
106 ms |
11860 KB |
Output is correct |
5 |
Correct |
81 ms |
10276 KB |
Output is correct |
6 |
Correct |
132 ms |
19840 KB |
Output is correct |
7 |
Correct |
136 ms |
20356 KB |
Output is correct |
8 |
Correct |
99 ms |
10700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
265 ms |
20908 KB |
Output is correct |
28 |
Correct |
260 ms |
20776 KB |
Output is correct |
29 |
Correct |
271 ms |
20800 KB |
Output is correct |
30 |
Correct |
260 ms |
20932 KB |
Output is correct |
31 |
Correct |
104 ms |
11860 KB |
Output is correct |
32 |
Correct |
58 ms |
5580 KB |
Output is correct |
33 |
Correct |
65 ms |
5576 KB |
Output is correct |
34 |
Correct |
64 ms |
5324 KB |
Output is correct |
35 |
Correct |
25 ms |
3220 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
30 ms |
3164 KB |
Output is correct |
38 |
Correct |
48 ms |
5572 KB |
Output is correct |
39 |
Correct |
121 ms |
10564 KB |
Output is correct |
40 |
Correct |
120 ms |
10560 KB |
Output is correct |
41 |
Correct |
246 ms |
20792 KB |
Output is correct |
42 |
Correct |
106 ms |
11860 KB |
Output is correct |
43 |
Correct |
81 ms |
10276 KB |
Output is correct |
44 |
Correct |
132 ms |
19840 KB |
Output is correct |
45 |
Correct |
136 ms |
20356 KB |
Output is correct |
46 |
Correct |
99 ms |
10700 KB |
Output is correct |
47 |
Correct |
121 ms |
10688 KB |
Output is correct |
48 |
Correct |
126 ms |
10564 KB |
Output is correct |
49 |
Correct |
262 ms |
20876 KB |
Output is correct |
50 |
Correct |
118 ms |
11936 KB |
Output is correct |
51 |
Correct |
108 ms |
10068 KB |
Output is correct |
52 |
Correct |
177 ms |
12372 KB |
Output is correct |
53 |
Correct |
136 ms |
20212 KB |
Output is correct |
54 |
Correct |
139 ms |
20908 KB |
Output is correct |
55 |
Correct |
214 ms |
20908 KB |
Output is correct |