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 int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
struct SegTree {
int n;
vector<int> tree;
SegTree(int _n) : n(_n), tree(4*_n+5) {}
void update(int u, int tl, int tr, int p, int v) {
if(tl == tr) {
tree[u] = max(tree[u], v);
} else {
int tm = (tl + tr) / 2;
if(p <= tm) update(2*u, tl, tm, p, v);
else update(2*u+1, tm+1, tr, p, v);
tree[u] = max(tree[2*u], tree[2*u+1]);
}
}
int query(int u, int tl, int tr, int l, int r) {
if(l > tr || tl > r) return 0;
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return max(query(2*u, tl, tm, l, r), query(2*u+1, tm+1, tr, l, r));
}
void update(int p, int v) { update(1, 0, n-1, p, v); }
int query(int l, int r) { return query(1, 0, n-1, l, r); }
};
int dp[7005][7005], pref[7005][7005], suf[7005][7005];
signed main() {
int n, d;
cin >> n >> d;
vector<int> v(n+1); set<int> s;
for(int i=1; i<=n; i++) cin >> v[i], s.insert(v[i]);
vector<int> comp(s.begin(), s.end());
for(int i=1; i<=n; i++) v[i] = lower_bound(comp.begin(), comp.end(), v[i]) - comp.begin();
int m = s.size();
//zemame bilo koja podsekvenca
if(d == n) {
int ans = 0;
SegTree tree(n);
for(int i=n; i>=1; i--) {
ans = max(ans, tree.query(v[i]+1, n-1) + 1);
tree.update(v[i], tree.query(v[i]+1, n-1) + 1);
}
cout << ans << '\n';
return 0;
}
//ako pocneme od i pa odime do n, odogovorot e kolku pati max ke se smeni
//stack???
//od desno na levo cuvame >= stack
if(d == 1) {
int ans = 0;
stack<pii> st;
for(int i=n; i>=1; i--) {
while(!st.empty() && st.top().first <= v[i]) st.pop();
st.push({ v[i], i });
ans = max(ans, (int)st.size());
}
cout << ans << '\n';
return 0;
}
for(int i=1; i<=n; i++) {
dp[i][v[i]] = 1;
for(int k=i-1; k>=max(1, i-d); k--)
for(int j=v[i]; j<m; j++) dp[i][j] = max(dp[i][j], dp[k][j]);
for(int k=i-1; k>=max(1, i-d); k--) dp[i][v[i]] = max(dp[i][v[i]], (v[i] >= 1 ? pref[k][v[i]-1] + 1 : 1));
pref[i][0] = dp[i][0];
for(int j=1; j<m; j++) pref[i][j] = max(pref[i][j-1], dp[i][j]);
}
int ans = 0;
for(int i=0; i<m; i++) ans = max(ans, dp[n][i]);
cout << ans << '\n';
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... |