/*************************************
* 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
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");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
408 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
17 ms |
976 KB |
Output is correct |
25 |
Correct |
22 ms |
1464 KB |
Output is correct |
26 |
Correct |
17 ms |
1308 KB |
Output is correct |
27 |
Correct |
13 ms |
1564 KB |
Output is correct |
28 |
Correct |
15 ms |
1000 KB |
Output is correct |
29 |
Correct |
11 ms |
1404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
657 ms |
31524 KB |
Output is correct |
3 |
Correct |
738 ms |
23504 KB |
Output is correct |
4 |
Correct |
1014 ms |
37512 KB |
Output is correct |
5 |
Correct |
1014 ms |
30680 KB |
Output is correct |
6 |
Correct |
831 ms |
27012 KB |
Output is correct |
7 |
Correct |
865 ms |
43444 KB |
Output is correct |
8 |
Correct |
690 ms |
23088 KB |
Output is correct |
9 |
Correct |
732 ms |
21840 KB |
Output is correct |
10 |
Correct |
726 ms |
27776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
657 ms |
31524 KB |
Output is correct |
3 |
Correct |
738 ms |
23504 KB |
Output is correct |
4 |
Correct |
1014 ms |
37512 KB |
Output is correct |
5 |
Correct |
1014 ms |
30680 KB |
Output is correct |
6 |
Correct |
831 ms |
27012 KB |
Output is correct |
7 |
Correct |
865 ms |
43444 KB |
Output is correct |
8 |
Correct |
690 ms |
23088 KB |
Output is correct |
9 |
Correct |
732 ms |
21840 KB |
Output is correct |
10 |
Correct |
726 ms |
27776 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
611 ms |
31960 KB |
Output is correct |
13 |
Correct |
804 ms |
23360 KB |
Output is correct |
14 |
Correct |
1006 ms |
37060 KB |
Output is correct |
15 |
Correct |
1012 ms |
30736 KB |
Output is correct |
16 |
Correct |
785 ms |
26996 KB |
Output is correct |
17 |
Correct |
841 ms |
43060 KB |
Output is correct |
18 |
Correct |
761 ms |
23048 KB |
Output is correct |
19 |
Correct |
768 ms |
21672 KB |
Output is correct |
20 |
Correct |
798 ms |
27780 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1179 ms |
23300 KB |
Output is correct |
32 |
Correct |
996 ms |
29476 KB |
Output is correct |
33 |
Correct |
1330 ms |
47264 KB |
Output is correct |
34 |
Correct |
1056 ms |
25564 KB |
Output is correct |
35 |
Correct |
1123 ms |
24568 KB |
Output is correct |
36 |
Correct |
973 ms |
30096 KB |
Output is correct |
37 |
Correct |
971 ms |
28092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
408 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
17 ms |
976 KB |
Output is correct |
27 |
Correct |
22 ms |
1464 KB |
Output is correct |
28 |
Correct |
17 ms |
1308 KB |
Output is correct |
29 |
Correct |
13 ms |
1564 KB |
Output is correct |
30 |
Correct |
15 ms |
1000 KB |
Output is correct |
31 |
Correct |
11 ms |
1404 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
657 ms |
31524 KB |
Output is correct |
34 |
Correct |
738 ms |
23504 KB |
Output is correct |
35 |
Correct |
1014 ms |
37512 KB |
Output is correct |
36 |
Correct |
1014 ms |
30680 KB |
Output is correct |
37 |
Correct |
831 ms |
27012 KB |
Output is correct |
38 |
Correct |
865 ms |
43444 KB |
Output is correct |
39 |
Correct |
690 ms |
23088 KB |
Output is correct |
40 |
Correct |
732 ms |
21840 KB |
Output is correct |
41 |
Correct |
726 ms |
27776 KB |
Output is correct |
42 |
Correct |
1 ms |
600 KB |
Output is correct |
43 |
Correct |
611 ms |
31960 KB |
Output is correct |
44 |
Correct |
804 ms |
23360 KB |
Output is correct |
45 |
Correct |
1006 ms |
37060 KB |
Output is correct |
46 |
Correct |
1012 ms |
30736 KB |
Output is correct |
47 |
Correct |
785 ms |
26996 KB |
Output is correct |
48 |
Correct |
841 ms |
43060 KB |
Output is correct |
49 |
Correct |
761 ms |
23048 KB |
Output is correct |
50 |
Correct |
768 ms |
21672 KB |
Output is correct |
51 |
Correct |
798 ms |
27780 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1179 ms |
23300 KB |
Output is correct |
63 |
Correct |
996 ms |
29476 KB |
Output is correct |
64 |
Correct |
1330 ms |
47264 KB |
Output is correct |
65 |
Correct |
1056 ms |
25564 KB |
Output is correct |
66 |
Correct |
1123 ms |
24568 KB |
Output is correct |
67 |
Correct |
973 ms |
30096 KB |
Output is correct |
68 |
Correct |
971 ms |
28092 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
660 ms |
31016 KB |
Output is correct |
71 |
Correct |
806 ms |
23504 KB |
Output is correct |
72 |
Correct |
1030 ms |
38188 KB |
Output is correct |
73 |
Correct |
997 ms |
30764 KB |
Output is correct |
74 |
Correct |
863 ms |
27220 KB |
Output is correct |
75 |
Correct |
828 ms |
42876 KB |
Output is correct |
76 |
Correct |
711 ms |
23052 KB |
Output is correct |
77 |
Correct |
683 ms |
21708 KB |
Output is correct |
78 |
Correct |
687 ms |
27872 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
460 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
468 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
1109 ms |
23096 KB |
Output is correct |
90 |
Correct |
943 ms |
29636 KB |
Output is correct |
91 |
Correct |
1369 ms |
47424 KB |
Output is correct |
92 |
Correct |
1083 ms |
25548 KB |
Output is correct |
93 |
Correct |
1050 ms |
24648 KB |
Output is correct |
94 |
Correct |
966 ms |
29428 KB |
Output is correct |
95 |
Correct |
950 ms |
28020 KB |
Output is correct |
96 |
Correct |
1 ms |
836 KB |
Output is correct |
97 |
Correct |
21 ms |
1036 KB |
Output is correct |
98 |
Correct |
16 ms |
1464 KB |
Output is correct |
99 |
Correct |
18 ms |
1420 KB |
Output is correct |
100 |
Correct |
9 ms |
1600 KB |
Output is correct |
101 |
Correct |
11 ms |
856 KB |
Output is correct |
102 |
Correct |
9 ms |
1216 KB |
Output is correct |
103 |
Correct |
587 ms |
41124 KB |
Output is correct |
104 |
Correct |
1164 ms |
25192 KB |
Output is correct |
105 |
Correct |
1129 ms |
26496 KB |
Output is correct |
106 |
Correct |
1388 ms |
35764 KB |
Output is correct |
107 |
Correct |
70 ms |
23496 KB |
Output is correct |
108 |
Correct |
939 ms |
37804 KB |
Output is correct |
109 |
Correct |
677 ms |
47640 KB |
Output is correct |
110 |
Correct |
952 ms |
51596 KB |
Output is correct |
111 |
Correct |
352 ms |
29760 KB |
Output is correct |
112 |
Correct |
1018 ms |
49620 KB |
Output is correct |