Submission #891304

#TimeUsernameProblemLanguageResultExecution timeMemory
891304loliconFinancial Report (JOI21_financial)C++14
100 / 100
422 ms42256 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; struct SGT { vector<int> tr; vector<multiset<int>> arr; SGT(int sz) : tr(sz << 2), arr(sz + 1) {} void pull(int x) { tr[x] = max(tr[x << 1], tr[(x << 1) | 1]); } void add(int x, int L, int R, int pos, int val) { if(L == R) { arr[L].insert(val); tr[x] = *arr[L].rbegin(); return; } int mid = (L + R) >> 1; if(pos <= mid) add(x << 1, L, mid, pos, val); else add((x << 1) | 1, mid + 1, R, pos, val); pull(x); } void kill(int x, int L, int R, int pos, int val) { if(L == R) { arr[L].erase(arr[L].find(val)); if(!arr[L].empty()) tr[x] = *arr[L].rbegin(); else tr[x] = 0; return; } int mid = (L + R) >> 1; if(pos <= mid) kill(x << 1, L, mid, pos, val); else kill((x << 1) | 1, mid + 1, R, pos, val); pull(x); } int ask(int x, int L, int R, int l, int r) { if(l > r) return 0; if(l <= L && R <= r) { return tr[x]; } int ret = 0, mid = (L + R) >> 1; if(l <= mid) ret = max(ret, ask(x << 1, L, mid, l, r)); if(r > mid) ret = max(ret, ask((x << 1) | 1, mid + 1, R, l, r)); return ret; } }; vector<int> V; int A[maxn]; int n, D; void getr(vector<int> &r) { set<int> st; vector<pair<int, int>> arr; vector<int> op(n + 1), ed(n + 1); for(int i = 1; i <= n; ++i) { arr.emplace_back(A[i], -i); } sort(begin(arr), end(arr)); for(int i = 0; i < n; ++i) { int pos = -arr[i].second; op[pos] = ed[pos] = pos; auto it = st.upper_bound(pos); if(it != st.end() && op[*it] < pos) { r[pos] = *it + D; continue; } it = st.insert(pos).first; // printf("insert %d\n", pos); auto xx = next(it); r[pos] = pos + D; if(xx != st.end()) { if(*xx - pos <= D) { int ll = op[pos]; int rr = ed[*xx]; // printf("merge (%d, %d), (%d, %d)\n", op[pos], ed[pos], op[*xx], ed[*xx]); ed[ll] = rr; op[rr] = ll; r[pos] = rr + D; if(pos != ll) st.erase(pos); if(*xx != rr) st.erase(*xx); } } if(it != st.begin()) { xx = prev(it); if(pos - *xx <= D) { int ll = op[*xx]; int rr = ed[pos]; // printf("merge (%d, %d), (%d, %d)\n", op[pos], ed[pos], op[*xx], ed[*xx]); ed[ll] = rr; op[rr] = ll; if(pos != rr) st.erase(pos); if(*xx != ll) st.erase(*xx); } } } } void solve() { vector<int> dp(n + 1), r(n + 1); // for(int i = 1; i <= n; ++i) { // int lst = i; // for(int j = i; j <= n; ++j) { // if(j - lst > D) break; // if(A[j] <= A[i]) lst = j; // } // r[i] = lst + D; // } // for(int i = 1; i <= n; ++i) printf("%d%c", r[i], " \n"[i == n]); getr(r); // for(int i = 1; i <= n; ++i) printf("%d%c", r[i], " \n"[i == n]); // O(n^2) dp SGT loli(n); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; for(int i = 1; i <= n; ++i) { // printf("here at %d\n", i); while(pq.size() && pq.top().first < i) { int pos = pq.top().second; loli.kill(1, 1, n, A[pos], dp[pos]); pq.pop(); } // printf("ask %d %d\n", 1, A[i] - 1); dp[i] = loli.ask(1, 1, n, 1, A[i] - 1) + 1; // printf("dp[%d] = %d\n", i, dp[i]); loli.add(1, 1, n, A[i], dp[i]); pq.emplace(r[i], i); // for(int j = 1; j < i; ++j) { // if(r[j] >= i && A[j] < A[i]) // dp[i] = max(dp[i], dp[j] + 1); // } } cout << *max_element(begin(dp), end(dp)) << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> D; for(int i = 1; i <= n; ++i) { cin >> A[i]; V.push_back(A[i]); } sort(begin(V), end(V)); for(int i = 1; i <= n; ++i) { A[i] = lower_bound(begin(V), end(V), A[i]) - begin(V) + 1; } solve(); 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...