This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int N = 3e5 + 10;
const int M = 7e3 + 10;
const int INF = 1e9 + 7;
int a[N];
int dp[M][M], mx[2][M];
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, d;
cin >> n >> d;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 0;
if(d == 1) {
int ma = 0;
stack <int> stk;
for(int i = n; i >= 1; i--) {
while(!stk.empty() and stk.top() <= a[i]) {
stk.pop();
}
stk.push(a[i]);
ma = max(ma, (int)stk.size());
}
ans = ma;
}
else if(d == n) {
vector <int> lis;
for(int i = 1; i <= n; i++) {
auto it = lower_bound(lis.begin(), lis.end(), a[i]);
if(it == lis.end()) {
lis.push_back(a[i]);
}
else {
*it = a[i];
}
}
ans = lis.size();
}
else if(n <= 7000) {
vector <int> vec;
vec.push_back(-INF);
for(int i = 1; i <= n; i++) {
vec.push_back(a[i]);
}
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
}
for(int j = 1; j < vec.size(); j++) {
int now = j % 2;
int prv = now ^ 1;
deque <pair <int, int>> dq1, dq2;
for(int i = 1; i <= n; i++) {
while(!dq1.empty() and dq1.front().second < i - d) {
dq1.pop_front();
}
while(!dq2.empty() and dq2.front().second < i - d) {
dq2.pop_front();
}
if(j == a[i]) {
int ma = 0;
if(!dq1.empty()) {
ma = max(ma, dq1.front().first);
}
dp[i][j] = ma + 1;
}
if(j >= a[i] and !dq2.empty()) {
dp[i][j] = max(dp[i][j], dq2.front().first);
}
while(!dq1.empty() and dq1.back().first <= mx[prv][i]) {
dq1.pop_back();
}
while(!dq2.empty() and dq2.back().first <= dp[i][j]) {
dq2.pop_back();
}
dq1.emplace_back(mx[prv][i], i);
dq2.emplace_back(dp[i][j], i);
}
for(int i = 1; i <= n; i++) {
mx[now][i] = max(mx[now][i], dp[i][j]);
}
ans = max(ans, mx[now][n]);
}
}
else {
assert(false);
}
cout << ans;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int j = 1; j < vec.size(); j++) {
| ~~^~~~~~~~~~~~
# | 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... |