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;
template<class T>
struct SegTree {
int n;
vector<T> tree;
void init(int _n) {
n = _n;
tree.resize(4*n+5);
}
void update(int u, int tl, int tr, int p, T 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]);
}
}
T 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); }
T query(int l, int r) { return query(1, 0, n-1, l, r); }
};
short dp[7005][7005], pref[7005][7005], suf[7005][7005];
deque<pair<short, short> > dq[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<int> tree; tree.init(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;
}
//aaaah fucking multiset
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 ? short(pref[k][v[i]-1] + 1) : (short)1));
for(int j=v[i]; j<m; j++) if(!dq[j].empty()) dp[i][j] = max(dp[i][j], dq[j].back().first);
pref[i][0] = dp[i][0];
for(int j=1; j<m; j++) pref[i][j] = max(pref[i][j-1], dp[i][j]);
for(int j=0; j<m; j++) {
while(!dq[j].empty() && dp[i][j] >= dq[j].back().first) dq[j].pop_back();
dq[j].push_back({ dp[i][j], i });
}
for(int j=0; j<m; j++) if(!dq[j].empty() && dq[j].front().second <= i - d) dq[j].pop_front();
}
short 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... |