#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 4e18;
vector<int> H[100100], A[100100], S[100100];
vector<ll> dp[2][100100];
int n, sz[100100];
ll cost(int i, int s, int e){
if (s>e) return 0;
int j1 = upper_bound(H[i].begin(), H[i].end(), s-1) - H[i].begin() - 1;
int j2 = upper_bound(H[i].begin(), H[i].end(), e) - H[i].begin() - 1;
return S[i][j2] - (j1<0?0:S[i][j1]);
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
n = N;
for (int i=0;i<=N;i++) {H[i].push_back(0); H[i].push_back(N);}
for (int i=0;i<M;i++){
++X[i], ++Y[i];
H[X[i]].push_back(Y[i]-1);
if (X[i]>1) H[X[i]-1].push_back(Y[i]);
if (X[i]<N) H[X[i]+1].push_back(Y[i]);
}
for (int i=0;i<=N;i++){
sort(H[i].begin(), H[i].end());
H[i].erase(unique(H[i].begin(), H[i].end()), H[i].end());
sz[i] = H[i].size();
A[i].resize(sz[i], 0);
S[i].resize(sz[i], 0);
dp[0][i].resize(sz[i], -INF);
dp[1][i].resize(sz[i], -INF);
}
for (int i=0;i<M;i++){
int idx = lower_bound(H[X[i]].begin(), H[X[i]].end(), Y[i]) - H[X[i]].begin();
assert(idx < sz[X[i]]);
A[X[i]][idx] = W[i];
}
for (int i=1;i<=n;i++){
for (int j=1;j<sz[i];j++) S[i][j] = S[i][j-1] + A[i][j];
}
dp[0][0][0] = 0;
dp[1][0][0] = 0;
for (int i=1;i<=n;i++){
for (int j=0;j<sz[i];j++){
///0 -> 0
for (int k=0;k<sz[i-1] && H[i-1][k]<=H[i][j];k++){
dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][k] + cost(i-1, H[i-1][k]+1, H[i][j]));
}
///1 -> 0
if (i>=2){
for (int k=0;k<sz[i-2];k++){
dp[0][i][j] = max(dp[0][i][j], dp[1][i-2][k] + cost(i-1, 0, max(H[i-2][k], H[i][j])));
}
}
///0 -> 1
dp[1][i][j] = max(dp[1][i][j], dp[0][i-1][sz[i-1]-1] + cost(i, H[i][j]+1, n));
///1 -> 1
for (int k=0;k<sz[i-1];k++) if (H[i-1][k] >= H[i][j]) dp[1][i][j] = max(dp[1][i][j], dp[1][i-1][k] + cost(i, H[i][j]+1, H[i-1][k]));
}
}
return max(*max_element(dp[0][n].begin(), dp[0][n].end()), *max_element(dp[1][n].begin(), dp[1][n].end()));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1074 ms |
32760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11988 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 |
47 ms |
28076 KB |
Output is correct |
2 |
Correct |
46 ms |
27992 KB |
Output is correct |
3 |
Incorrect |
115 ms |
28568 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '20897672610412' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11988 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 |
6 ms |
11988 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 |
6 ms |
11988 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 |
47 ms |
28076 KB |
Output is correct |
2 |
Correct |
46 ms |
27992 KB |
Output is correct |
3 |
Incorrect |
115 ms |
28568 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '20897672610412' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1074 ms |
32760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |