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>
using namespace std;
const int MAXN = 3e5 + 2;
const int OFF = 1 << 20;
int n, d, a[MAXN], ans, dp[7005];
bool ok[7005];
int tour[OFF];
priority_queue<int, vector<int>, greater<int> > pq;
multiset<int> ms;
vector<int> cmp;
map<int, int> um;
int f(int x, int l, int r, int ql, int qr){
if(ql <= l && r <= qr) return tour[x];
if(ql > r || l > qr) return 0;
int mid = (l + r) >> 1;
return max(f(x * 2 + 1, l, mid, ql, qr), f(x * 2 + 2, mid + 1, r, ql, qr));
}
void upd(int x, int l, int r, int i, int val){
if(l > i || r < i) return ;
if(l == r){
tour[x] = val;
return ;
}
int mid = (l + r) >> 1;
upd(x * 2 + 1, l, mid, i, val);
upd(x * 2 + 2, mid + 1, r, i, val);
tour[x] = max(tour[x * 2 + 1], tour[x * 2 + 2]);
}
int main(){
cin >> n >> d;
for(int i = 0; i < n; ++i){
cin >> a[i];
cmp.push_back(a[i]);
}
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
for(int i = 0; i < (int)cmp.size(); ++i) um[cmp[i]] = i;
for(int i = 0; i < n; ++i) a[i] = um[a[i]];
if(n < 7005){
memset(ok, 1, sizeof ok);
for(int i = 0; i < n; ++i){
dp[i] = 1;
for(int j = i - 1; j >= 0; --j){
if(ok[j] && a[j] < a[i]) dp[i] = max(dp[i], dp[j] + 1);
}
// assume [i - d + 1..i] blocks j <= i - d
if(i - d >= 0){
int mini = n;
for(int j = i; j > i - d; --j) mini = min(mini, a[j]);
for(int j = i - d; j >= 0; --j){
if(a[j] < mini) ok[j] = 0;
}
}
ans = max(ans, dp[i]);
}
cout << ans << "\n";
return 0;
}
for(int i = 0; i < n; ++i){
int x = 0;
if(a[i]) x = f(0, 0, n - 1, 0, a[i] - 1);
upd(0, 0, n - 1, a[i], x + 1);
ans = max(ans, x + 1);
ms.insert(a[i]);
pq.push(a[i]);
if(i >= d){
ms.erase(ms.find(a[i - d]));
int mini = *ms.begin();
while(!pq.empty() && pq.top() < mini){
int x = pq.top();
upd(0, 0, n - 1, x, 0);
pq.pop();
}
}
}
cout << ans;
return 0;
}
# | 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... |