이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const ll INF = 1e18;
int N, M;
vector<pii> V[MAXN+10];
vector<ll> dp1[MAXN+10], dp2[MAXN+10];
struct BIT
{
ll tree[MAXN+10];
void init() { for(int i=1; i<=N; i++) tree[i]=-INF; }
void update(int i, ll k) { for(; i<=N; i+=(i&-i)) tree[i]=max(tree[i], k); }
ll query(int i) { ll ret=-INF; for(; i>0; i-=(i&-i)) ret=max(ret, tree[i]); return ret; }
}bit1, bit2;
ll max_weights(int _N, int _M, vector<int> _X, vector<int> _Y, vector<int> _W)
{
N=_N; M=_M;
for(int i=0; i<M; i++)
{
int x=_X[i]+1, y=_Y[i]+1, w=_W[i];
V[x].push_back({y, w});
}
for(int i=1; i<=N; i++)
{
sort(V[i].begin(), V[i].end());
if(V[i].empty() || V[i][0].first!=1) V[i].push_back({1, 0});
if(V[i].back().first!=N) V[i].push_back({N, 0});
sort(V[i].begin(), V[i].end());
dp1[i]=vector<ll>(V[i].size(), -INF);
dp2[i]=vector<ll>(V[i].size(), -INF);
}
bit1.init(); bit2.init();
ll ans=0;
dp1[1][0]=V[1][0].second;
for(int i=1; i<=N; i++)
{
int t=V[i].size();
if(i-1>=1) dp1[i][0]=max(dp1[i][0], dp2[i-1][0]+V[i][0].second);
if(i-2>=1) dp1[i][0]=max(dp1[i][0], dp1[i-2].back()+V[i][0].second);
if(i-2>=1) dp2[i][t-1]=max(dp2[i][t-1], dp1[i-2].back()+V[i][t-1].second);
if(i-3>=1) dp2[i][t-1]=max(dp2[i][t-1], dp1[i-3].back()+V[i][t-1].second);
for(int j=0; j<t; j++)
{
auto [y, w] = V[i][j];
dp1[i][j]=max(dp1[i][j], bit1.query(y-1)+w);
bit1.update(y, dp1[i][j]);
ans=max(ans, dp1[i][j]);
}
for(int j=t-1; j>=0; j--)
{
auto [y, w] = V[i][j];
dp2[i][j]=max(dp2[i][j], bit2.query(N-y)+w);
bit2.update(N-y+1, dp2[i][j]);
ans=max(ans, dp2[i][j]);
}
//for(auto it : dp1[i]) printf("%lld ", it); printf("\n");
//for(auto it : dp2[i]) printf("%lld ", it); printf("\n");
//printf("\n");
}
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... |