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;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }
constexpr int nax = 300300;
int n,d;
int a[nax];
int b[nax];
int dp[nax];
int st[4*nax];
multiset<int> s[nax];
void upd(int p,int x,int u=1,int tl=0,int tr=nax) {
if (tl == tr) {
st[u] = x;
return;
}
int mid=(tl+tr)/2;
if (p <= mid) {
upd(p,x,2*u,tl,mid);
} else {
upd(p,x,2*u+1,mid+1,tr);
}
st[u] = max(st[2*u],st[2*u+1]);
}
int qry(int l,int r,int u=1,int tl=0,int tr=nax) {
if (l > r) { return 0; }
if (l == tl && r == tr) {
return st[u];
}
int mid=(tl+tr)/2;
return max(qry(l,min(r,mid),2*u,tl,mid),qry(max(mid+1,l),r,2*u+1,mid+1,tr));
}
int main() {
cin >> n >> d;
for (int i=0;i<n;i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b,b+n);
for (int i=0;i<n;i++) {
a[i] = lower_bound(b,b+n,a[i])-b;
}
int mx = 0;
for (int i=0;i<n;i++) {
if (i > d) {
int w = a[i-d-1];
auto it = s[w].find(dp[i-d-1]);
assert(it!=s[w].end());
s[w].erase(it);
upd(w,(s[w].empty() ? 0 : *s[w].rbegin()));
}
int w = a[i];
int c = qry(0,w-1)+1;
s[w].insert(c);
upd(w,*s[w].rbegin());
dp[i] = c;
cmax(mx,dp[i]);
// cerr << c << endl;
}
cout << mx << endl;
}
# | 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... |