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;
long long max_weights(int N, int M,vector<int> X,vector<int> Y,
vector<int> W){
vector DP(N+1,vector(2,0ll));
vector<vector<pair<int,long long>>> pref(N+2);
for(int i=0;i<=N+1;i++)pref[i].emplace_back(0,0);
vector<vector<int>> special_points(N+2);
for(int i=1;i<=N+1;i++)special_points[i].emplace_back(0);
for(int i=1;i<=N+1;i++)special_points[i].emplace_back(N);
vector transition1(2,vector(N+1,0ll));
vector transition2(3,vector(N+1,0ll));
vector transition3(3,vector(N+1,0ll));
vector transition4(2,vector(N+1,0ll));
for(int i=0;i<M;i++) {
pref[X[i]+1].emplace_back(Y[i]+1,W[i]);
special_points[X[i]+1].emplace_back(Y[i]);
}
for(int i=1;i<=N+1;i++) {
sort(special_points[i].begin(),special_points[i].end());
special_points[i].erase(unique(special_points[i].begin(),special_points[i].end()),special_points[i].end());
sort(pref[i].begin(), pref[i].end());
for(int j=1;j<pref[i].size();j++)pref[i][j].second+=pref[i][j-1].second;
}
auto getpref = [&](int i,int j) {
auto iter = lower_bound(pref[i].begin(),pref[i].end(),make_pair(j+1,-1ll));
iter--;
return iter->second;
};
auto get_higher = [&](int i,int j) {
return lower_bound(special_points[i].begin(),special_points[i].end(),j)-special_points[i].begin()-0;
};
auto get_lower = [&](int i,int j) {
return upper_bound(special_points[i].begin(),special_points[i].end(),j)-special_points[i].begin()-1;
};
for(int i=2;i<=N;i++) {
int tot = special_points[i].size()-1;
for(int s=0;s<=tot;s++) {
int j = special_points[i][s];
if(i!=2)DP[s][1]=max(DP[s][1],-getpref(i,j)+transition1[0][get_higher(i-1,j)]);
else DP[s][1]=max(DP[s][1],getpref(i,N)-getpref(i,j));
if(i>2) {
if(i!=3) {
DP[s][0]=max(DP[s][0],getpref(i-1,j)+transition2[0][get_lower(i-2,j)]);
DP[s][0]=max(DP[s][0],transition3[0][get_higher(i-2,j)]);
}
else DP[s][0]=max(DP[s][0],getpref(i-1,N));
}
if(i!=2)DP[s][0]=max(DP[s][0],getpref(i-1,j)+transition4[0][get_lower(i-1,j)]);
else DP[s][0]=max(DP[s][0],getpref(i-1,j));
transition1[1][s]=getpref(i+1,j)+max(DP[s][0],DP[s][1]);
transition2[2][s]=max(DP[s][0],DP[s][1]);
transition3[2][s]=getpref(i+1,j)+max(DP[s][0],DP[s][1]);
transition4[1][s]=-getpref(i,j)+DP[s][0];
}
for(int j=tot-1;j>=0;j--)transition1[1][j]=max(transition1[1][j],transition1[1][j+1]);
for(int j=1;j<=tot;j++)transition2[2][j]=max(transition2[2][j],transition2[2][j-1]);
for(int j=tot-1;j>=0;j--)transition3[2][j]=max(transition3[2][j],transition3[2][j+1]);
for(int j=1;j<=tot;j++)transition4[1][j]=max(transition4[1][j],transition4[1][j-1]);
swap(transition1[0],transition1[1]);
swap(transition2[0],transition2[1]);
swap(transition2[1],transition2[2]);
swap(transition3[0],transition3[1]);
swap(transition3[1],transition3[2]);
swap(transition4[0],transition4[1]);
}
long long ans = 0;
for(int j=0;j<=N;j++)ans=max(ans,DP[j][0]);
for(int j=0;j<=N;j++)ans=max(ans,DP[j][1]);
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:25:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int j=1;j<pref[i].size();j++)pref[i][j].second+=pref[i][j-1].second;
| ~^~~~~~~~~~~~~~~
# | 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... |