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 std::vector;
using std::string;
using std::cin;
using std::cout;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define ln "\n"
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define all(a) a.begin(), a.end()
#define mp std::make_pair
#define pb emplace_back
constexpr ll inf = (1ll<<60);
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
// task 6
rep(i,0,M) X[i]++, Y[i]++;
// data1 : その行
// data2 : 隣接
vector<vector<pii>> data1(N+2), data2(N+2);
rep(i,0,M){
data1[X[i]].pb(mp(Y[i],W[i]));
if(X[i]-1) data2[X[i]-1].pb(mp(Y[i],W[i]));
if(X[i]+1 <= N) data2[X[i]+1].pb(mp(Y[i],W[i]));
}
vii sum(N+2);
rep(i,0,N+2){
data1[i].pb(mp(0,0));
data2[i].pb(mp(0,0));
sort(all(data1[i]));
sort(all(data2[i]));
sum[i].resize(data1[i].size());
rep(j,1,data1[i].size()) sum[i][j] = sum[i][j-1]+data1[i][j].second;
}
auto id = [&](ll x, ll height){
ll idx = std::upper_bound(all(data1[x]), mp(height, inf))-data1[x].begin();
idx--;
return idx;
};
auto calc = [&](ll x, ll height){
ll idx = std::upper_bound(all(data1[x]), mp(height, inf))-data1[x].begin();
idx--;
return sum[x][idx];
};
vii up(N+2), down(N+2);
up[0].resize(1);
down[0].resize(1);
up[0][0] = down[0][0] = 0;
rep(i,1,N+2){
up[i].resize(data2[i].size(),-inf);
down[i].resize(data2[i].size(),-inf);
{
vi max(data2[i-1].size());
rep(j,0,data2[i-1].size()){
max[j] = up[i-1][j]-calc(i-1, data2[i-1][j].first);
if(j) max[j] = std::max(max[j], max[j-1]);
}
rep(j,0,data2[i].size()){
ll idx = upper_bound(all(data2[i-1]), data2[i][j])-data2[i-1].begin();
if(idx) up[i][j] = std::max(up[i][j], max[idx-1]+calc(i-1, data2[i][j].first));
}
}
{
vi max(data2[i-1].size());
per(j,data2[i-1].size()-1,0){
max[j] = down[i-1][j]+calc(i, data2[i-1][j].first);
if(j+1 < data2[i-1].size()) max[j] = std::max(max[j], max[j+1]);
}
rep(j,0,data2[i].size()){
ll idx = lower_bound(all(data2[i-1]), data2[i][j])-data2[i-1].begin();
if(idx < data2[i-1].size()) down[i][j] = std::max(down[i][j], max[idx]-calc(i, data2[i][j].first));
}
}
if(i-2 >= 0){
vi max(data2[i-2].size());
per(j,data2[i-2].size()-1,0){
max[j] = down[i-2][j]+calc(i-1, data2[i-2][j].first);
if(j+1 < data2[i-2].size()) max[j] = std::max(max[j], max[j+1]);
}
rep(j,0,data2[i].size()){
ll idx = lower_bound(all(data2[i-2]), data2[i][j])-data2[i-2].begin();
if(idx < data2[i-2].size()) up[i][j] = std::max(up[i][j], max[idx]);
}
}
if(i-2 >= 0){
vi max(data2[i-2].size());
rep(j,0,data2[i-2].size()){
max[j] = down[i-2][j];
if(j) max[j] = std::max(max[j], max[j-1]);
}
rep(j,0,data2[i].size()){
ll idx = upper_bound(all(data2[i-2]), data2[i][j])-data2[i-2].begin();
if(idx) up[i][j] = std::max(up[i][j], max[idx-1]+calc(i-1, data2[i][j].first));
}
}
if(i-3 >= 0){
ll max = 0;
rep(k,0,data2[i-3].size()){
max = std::max(max, down[i-3][k]+calc(i-2, data2[i-3][k].first));
}
rep(j,0,data2[i].size()){
up[i][j] = std::max(up[i][j], max+calc(i-1,data2[i][j].first));
}
}
rep(j,0,data2[i].size()) down[i][j] = std::max(down[i][j], up[i][j]);
}
ll ans = std::max(up[N+1][0], down[N+1][0]);
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:74:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if(j+1 < data2[i-1].size()) max[j] = std::max(max[j], max[j+1]);
| ~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:78:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if(idx < data2[i-1].size()) down[i][j] = std::max(down[i][j], max[idx]-calc(i, data2[i][j].first));
| ~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:85:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | if(j+1 < data2[i-2].size()) max[j] = std::max(max[j], max[j+1]);
| ~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:89:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if(idx < data2[i-2].size()) up[i][j] = std::max(up[i][j], max[idx]);
| ~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:42:8: warning: variable 'id' set but not used [-Wunused-but-set-variable]
42 | auto id = [&](ll x, ll height){
| ^~
# | 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... |