#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <array>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT
freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
/* online submission */
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
}
/*************************CEOI 2018 Day 1 P2*************************/
template<int SZ, class T> struct SegmentTree {
T data[2 * SZ];
int _N;
T id = 0; // identity function (a,id) = a; (id,a) = a;
T combine(T a, T b) {
return max(a, b);
}
SegmentTree() {}
void clear(int n) {
_N = n;
FOR(i, 0, 2 * _N) data[i] = id;
}
void update(int p, T val) { // set value at i to val !not add val!
data[p + _N] = val;
for (p += _N; p > 1; p >>= 1)
data[p >> 1] = combine(data[p], data[p ^ 1]);
}
T query(int l, int r) {
if (l > r) return id;
r++;
T resl = id, resr = id;
for (l += _N, r += _N; l < r; l >>= 1, r >>= 1) {
if (l & 1) resl = combine(resl, data[l++]);
if (r & 1) resr = combine(data[--r], resr);
}
return combine(resl, resr);
}
};
const int MX = 200099;
SegmentTree<MX, int> tree;
int n, A[MX], x;
int bacc[MX], forw[MX];
inline bool cmpa(int i, int j) {
return A[i] < A[j];
}
int addlis(int x, v<int> &cur) {
int idx = lower_bound(all(cur), x) - cur.begin();
if (idx == cur.size()) cur.push_back(x);
else cur[idx] = x;
return idx;
}
int main() {
io();
cin >> n >> x;
FOR(i, 0, n) cin >> A[i];
v<int> temp = { INT_MIN };
FOR(i, 0, n)
bacc[i] = addlis(A[i], temp);
temp = { INT_MIN };
RFORE(i, n - 1, 0)
forw[i] = addlis(-A[i], temp);
// darr(forw, n);
// darr(bacc, n);
v<int> idx(n);
FOR(i, 0, n) idx[i] = i;
sort(all(idx), cmpa);
int ans = 0;
// +x
tree.clear(n);
for (int pi = n - 1, pj = n - 1; pi >= 0; pi--) {
while (pj >= 0 && A[idx[pj]] >= A[idx[pi]] - x + 1) {
tree.update(idx[pj], forw[idx[pj]]);
pj--;
}
maxx(ans, bacc[idx[pi]] + tree.query(idx[pi] + 1, n - 1));
}
// -x
tree.clear(n);
for (int pi = 0, pj = 0; pi < n; pi++) {
while (pj < n && A[idx[pj]] <= A[idx[pi]] + x - 1) {
tree.update(idx[pj], bacc[idx[pj]]);
pj++;
}
maxx(ans, forw[idx[pi]] + tree.query(0, idx[pi] - 1));
}
cout << ans << "\n";
}
Compilation message
glo.cpp: In function 'int addlis(int, std::vector<int>&)':
glo.cpp:85:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (idx == cur.size()) cur.push_back(x);
~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
420 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
5088 KB |
Output is correct |
2 |
Correct |
129 ms |
5036 KB |
Output is correct |
3 |
Correct |
129 ms |
5112 KB |
Output is correct |
4 |
Correct |
133 ms |
5112 KB |
Output is correct |
5 |
Correct |
94 ms |
5620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
1528 KB |
Output is correct |
2 |
Correct |
29 ms |
1528 KB |
Output is correct |
3 |
Correct |
29 ms |
1528 KB |
Output is correct |
4 |
Correct |
20 ms |
1784 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
19 ms |
2040 KB |
Output is correct |
7 |
Correct |
26 ms |
2040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
2808 KB |
Output is correct |
2 |
Correct |
63 ms |
2680 KB |
Output is correct |
3 |
Correct |
131 ms |
5012 KB |
Output is correct |
4 |
Correct |
81 ms |
5592 KB |
Output is correct |
5 |
Correct |
39 ms |
3600 KB |
Output is correct |
6 |
Correct |
72 ms |
6516 KB |
Output is correct |
7 |
Correct |
78 ms |
7324 KB |
Output is correct |
8 |
Correct |
52 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
420 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
150 ms |
5088 KB |
Output is correct |
28 |
Correct |
129 ms |
5036 KB |
Output is correct |
29 |
Correct |
129 ms |
5112 KB |
Output is correct |
30 |
Correct |
133 ms |
5112 KB |
Output is correct |
31 |
Correct |
94 ms |
5620 KB |
Output is correct |
32 |
Correct |
30 ms |
1528 KB |
Output is correct |
33 |
Correct |
29 ms |
1528 KB |
Output is correct |
34 |
Correct |
29 ms |
1528 KB |
Output is correct |
35 |
Correct |
20 ms |
1784 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
19 ms |
2040 KB |
Output is correct |
38 |
Correct |
26 ms |
2040 KB |
Output is correct |
39 |
Correct |
63 ms |
2808 KB |
Output is correct |
40 |
Correct |
63 ms |
2680 KB |
Output is correct |
41 |
Correct |
131 ms |
5012 KB |
Output is correct |
42 |
Correct |
81 ms |
5592 KB |
Output is correct |
43 |
Correct |
39 ms |
3600 KB |
Output is correct |
44 |
Correct |
72 ms |
6516 KB |
Output is correct |
45 |
Correct |
78 ms |
7324 KB |
Output is correct |
46 |
Correct |
52 ms |
3704 KB |
Output is correct |
47 |
Correct |
61 ms |
3676 KB |
Output is correct |
48 |
Correct |
63 ms |
3692 KB |
Output is correct |
49 |
Correct |
146 ms |
7032 KB |
Output is correct |
50 |
Correct |
84 ms |
6772 KB |
Output is correct |
51 |
Correct |
72 ms |
5332 KB |
Output is correct |
52 |
Correct |
83 ms |
6900 KB |
Output is correct |
53 |
Correct |
75 ms |
6836 KB |
Output is correct |
54 |
Correct |
81 ms |
7516 KB |
Output is correct |
55 |
Correct |
116 ms |
7032 KB |
Output is correct |