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 = 1e6;
const int OFF = 1 << 21;
int n, d, a[MAXN], tour[OFF], ans;
priority_queue<int, vector<int>, greater<int> > pq;
multiset<int> ms;
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;
vector<int> cmp;
unordered_map<int, int> um;
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]];
for(int i = 0; i < n; ++i){
int x = max(1, f(0, 0, n - 1, a[i], a[i]));
if(a[i]) x = max(x, f(0, 0, n - 1, 0, a[i] - 1) + 1);
upd(0, 0, n - 1, a[i], x);
ans = max(ans, x);
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 idx = pq.top();
upd(0, 0, n - 1, idx, 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... |