Submission #1130438

#TimeUsernameProblemLanguageResultExecution timeMemory
1130438VMaksimoski008Financial Report (JOI21_financial)C++17
19 / 100
582 ms46208 KiB
#include <bits/stdc++.h> #define ar array //#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 union_find { int n; vector<int> par, size, L, R, mx; union_find(int _n) : n(_n), par(n+1), size(n+1, 1), L(n+1), R(n+1), mx(n+1, 0) { for(int i=1; i<=n; i++) par[i] = L[i] = R[i] = i; } int find(int u) { if(u == par[u]) return u; return par[u] = find(par[u]); } void uni(int a, int b) { a = find(a); b = find(b); if(a == b) return ; if(size[a] < size[b]) swap(a, b); size[a] += size[b]; par[b] = a; L[a] = min(L[a], L[b]); R[a] = max(R[a], R[b]); mx[a] = max(mx[a], mx[b]); } void set_dp(int u, int val) { mx[find(u)] = max(mx[find(u)], val); } int get_l(int u) { return L[find(u)]; } int get_r(int u) { return R[find(u)]; } int get_dp(int u) { return mx[find(u)]; } }; struct segment_tree { int n; vector<int> tree; segment_tree(int _n) : n(_n), tree(4*n) {} void update(int u, int tl, int tr, int p, int v) { if(tl == tr) { // cout << "at " << p << ", " << v << '\n'; 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(tl > r || l > tr) 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, 1, n, p, v); } int query(int l, int r) { return query(1, 1, n, l, r); } }; signed main() { int n, d; cin >> n >> d; vector<int> dp(n+1, 1); map<int, vector<int> > mp; for(int i=1; i<=n; i++) { int x; cin >> x; mp[x].push_back(i); } union_find dsu(n); segment_tree sgt(n); vector<int> act(n+1); for(auto [val, vec] : mp) { for(int p : vec) { dp[p] = sgt.query(max(1, p-d), p-1) + 1; if(p > 1 && act[p-1]) dsu.uni(p-1, p); if(p < n && act[p+1]) dsu.uni(p, p+1); // for(int i=1; i<=n; i++) cout << sgt.query(i, i) << " "; // cout << '\n'; sgt.update(dsu.get_r(p), dsu.get_dp(p)); } for(int p : vec) { dsu.set_dp(p, dp[p]); sgt.update(dsu.get_r(p), dsu.get_dp(p)); act[p] = 1; } } cout << *max_element(dp.begin(), dp.end()) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...