This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// CF template, version 3.0
#include <bits/stdc++.h>
using namespace std;
#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}
#define int long long int
vector<int> tree;
int n;
void upd(int i, int x, int l = 0, int r = n - 1, int v = 1) {
if (l == r) {
tree[v] = x;
} else {
int middle = (l + r) / 2;
if (i <= middle) {
upd(i, x, l, middle, 2 * v);
} else {
upd(i, x, middle + 1, r, 2 * v + 1);
}
tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}
}
int rq(int ql, int qr, int l = 0, int r = n - 1, int v = 1) {
if (ql <= l && r <= qr) {
return tree[v];
} else if (qr < l || r < ql) {
return 0;
} else {
int middle = (l + r) / 2;
int left = rq(ql, qr, l, middle, 2 * v);
int right = rq(ql, qr, middle + 1, r, 2 * v + 1);
return max(left, right);
}
}
int query(vector<vector<int>>& sparse, int l, int r) {
int sz = r - l + 1;
int lg = 0;
int p = 1;
while (p <= sz) {
p *= 2;
lg++;
}
p /= 2;
lg--;
int left = sparse[l][lg];
int right = sparse[r - p + 1][lg];
return max(left, right);
}
signed main() {
improvePerformance;
cin >> n;
get(d);
getList(n, nums);
vector<int> minimums(n, 1e18);
multiset<int> window;
forto(d, i) window.insert(nums[n - i - 1]);
for (int i = n - 1; i >= d - 1; i--) {
minimums[i] = *window.begin();
window.erase(window.find(nums[i]));
if (i != d - 1) window.insert(nums[i - d]);
}
vector<vector<int>> sparse(n, vector<int>(19, 0));
for (int i = n - 1; i >= 0; i--) {
sparse[i][0] = minimums[i];
int p = 2;
forto(19, j) {
if (j == 0) continue;
if (i + p > n) break;
sparse[i][j] = max(sparse[i][j - 1], sparse[i + p / 2][j - 1]);
p *= 2;
}
}
vector<int> dp;
forto(n, i) dp.push_back(0);
tree.resize(4 * n, 0);
vector<pair<int, int>> sorted;
forto(n, i) sorted.push_back({nums[i], i});
sortl(sorted);
for (int j = n - 1; j >= 0; j--) {
int i = sorted[j].second;
// what's the smallest index j such that j >= i+d and minimums[j] > a[i]?
// then we can take any element in between.
// (sparse table for the first part)
// what about the second part, "taking any element in between"?
int l = min(i + d, n);
int r = n;
while (r - l >= 1) {
int middle = (l + r) / 2;
if (query(sparse, i + d, middle) <= nums[i]) l = middle + 1;
else r = middle;
}
int maximum = rq(i + 1, min(l, n - 1));
dp[i] = maximum + 1;
if (j != 0 && sorted[j].first != sorted[j - 1].first) {
int m = j;
while (m <= n - 1 && sorted[m].first == sorted[j].first) {
int ind = sorted[m].second;
upd(ind, dp[ind]);
m++;
}
}
}
out(*max_element(dp.begin(), dp.end()));
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:10:23: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
Main.cpp:69:5: note: in expansion of macro 'get'
69 | get(d);
| ^~~
Main.cpp:12:40: warning: unnecessary parentheses in declaration of 'nums' [-Wparentheses]
12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
| ^
Main.cpp:70:5: note: in expansion of macro 'getList'
70 | getList(n, nums);
| ^~~~~~~
Main.cpp:10:23: warning: unnecessary parentheses in declaration of 'a' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
Main.cpp:12:76: note: in expansion of macro 'get'
12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
| ^~~
Main.cpp:70:5: note: in expansion of macro 'getList'
70 | getList(n, nums);
| ^~~~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
Main.cpp:73:5: note: in expansion of macro 'forto'
73 | forto(d, i) window.insert(nums[n - i - 1]);
| ^~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
Main.cpp:83:9: note: in expansion of macro 'forto'
83 | forto(19, j) {
| ^~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
Main.cpp:91:5: note: in expansion of macro 'forto'
91 | forto(n, i) dp.push_back(0);
| ^~~~~
Main.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
Main.cpp:94:5: note: in expansion of macro 'forto'
94 | forto(n, i) sorted.push_back({nums[i], i});
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |