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>
using namespace std;
const long long oo = 1LL << 60;
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w)
{
vector<vector<pair<int, int>>> fish(n);
for (int i = 0; i < m; i++)
fish[x[i]].push_back({y[i], w[i]});
vector<long long> bestPrefix(n, -oo);
// f: left, g: right
vector<long long> f, g, lastF, lastG;
for (int x = 0; x < n; x++)
{
sort(begin(fish[x]), end(fish[x]));
int sz = size(fish[x]);
swap(f, lastF);
swap(g, lastG);
f = g = vector<long long>(sz, -oo);
long long bestLastF = -oo, bestCurF = -oo;
for (int i = sz - 1, j = int(size(lastF)) - 1; i >= 0; i--)
if (x)
{
auto [y, w] = fish[x][i];
f[i] = max(bestCurF, 0LL) + w;
if (x >= 2)
f[i] = max(f[i], bestPrefix[x - 2] + w);
while (j >= 0 && fish[x - 1][j].first > y)
bestLastF = max(bestLastF, lastF[j--]);
f[i] = max(f[i], bestLastF + w);
bestCurF = max(bestCurF, f[i]);
}
if (!empty(lastF))
bestLastF = *max_element(begin(lastF), end(lastF));
long long bestLastG = -oo, bestCurG = -oo;
for (int i = 0, j = 0; i < sz; i++)
if (x < n - 1)
{
auto [y, w] = fish[x][i];
g[i] = max(bestCurG, 0LL) + w;
if (x >= 2)
g[i] = max(g[i], bestPrefix[x - 2] + w);
g[i] = max(g[i], bestLastF + w);
while (j < size(lastG) && fish[x - 1][j].first < y)
bestLastG = max(bestLastG, lastG[j++]);
g[i] = max(g[i], bestLastG + w);
bestCurG = max(bestCurG, g[i]);
}
if (x)
bestPrefix[x] = bestPrefix[x - 1];
bestPrefix[x] = max({bestPrefix[x], bestCurF, bestCurG});
}
return bestPrefix[n - 1];
}
Compilation message (stderr)
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | while (j < size(lastG) && fish[x - 1][j].first < y)
| ~~^~~~~~~~~~~~~
# | 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... |