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;
#define ll long long
int n;
vector<vector<int>> v;
unordered_map<ll,ll> m, dp;
ll solve(int c, int p1, int p2){
if (c == n) return 0;
if (dp.find((ll)c*(ll)pow(10,12)+(ll)p1*(ll)pow(10,6)+(ll)p2) != dp.end()) return dp[(ll)c*(ll)pow(10,12)+(ll)p1*(ll)pow(10,6)+(ll)p2];
ll cur = 0, res = 0;
int l = 0, r = 0;
while (r < v[c].size()){
res = max(res, cur+solve(c+1, r, p1));
//if (c == 3 && p1 == 2 && p2 == 0) cout << r << " " << res << "\n";
while (c != 0 && l < p1 && v[c-1][l+1] <= v[c][r+1]){
l++;
cur -= m[(ll) c*100001 + (ll) v[c-1][l]];
}
r++;
if (c < n-1) cur += m[(ll) (c+1)*100001 + (ll) v[c][r]];
if (c > 0 && v[c-1][p1] < v[c][r] && (c == 1 || v[c][r] > v[c-2][p2])) cur += m[(ll) (c-1)*100001 + (ll) v[c][r]];
}
//cout << c << " " << p1 << " " << p2 << " " << res << "\n";
return dp[(ll)c*(ll)pow(10,12)+(ll)p1*(ll)pow(10,6)+(ll)p2] = res;
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W){
n = N;
v.resize(N, {-1});
for (int i=0; i<M; i++){
m[(ll)X[i]*100001+(ll)Y[i]] = W[i];
if (X[i] > 0) v[X[i]-1].push_back(Y[i]);
if (X[i] < N-1) v[X[i]+1].push_back(Y[i]);
}
for (int i=0; i<N; i++) sort(v[i].begin(), v[i].end());
return solve(0, 0, 0);
}
Compilation message (stderr)
fish.cpp: In function 'long long int solve(int, int, int)':
fish.cpp:16:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | while (r < v[c].size()){
| ~~^~~~~~~~~~~~~
# | 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... |