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 all(x) begin(x),end(x)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const ll oo = 1e18;
void cmax(ll& a, ll b) {a=max(a,b);}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vvi ys(N+1);
ll total=0;
for(int i=0;i<M;++i) {
Y[i]++,X[i]++;
total+=W[i];
ys[X[i]].push_back(i);
}
vector<vector<ll>> pref(N+1,{0});
{ // preprocessing and coordinate compression.
int x=0;
for(auto& v : ys) {
sort(all(v),[&](int i, int j) {return Y[i]<Y[j];});
for(auto& id : v) {
pref[x].push_back(W[id]);
id = Y[id];
}
pref[x].push_back(0);
v.insert(v.begin(),0);
v.push_back(N+1);
partial_sum(all(pref[x]),pref[x].begin());
x++;
}
}
// dynamic programming
// DP[column][y-pos][state]
// state {0: rising, 1: falling}
// transition going up:
array<vector<ll>,2> dp = {vector<ll>{0LL,-oo},{-oo,-oo}};
ll top = -oo;
auto getS = [&](int x, int y) {
auto i = max(0LL,upper_bound(all(ys[x]),y)-ys[x].begin()-1LL);
return pref[x][i];
};
for(int x=1;x<=N;++x) {
int k = ys[x].size();
array<vector<ll>,2> ndp = {vector<ll>(k,-oo),vector<ll>(k,-oo)};
{ // falling to falling
int j = ys[x-1].size()-1;
ll best = -oo;
for(int i=k-1;i>=0;--i) {
while(j>=0 and ys[x-1][j]>=ys[x][i]) {
cmax(best,dp[1][j]);
--j;
}
cmax(ndp[1][i], best-getS(x-1,ys[x][i]) + pref[x][i]);
}
}
{ // rising to rising
int j = 0;
ll best = -oo;
for(int i=0;i<k;++i) {
while(j<int(ys[x-1].size()) and ys[x-1][j]<=ys[x][i]) {
cmax(best,dp[0][j]-getS(x,ys[x-1][j]));
++j;
}
cmax(ndp[0][i], best + pref[x][i]);
}
}
{ // falling to rising, far apart
ll best = *max_element(all(dp[1]));
for(int i=0;i<k;++i)
cmax(ndp[0][i],best+pref[x][i]);
}
// top to falling
for(int i=0;i<k;++i) {
cmax(ndp[1][i],pref[x][i]+top);
}
// rising to top
cmax(top,*max_element(all(dp[0])));
cmax(top,*max_element(all(dp[1])));
swap(dp,ndp);
}
// falling or top
auto ans = max(top,*max_element(all(dp[1])));;
return ans;
}
# | 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... |