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"
using namespace std;
#include <vector>
#include <algorithm>
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vector< vector<int> > Ys_per_col(N);
vector<long long> colsum(N);
for (int i = 0; i < M; i++) colsum[X[i]] += W[i];
auto find = [&](int x, int y){ return (upper_bound(Ys_per_col[x].begin(), Ys_per_col[x].end(), y) - Ys_per_col[x].begin()) - 1; };
for (int i = 0; i < M; i++) { X.push_back(X[i]); Y.push_back(Y[i]+1); W.push_back(0); }
for (int i = 0; i < N; i++) { X.push_back(i); Y.push_back(-1); W.push_back(0); }
for (int i = 0; i < N; i++) { X.push_back(i); Y.push_back(N); W.push_back(0); }
for (int i = 0; i < X.size(); i++) {
Ys_per_col[X[i]].push_back(Y[i]);
if (X[i] > 0) Ys_per_col[X[i]-1].push_back(Y[i]);
if (X[i]+1 < N) Ys_per_col[X[i]+1].push_back(Y[i]);
}
for (int i = 0; i < N; i++) {
sort(Ys_per_col[i].begin(), Ys_per_col[i].end());
Ys_per_col[i].erase( unique(Ys_per_col[i].begin(), Ys_per_col[i].end()), Ys_per_col[i].end());
}
vector< vector<int> > WW(N);
for (int i = 0; i < N; i++) WW[i].resize(Ys_per_col[i].size());
for (int i = 0; i < M; i++) {
int idx = find(X[i], Y[i]);
WW[X[i]][idx] = max(WW[X[i]][idx], W[i]);
}
vector< vector<long long> > dp[2];
for (int i = 0; i < 2; i++) {
dp[i].resize(N);
for (int j = 0; j < N; j++) dp[i][j].resize(Ys_per_col[j].size());
}
long long ans = 0;
for (int i = 1; i < N; i++) {
if (i >= 2) dp[0][i][0] = dp[1][i-2][0];
if (i >= 2) dp[0][i][ Ys_per_col[i].size() - 1 ] = dp[0][i-2][ Ys_per_col[i-2].size() - 1 ] + colsum[i-1];
for (int j = 1; j < (int)Ys_per_col[i].size(); j++) {
dp[0][i][j] = max(dp[0][i][j], dp[0][i][j-1] + WW[i-1][ find(i-1, Ys_per_col[i][j-1]) ]);
dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][ find(i-1, Ys_per_col[i][j]) ]);
}
dp[1][i][ Ys_per_col[i].size() - 1 ] = dp[0][i-1][ Ys_per_col[i-1].size() - 1 ];
for (int j = (int)Ys_per_col[i].size() - 2; j >= 0; j--) {
dp[1][i][j] = dp[1][i][j+1] + WW[i][j];
dp[1][i][j] = max( dp[1][i][j], dp[1][i-1][ find(i-1, Ys_per_col[i][j] - 1) + 1 ]);
}
for (int j = 0; j < (int)Ys_per_col[i].size(); j++) {
ans = max(ans, dp[0][i][j]);
ans = max(ans, dp[1][i][j]);
}
}
return ans;
}
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:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i = 0; i < X.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |