이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
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... |