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 <vector>
#include <algorithm>
#include <iostream>
using namespace std;
using ll = long long ;
#define sz(x) (int)(x).size()
#define xx first
#define yy second
//dp0[i][j] -> i. oszlopban j-ig elé építve piert max hogyha mindkettő szomszédosnál magasabb
//dp1[i][j] -> i. oszlopban j-ig elé építve piert max hogyha bal oldali számít
//dp2[i][j] -> i. oszlopban j-ig elé építve piert max hogyha jobb oldali számít
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<pair<int,ll>>> lst(N+2, vector<pair<int,ll>>());
for(int i=0;i<M;++i) {
lst[X[i]].push_back({Y[i], W[i]});
}
for(int i=0;i<=N+1;++i) lst[i].push_back({N+10,0});
for(auto& i:lst) sort(i.begin(), i.end());
for(auto& i:lst)
for(int j=1;j<sz(i);++j) i[j].yy+=i[j-1].yy;
auto get_sum=[&](int x, int l, int r) -> ll {
if(l>r) return 0;
//~ ll sum=0;
//~ for(int i=0;i<sz(lst[x]);++i) {
//~ if(l<=lst[x][i].xx && lst[x][i].xx<=r) sum+=lst[x][i].yy;
//~ }
//~ cerr<<x<<","<<l<<","<<r<<" -> "<<sum<<"\n";
auto itL=lower_bound(lst[x].begin(), lst[x].end(), make_pair(l, 0LL));
auto itR=lower_bound(lst[x].begin(), lst[x].end(), make_pair(r+1, 0LL));
ll sum=(itR==lst[x].begin()?0:prev(itR)->yy)-(itL==lst[x].begin()?0:prev(itL)->yy);
return sum;
};
vector<ll> dp0(lst[0].size());
vector<ll> dp1(lst[0].size());
vector<ll> dp2(lst[0].size());
for(int i=1;i<N;++i) {
vector<ll> ndp0(lst[i].size());
vector<ll> ndp1(lst[i].size());
vector<ll> ndp2(lst[i].size());
//dp0
for(int j=0;j<sz(lst[i]);++j) {
for(int k=0;k<sz(lst[i-1]);++k) {
ndp0[j]=max({ndp0[j], dp1[k], dp2[k]+get_sum(i-1, lst[i-1][k].xx, lst[i][j].xx-1)});
}
}
//dp1
for(int j=0;j<sz(lst[i]);++j) {
for(int k=0;k<sz(lst[i-1]);++k) {
ndp1[j]=max({ndp1[j], max(dp0[k],dp1[k])+get_sum(i, lst[i][j].xx, lst[i-1][k].xx-1)});
}
}
//dp2
for(int j=0;j<sz(lst[i]);++j) {
for(int k=0;k<sz(lst[i-1]);++k) {
ndp2[j]=max({ndp2[j], dp0[k], dp1[k], dp2[k]+get_sum(i-1, lst[i-1][k].xx, lst[i][j].xx-1)});
}
}
//~ cerr<<i<<", 0: "; for(auto j:ndp0) cerr<<j<<" ";cerr<<"\n";
//~ cerr<<i<<", 1: "; for(auto j:ndp1) cerr<<j<<" ";cerr<<"\n";
//~ cerr<<i<<", 2: "; for(auto j:ndp2) cerr<<j<<" ";cerr<<"\n\n";
dp0.swap(ndp0);
dp1.swap(ndp1);
dp2.swap(ndp2);
}
return max({
*max_element(dp0.begin(), dp0.end()),
*max_element(dp1.begin(), dp1.end()),
*max_element(dp2.begin(), dp2.end())
});
}
# | 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... |