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;
#define ll long long
#define FOR(i, n) for (int i = 0; i < n; i++)
#define PB push_back
#define pii pair<int, int>
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
const int INF = 1e9 + 7;
const int maxn = 1e5 + 10;
int n, m;
vector<pii> fish[maxn]; // {y, w}
vector<ll> include[maxn], includent[maxn];
ll max_weights(int N, int M, vector<int> x,
vector<int> y, vector<int> w){
n = N, m = M;
FOR(i, m) fish[x[i]].PB({y[i], w[i]});
fish[0].PB({n, 0}); sort(ALL(fish[0]));
include[0].assign(fish[0].size(), 0);
includent[0].assign(fish[0].size(), 0);
for (int k = 1; k < n; k++){
fish[k].PB({n, 0}); sort(ALL(fish[k]));
// case 0: k + 1 pier is >= k pier
// k + 1 pier determines fish on k
// k - 1 pier length does not matter
ll best = 0;
for (auto v : include[k - 1]) best = max(best, v);
include[k].assign(fish[k].size(), best);
includent[k].assign(fish[k].size(), best);
// case 1: k pier is >= k - 1 pier
// k pier determines which fish can be caught on k - 1
ll j = 0, sum = 0, mx = -INF;
FOR(i, fish[k].size()){
// building a pier of length y[fish[k][i]] - 1
while (j < fish[k - 1].size() && fish[k - 1][j].F < fish[k][i].F){
mx = max(mx, includent[k - 1][j] - sum);
sum += fish[k - 1][j].S; j++;
}
include[k][i] = max(include[k][i], mx + sum);
includent[k][i] = max(includent[k][i], mx + sum);
}
// case 2: k pier is <= k - 1 pier
// k - 1 pier determines which fish can be caught on k
j = fish[k - 1].size(), sum = 0, mx = -INF;
for (int i = fish[k].size() - 1; i >= 0; i--){
// building a pier of length y[fish[k][i]] - 1
while (j > 0 && fish[k - 1][j - 1].F > fish[k][i].F){
j--;
mx = max(mx, include[k - 1][j] - sum);
}
sum += fish[k][i].S;
include[k][i] = max(include[k][i], mx + sum);
}
// cout << "we are on x = " << k << endl;
// for (auto v : include[k]) cout << v << ' '; cout << endl;
// for (auto v : includent[k]) cout << v << ' '; cout << endl;
// cout << endl;
}
ll re = 0;
for (auto v : include[n - 1]) re = max(re, v);
return re;
}
// signed main(){
// cout << max_weights(5, 4, {0, 1, 4, 3},
// {2, 1, 4, 3}, {5, 2, 1, 3});
// }
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:5:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define FOR(i, n) for (int i = 0; i < n; i++)
......
37 | FOR(i, fish[k].size()){
| ~~~~~~~~~~~~~~~~~
fish.cpp:37:9: note: in expansion of macro 'FOR'
37 | FOR(i, fish[k].size()){
| ^~~
fish.cpp:39:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | while (j < fish[k - 1].size() && fish[k - 1][j].F < fish[k][i].F){
| ~~^~~~~~~~~~~~~~~~~~~~
# | 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... |