Submission #922405

#TimeUsernameProblemLanguageResultExecution timeMemory
922405AkibAzmainCatfish Farm (IOI22_fish)C++17
100 / 100
834 ms53728 KiB
#include "fish.h" #include <bits/stdc++.h> using namespace std; using ll = long long; long long max_weights(int n, int m, std::vector<int> fx, std::vector<int> fy, std::vector<int> fw) { n += 2; vector < map < int, ll > > a (n); for (int i = 0; i < m; ++i) a[fx[i] + 1][fy[i]] += fw[i]; ll mxdp = 0; vector < vector < pair < int, ll > > > p (3), q (2), dp (3); for (int i = 2; i < n; ++i) { set < int > is; for (int j = 0; j < 3; ++j) for (auto &x : a[i - j]) is.insert (x.first); ll ls = 0, rs = 0, ms = 0; ll mxp = -1e18, mxq = -1e18; ll id = -1; stack < int > st; auto itp = p[2].begin (); auto itq = q[1].begin (); for (auto &x : is) { st.push (x); ls += a[i - 2].count (x) ? a[i - 2][x] : 0ll; ms += a[i - 1].count (x) ? a[i - 1][x] : 0ll; rs += a[i].count (x) ? a[i][x] : 0ll; if (a[i].count (x) + a[i - 2].count (x) == 0) continue; dp[0].push_back ({ x, 0 }); p[0].push_back ({ x, 0 }); q[0].push_back ({ x, 0 }); ++id; dp[0][id].second = ls + rs + mxdp; while (itp != p[2].end () && itp->first <= x) mxp = max (mxp, itp->second), ++itp; while (itq != q[1].end () && itq->first <= x) mxq = max (mxq, itq->second), ++itq; dp[0][id].second = max (dp[0][id].second, ls + rs + mxp); dp[0][id].second = max (dp[0][id].second, ls + rs + mxq); } mxp = -1e18, mxq = -1e18; auto ritp = dp[2].rbegin (), ritq = dp[1].rbegin (); while (st.size ()) { int x = st.top (); st.pop (); if (a[i].count (x) + a[i - 2].count (x) == 0) goto cont; while (ritp != dp[2].rend () && ritp->first >= x) mxp = max (mxp, ritp->second), ++ritp; while (ritq != dp[1].rend () && ritq->first >= x) mxq = max (mxq, ritq->second), ++ritq; dp[0][id].second = max (dp[0][id].second, rs + mxp); q[0][id].second = dp[0][id].second - ms - rs; dp[0][id].second = max (dp[0][id].second, rs + mxq - ms); p[0][id].second = dp[0][id].second - rs; --id; cont: ls -= a[i - 2].count (x) ? a[i - 2][x] : 0ll; ms -= a[i - 1].count (x) ? a[i - 1][x] : 0ll; rs -= a[i].count (x) ? a[i][x] : 0ll; } swap (p[2], p[1]); swap (p[1], p[0]); p[0].clear (); swap (q[1], q[0]); q[0].clear (); swap (dp[2], dp[1]); swap (dp[1], dp[0]); for (auto &x : dp[0]) mxdp = max (mxdp, x.second); dp[0].clear (); } for (int i = 1; i <= 2; ++i) for (auto &x : dp[i]) mxdp = max (mxdp, x.second); return mxdp; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...