Submission #1099991

#TimeUsernameProblemLanguageResultExecution timeMemory
1099991marvinthangTricks of the Trade (CEOI23_trade)C++17
100 / 100
1388 ms51596 KiB
/************************************* * author: marvinthang * * created: 14.03.2024 19:06:02 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i-- > 0; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } template <class A, class B> bool minimize(A &a, B b) { return a > b ? a = b, true : false; } template <class A, class B> bool maximize(A &a, B b) { return a < b ? a = b, true : false; } // end of template const long long INF = 1e18; void process(void) { int n, k; cin >> n >> k; vector <int> buy(n), sell(n); cin >> buy >> sell; priority_queue <pair <int, int>, vector<pair <int, int>>, greater<pair <int, int>>> k_largest; priority_queue <pair <int, int>> remains; int sz_kl = 0; long long cur_sum = 0; int cur_l = 0, cur_r = 0; vector <int> in_pq(n); auto add = [&] (int i) { cur_sum -= buy[i]; if (sz_kl < k) { cur_sum += sell[i]; k_largest.emplace(sell[i], i); in_pq[i] = 2; ++sz_kl; } else { while (in_pq[k_largest.top().se] != 2) k_largest.pop(); auto [sell_j, j] = k_largest.top(); if (sell[i] > sell_j) { remains.emplace(sell_j, j); k_largest.pop(); in_pq[j] = 1; k_largest.emplace(sell[i], i); in_pq[i] = 2; cur_sum += sell[i] - sell_j; } else { remains.emplace(sell[i], i); in_pq[i] = 1; } } }; auto del = [&] (int i) { cur_sum += buy[i]; if (in_pq[i] == 2) { while (in_pq[remains.top().se] != 1) remains.pop(); auto [sell_j, j] = remains.top(); remains.pop(); k_largest.emplace(sell_j, j); in_pq[j] = 2; cur_sum += sell_j - sell[i]; } in_pq[i] = 0; }; auto calc = [&] (int l, int r) { while (cur_r < r) add(cur_r++); while (cur_l > l) add(--cur_l); while (cur_r > r) del(--cur_r); while (cur_l < l) del(cur_l++); return cur_sum; }; vector <long long> dp(n, -INF); vector <int> opt(n); auto solve = [&] (auto &&self, int l, int r, int from, int to) -> void { if (l > r) return; int m = l + r >> 1; FORE(i, max(from, m + k), to) if (maximize(dp[m], calc(m, i))) opt[m] = i; self(self, l, m - 1, from, opt[m]); self(self, m + 1, r, opt[m], to); }; solve(solve, 0, n - k, k, n); long long max_profit = *max_element(ALL(dp)); cout << max_profit << '\n'; int r = n; vector <vector <int>> insertAt(n), eraseAt(n); REPD(l, n) if (dp[l] == max_profit) { FORDE(i, r, opt[l]) if (calc(l, i) == max_profit) { while (in_pq[k_largest.top().se] != 2) k_largest.pop(); insertAt[l].push_back(k_largest.top().fi); if (i < n) eraseAt[i].push_back(k_largest.top().fi); } r = opt[l]; } multiset <int> current; REP(i, n) { for (int v: insertAt[i]) current.insert(v); for (int v: eraseAt[i]) current.erase(current.find(v)); cout << (!current.empty() && sell[i] >= *current.begin()); } } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); file("trade"); // int t; cin >> t; while (t--) process(); // cerr << "Time elapsed: " << TIME << " s.\n"; return (0^0); }

Compilation message (stderr)

trade.cpp: In instantiation of 'process()::<lambda(auto:23&&, int, int, int, int)> [with auto:23 = process()::<lambda(auto:23&&, int, int, int, int)>&]':
trade.cpp:103:29:   required from here
trade.cpp:98:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   98 |   int m = l + r >> 1;
      |           ~~^~~
trade.cpp: In function 'int main()':
trade.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
trade.cpp:126:2: note: in expansion of macro 'file'
  126 |  file("trade");
      |  ^~~~
trade.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
trade.cpp:126:2: note: in expansion of macro 'file'
  126 |  file("trade");
      |  ^~~~
#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...