#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
template <class U, class V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <class U, class V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
long long max_weights(int numCol, int numCatfish, vector <int> rows, vector <int> cols, vector <int> weights) {
vector <vector <pair <int, long long>>> atCol(numCol);
REP(i, numCatfish) {
atCol[cols[i]].push_back(make_pair(rows[i], weights[i]));
}
REP(i, numCol) {
sort(ALL(atCol[i]));
long long pre = 0;
for (auto &[_, x] : atCol[i]) {
x += pre;
pre = x;
}
}
auto prefix_sum = [&] (int i, int cur) -> long long {
auto it = lower_bound(ALL(atCol[i]), make_pair(cur, -1ll));
if(it != atCol[i].begin()) return prev(it) -> second;
return 0;
};
vector <vector <vector <long long>>> dp(numCol, vector <vector <long long>>(2));
REP(i, numCol) {
dp[i][0].assign((int) atCol[i].size(), 0);
dp[i][1].assign((int) atCol[i].size(), 0);
long long Max = 0;
if(i == 0) continue;
for (int cur = (int) atCol[i].size() - 1, pre = (int) atCol[i - 1].size(); cur >= 0; cur--) {
while(pre > 0 && atCol[i - 1][pre - 1].first >= atCol[i][cur].first) {
pre--;
maximize(Max, dp[i - 1][0][pre] + prefix_sum(i, atCol[i - 1][pre].first));
}
maximize(dp[i][0][cur], Max - prefix_sum(i, atCol[i][cur].first));
}
// Max = (i >= 2 ? max(*max_element(ALL(dp[i - 2][0])), *max_element(ALL(dp[i - 2][1]))) : 0);
// if(i > 1) Max = max(*max_element(ALL(dp[i - 2][0])), *max_element(ALL(dp[i - 2][1])));
// else Max = 0;
Max = 0;
for (int cur = 0, pre = -1; cur < (int) atCol[i].size(); ++cur) {
while(pre + 1 < (int) atCol[i - 1].size() && atCol[i - 1][pre + 1].first <= atCol[i][cur].first) {
pre++;
maximize(Max, dp[i - 1][1][pre] - prefix_sum(i - 1, atCol[i - 1][pre].first));
}
// cout << cur << '\n';
maximize(dp[i][1][cur], Max + prefix_sum(i - 1, atCol[i][cur].first));
}
}
return max(*max_element(ALL(dp[numCol - 1][0])), *max_element(ALL(dp[numCol - 1][1])));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
20936 KB |
1st lines differ - on the 1st token, expected: '40313272768926', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
22 ms |
22612 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
22 ms |
22612 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
20936 KB |
1st lines differ - on the 1st token, expected: '40313272768926', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |