This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
using ll = long long;
ll max_weights(
int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector arr(n+1, vector<pair<int,ll>>());
for (int i = 0; i < m; i++) {
arr[x[i]+1].emplace_back(y[i], w[i]);
}
for (int i = 0; i < n; i++)
sort(arr[i].begin(), arr[i].end());
auto pref = arr;
for (int i = 0; i <= n; i++) {
for (int j = 1; j < pref[i].size(); j++) {
pref[i][j].second += pref[i][j-1].second;
}
}
auto sum_right = [&](int c, int right)->ll{
int l = -1, r = pref[c].size();
while (l < r-1) {
int mid = (l+r) / 2;
if (pref[c][mid].first <= right) l = mid;
else r= mid;
}
return l==-1 ? 0 : pref[c][l].second;
};
auto sum = [&](int c, int l, int r) { // [l, r]
if (l <= r) return sum_right(c, r) - sum_right(c, l-1);
else return 0ll;
};
vector<vector<int>> byx(n+2), used(n+1);
for (int i = 0; i < m; i++)
byx[x[i]+1].push_back(y[i]);
for (int i = 0; i <= n; i++) {
sort(byx[i].begin(), byx[i].end());
byx[i].erase(unique(byx[i].begin(), byx[i].end()), byx[i].end());
}
used[0] = {0};
for (int i = 1; i <= n; i++) {
merge(byx[i-1].begin(), byx[i-1].end(),
byx[i+1].begin(), byx[i+1].end(),
back_inserter(used[i]));
for (int& j : used[i])
j++;
used[i].insert(used[i].begin(), 0);
}
vector memo(2, vector(n+1, vector(n+1, -1ll)));
auto dp = [&](auto& dp, bool inc, int c, int h2)->ll{
if (c == n+1) return 0;
int h2_key = lower_bound(used[c-1].begin(), used[c-1].end(), h2) - used[c-1].begin();
ll& res = memo[inc][c][h2_key];
if (res!=-1) return res;
res = 0;
if (inc) {
for (int h3 : used[c]) {
if (h3 < h2) continue;
ll val = sum(c-1, h2, h3-1);
res = max(res, val + dp(dp, true, c+1, h3));
res = max(res, val + dp(dp, false, c+1, h3));
}
} else {
for (int h3 : used[c]) {
if (h3 > h2) break;
ll val = sum(c, h3, h2-1);
res = max(res, val + dp(dp, false, c+1, h3));
}
}
res = max(res, dp(dp, true, c+1, 0));
return res;
};
ll res = dp(dp, true, 1, 0);
return res;
}
Compilation message (stderr)
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:16:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int j = 1; j < pref[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |