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<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define left BAO
#define right ANH
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 2e5 + 10;
struct Data {
int t, y, w;
};
vector<Data> fish[N];
vector<ll> dp[N], pref[N], pref2[N], suff[N];
ll f[N];
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
for(int i = 0; i < m; i++) {
if(x[i] > 0) fish[x[i] - 1].pb({1, y[i], w[i]});
fish[x[i]].pb({0, y[i], w[i]});
fish[x[i] + 1].pb({-1, y[i], w[i]});
}
for(int i = 0; i < n; i++) {
sort(all(fish[i]), [&](Data x, Data y) {
if(x.y == y.y) return (x.t == 0);
return x.y < y.y;
});
}
ll ans = 0;
for(int i = 0; i < n; i++) {
ll left = 0;
ll right = 0;
ll hide = 0;
dp[i].resize(fish[i].size() + 2);
for(int j = 0; j < fish[i].size(); j++) {
if(fish[i][j].t == -1) left += fish[i][j].w;
if(fish[i][j].t == 0) hide += fish[i][j].w;
if(fish[i][j].t == 1) right += fish[i][j].w;
dp[i][j] = left + right;
if(i >= 3) maximize(dp[i][j], left + right + f[i - 3]);
if(i >= 2) {
int l = 0, r = fish[i - 2].size() - 1, pos = -1;
while(l <= r) {
int mid = (l + r) >> 1;
if(fish[i - 2][mid].y >= fish[i][j].y) {
pos = mid;
r = mid - 1;
} else l = mid + 1;
}
if(pos >= 0) maximize(dp[i][j], right + suff[i - 2][pos]);
if(pos == -1) pos = fish[i - 2].size();
pos--;
if(pos >= 0) maximize(dp[i][j], right + left + pref[i - 2][pos]);
}
if(i >= 1 && (i % 2 == 0)) {
int l = 0, r = fish[i - 1].size() - 1, pos = -1;
while(l <= r) {
int mid = (l + r) >> 1;
if(fish[i - 1][mid].y >= fish[i][j].y) {
pos = mid;
r = mid - 1;
} else l = mid + 1;
}
if(pos >= 0) maximize(dp[i][j], right - hide + suff[i - 1][pos]);
if(pos == -1) pos = fish[i - 1].size();
pos--;
assert(!left && !right);
if(pos >= 0) maximize(dp[i][j], right + left + pref2[i - 1][pos]);
}
}
suff[i].resize(fish[i].size() + 2);
pref[i] = pref2[i] = suff[i];
for(int j = fish[i].size(); j >= 0; j--) {
suff[i][j] = max(suff[i][j + 1], dp[i][j]);
}
left = right = hide = 0;
for(int j = 0; j < fish[i].size(); j++) {
if(fish[i][j].t == -1) left += fish[i][j].w;
if(fish[i][j].t == 0) hide += fish[i][j].w;
if(fish[i][j].t == 1) right += fish[i][j].w;
pref[i][j] = dp[i][j] - right;
pref2[i][j] = dp[i][j] - right - hide;
if(j > 0) {
maximize(pref[i][j], pref[i][j - 1]);
maximize(pref2[i][j], pref2[i][j - 1]);
}
}
for(int j = 0; j < fish[i].size(); j++) maximize(f[i], dp[i][j]);
if(i > 0) maximize(f[i], f[i - 1]);
maximize(ans, f[i]);
}
return ans;
}
Compilation message (stderr)
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:100:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int j = 0; j < fish[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
fish.cpp:145:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for(int j = 0; j < fish[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
fish.cpp:157:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | for(int j = 0; j < fish[i].size(); j++) maximize(f[i], dp[i][j]);
| ~~^~~~~~~~~~~~~~~~
# | 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... |