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 "fish.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = -(1LL << 60);
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector<vector<int>> b(n);
for (int i = 0; i < m; i++) {
b[x[i]].push_back(i);
}
vector d(n, vector<int>{-1});
vector<vector<int>> r(n), p(n);
for (int i = 0; i < n; i++) {
sort(b[i].begin(), b[i].end(), [&](int j, int k) {
return y[j] < y[k];
});
for (int j : b[i]) {
r[i].push_back(y[j]);
p[i].push_back(w[j]);
if (i > 0) {
d[i - 1].push_back(y[j]);
}
if (i < n - 1) {
d[i + 1].push_back(y[j]);
}
}
}
vector<vector<long long>> s(n);
for (int i = 0; i < n; i++) {
int k = p[i].size();
s[i] = vector<long long>(k + 1);
for (int j = k - 1; j >= 0; j--) {
s[i][j] = s[i][j + 1] + p[i][j];
}
}
auto get_sum = [&](int i, int x) -> long long {
if (i == -1) {
return 0LL;
}
return s[i][upper_bound(r[i].begin(), r[i].end(), x) - r[i].begin()];
};
vector<int> e{-1};
vector<array<long long, 2>> dp{{0, 0}};
for (int i = 0; i < n; i++) {
int k = dp.size();
vector<long long> pmax(k + 1, inf);
for (int j = 0; j < k; j++) {
pmax[j + 1] = max(pmax[j], dp[j][0] + get_sum(i - 1, e[j]));
}
vector<long long> smax(k + 1, inf);
for (int j = k - 1; j >= 0; j--) {
smax[j] = max(smax[j + 1], dp[j][1] - get_sum(i, e[j]));
}
for (int j = 0; j < k; j++) {
dp[0][0] = max(dp[0][0], dp[j][0]);
dp[0][1] = max(dp[0][1], max(dp[j][0], dp[j][1]));
}
int l = d[i].size();
dp.resize(l);
for (int j = 1; j < l; j++) {
dp[j][0] = pmax[upper_bound(e.begin(), e.end(), d[i][j]) - e.begin()] - get_sum(i - 1, d[i][j]);
dp[j][1] = smax[lower_bound(e.begin(), e.end(), d[i][j]) - e.begin()] + get_sum(i, d[i][j]);
}
swap(e, d[i]);
}
long long ans = 0;
for (auto v : dp) {
ans = max(ans, max(v[0], v[1]));
}
return ans;
}
# | 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... |