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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e18;
const ll weirdMod = 998244353;
int n, m, y, w;
struct fentree {
ll t[100005];
void init() {
for (int i = 1; i <= n; i++) t[i] = -inf;
}
void update(int i, ll k) {
for ( ; i <= n; i += (i & -i)) t[i] = max(t[i], k);
}
ll query(int i) {
ll ans = -inf;
for ( ; i; i -= (i & -i)) ans = max(ans, t[i]);
return ans;
}
} t1, t2;
vector<pll> pos[100005];
vector<ll> dp1[100005];
vector<ll> dp2[100005];
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++) {
X[i]++; Y[i]++;
pos[X[i]].pb({Y[i], W[i]});
}
for (int i = 0; i <= n + 1; i++) {
sort(pos[i].begin(), pos[i].end());
if (pos[i].empty() || pos[i][0].ff != 1) pos[i].pb({1, 0});
sort(pos[i].begin(), pos[i].end());
if (pos[i].back().ff != n) pos[i].pb({n, 0});
dp1[i] = vector<ll>(pos[i].size(), -inf);
dp2[i] = vector<ll>(pos[i].size(), -inf);
}
t1.init(); t2.init();
dp1[0][dp1[0].size() - 1] = 0; dp2[0][0] = 0;
ll a = -inf, b = -inf, ans = 0;
for (int i = 0; i <= n + 1; i++) {
int k = pos[i].size();
if (i >= 1) a = max(a, dp2[i - 1][0]);
if (i >= 2) b = max(b, dp1[i - 2].back());
dp1[i][0] = max(dp1[i][0], a + pos[i][0].sc);
if (i >= 2) dp1[i][0] = max(dp1[i][0], dp1[i - 2].back() + pos[i][0].sc);
dp2[i][k - 1] = max(dp2[i][k - 1], b + pos[i][k - 1].sc);
for (int j = 0; j < k; j++) {
y = pos[i][j].ff;
w = pos[i][j].sc;
dp1[i][j] = max(dp1[i][j], t1.query(y - 1) + w);
t1.update(y, dp1[i][j]);
}
for (int j = k - 1; j >= 0; j--) {
y = pos[i][j].ff;
w = pos[i][j].sc;
dp2[i][j] = max(dp2[i][j], t2.query(n - y) + w);
t2.update(n - y + 1, dp2[i][j]);
}
ans = max(ans, dp2[i][0]);
}
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... |