#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector <vector <pair <int, ll>>> fish;
vector <vector <ll>> pfx;
int n, m;
const int maxn = 1e5 + 5;
ll dp[maxn][5][5];
bool ready[maxn][5][5];
ll cost(int i, int h1, int h2, int h3) {
if (i == 1)
return 0;
if (max(h1, h3) <= h2 || (int)fish[i-1].size() == 1)
return 0;
int mx = max(h1, h3);
pair <int, ll> tp = {h2+1, 0};
int it = lower_bound(fish[i-1].begin(), fish[i-1].end(), tp) - fish[i-1].begin();
tp.first = mx;
tp.second = 1e18;
int it2 = upper_bound(fish[i-1].begin(), fish[i-1].end(), tp) - fish[i-1].begin();
it2--;
// if (it == it2)
// return 0;
if (it == 0)
return pfx[i-1][it2];
return pfx[i-1][it2] - pfx[i-1][it-1];
}
ll solve(int i, int h1, int h2) {
if (i == n+1) {
return cost(i, fish[i-2][h1].first, fish[i-1][h2].first, 0);
}
if (ready[i][h1][h2])
return dp[i][h1][h2];
ll ans = 0;
if (i-2 >= 0)
ans = solve(i+1, h2, 0) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, 0);
else
ans = solve(i+1, h2, 0) + cost(i, 0, fish[i-1][h2].first, 0);
int id = 0;
if (i-1 >= 1) {
if (!fish[i-1].empty())
ans = max(ans, solve(i+1, h2, 0) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, fish[i-1][0].first));
if ((int)fish[i-1].size() == 2)
ans = max(ans, solve(i+1, h2, 1) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, fish[i-1][1].first));
}
for (auto j : fish[i]) {
if (!fish[i].empty()) {
if (i-2 >= 0)
ans = max(ans, solve(i+1, h2, 0) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, fish[i][0].first));
else
ans = max(ans, solve(i+1, h2, 0) + cost(i, 0, fish[i-1][h2].first, fish[i][0].first));
}
if ((int)fish[i].size() == 2) {
if (i-2 >= 0)
ans = max(ans, solve(i+1, h2, 1) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, fish[i][1].first));
else
ans = max(ans, solve(i+1, h2, 1) + cost(i, 0, fish[i-1][h2].first, fish[i][1].first));
}
}
if (i+1 <= n) {
if (!fish[i+1].empty()) {
if (i-2 >= 0)
ans = max(ans, solve(i+1, h2, 0) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, fish[i+1][0].first));
else
ans = max(ans, solve(i+1, h2, 0) + cost(i, 0, fish[i-1][h2].first, fish[i+1][0].first));
}
if ((int)fish[i+1].size() == 2) {
if (i-2 >= 0)
ans = max(ans, solve(i+1, h2, 1) + cost(i, fish[i-2][h1].first, fish[i-1][h2].first, fish[i+1][1].first));
else
ans = max(ans, solve(i+1, h2, 1) + cost(i, 0, fish[i-1][h2].first, fish[i+1][1].first));
}
}
ready[i][h1][h2] = true;
dp[i][h1][h2] = ans;
return ans;
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N;
m = M;
fish.resize(N+1);
pfx.resize(N+1);
for (int i = 0; i < M; i++) {
fish[X[i] + 1].push_back({Y[i] + 1, W[i]});
}
// dp = vector <vector <ll>> (N+1, vector <ll> (N+1, -1));
for (int i = 0; i <= N; i++) {
fish[i].push_back({0, 0});
sort(fish[i].begin(), fish[i].end());
pfx[i].resize((int)fish[i].size() + 5, 1e18);
pfx[i][0] = 0;
for (int j = 1; j < (int)fish[i].size(); j++)
pfx[i][j] = pfx[i][j-1] + fish[i][j].second;
}
return solve(1, 0, 0);
}
// int main() {
// int nn, mm;
// cin >> nn >> mm;
// vector <int> x(mm), y(mm), w(mm);
// for (int i = 0; i < mm; i++)
// cin >> x[i] >> y[i] >> w[i];
// cout << max_weights(nn, mm, x, y, w) << '\n';
// }
Compilation message
fish.cpp: In function 'll solve(int, int, int)':
fish.cpp:48:15: warning: variable 'j' set but not used [-Wunused-but-set-variable]
48 | for (auto j : fish[i]) {
| ^
fish.cpp:41:9: warning: unused variable 'id' [-Wunused-variable]
41 | int id = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
49 ms |
51680 KB |
1st lines differ - on the 1st token, expected: '40313272768926', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
2396 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
52012 KB |
Output is correct |
2 |
Incorrect |
43 ms |
52048 KB |
1st lines differ - on the 1st token, expected: '882019', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
52012 KB |
Output is correct |
2 |
Incorrect |
43 ms |
52048 KB |
1st lines differ - on the 1st token, expected: '882019', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
49 ms |
51680 KB |
1st lines differ - on the 1st token, expected: '40313272768926', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |