#include "fish.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <climits>
#include <map>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
int n, m;
vector<pll> pts[100005];
vector<int> dppoints[100005];
long long getsum(int col, int row) {
if(col == -1 || col == n) return 0;
auto it = upper_bound(pts[col].begin(), pts[col].end(), pll(row, LLONG_MAX));
if(it == pts[col].begin()) return 0;
return (--it)->second;
}
ll f1[100005], f2[100005], f3[100005], f4[100005];
map<int, ll> DP[100005];
vector<int> used;
void upd(ll*fwk, int idx, ll val) {
for(;idx<=100000;idx+=idx&-idx) {
fwk[idx] = max(fwk[idx], val);
used.push_back(idx);
}
}
ll qr(ll* fwk, int idx) {
ll cmax = 0;
for(;idx;idx-=idx&-idx)
cmax = max(cmax, fwk[idx]);
return cmax;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
n = N; m = M;
for(int i=0;i<X.size();i++) {
pts[X[i]].emplace_back(Y[i], i);
if(X[i] != 0) dppoints[X[i]-1].push_back(Y[i]);
if(X[i] != N-1)dppoints[X[i]+1].push_back(Y[i]);
}
for(int i=0;i<N;i++) {
if(pts[i].empty()) continue;
sort(pts[i].begin(), pts[i].end());
pts[i][0].second = W[pts[i][0].second];
for(int j=1;j<pts[i].size();j++) {
pts[i][j].second = pts[i][j-1].second + W[pts[i][j].second];
}
}
long long best = 0, cur_im1y = 0;
for(int col=0;col<N;col++) {
sort(dppoints[col].begin(), dppoints[col].end());
dppoints[col].resize(unique(dppoints[col].begin(), dppoints[col].end())-dppoints[col].begin());
for(int row:dppoints[col]) {
ll Wij = getsum(col, row), Wipj=getsum(col+1,row), Wimj=getsum(col-1,row);
ll &cdp = DP[col][row];
cdp = cur_im1y + Wipj + Wimj;
if(col != 0) {
//max{k <= j}(DP[i-1][k]) - sum{m<=k}(w[i][m])
//max{k > j}(DP[i-1][k]) - sum{m<=j}(w[i][m])
long long cbest = max(qr(f1, row+1), qr(f2, n-row) - Wij);
//cout << qr(f1, row+1) << ' ' << qr(f2, n-row) << '\n';
cdp = max(cbest+Wipj, cdp);
}
if(col>1) {
//max{k <= j}(DP[i-2][k]) - sum{m<=k}(w[i-1][m])
//max{k > j}(DP[i-2][k]) - sum{m<=j}(w[i-1][m])
long long cbest = max(qr(f3, row+1) + Wimj, qr(f4, n-row)-Wimj);
cdp = max(cbest + Wipj, cdp);
}
best = max(best, cdp);
}
if(col!=0) {
for(int e:used)
f1[e] = f2[e] = f3[e] = f4[e] = 0;
used.clear();
for(int row:dppoints[col-1]) {
upd(f3, row+1, DP[col-1][row] - getsum(col, row));
upd(f4, n-row, DP[col-1][row]);
}
}
if(col >= 2)
for(int row:dppoints[col-2])
cur_im1y = max(cur_im1y, DP[col-2][row]);
//cout << "-----\n" << col << '\n';
for(int row:dppoints[col]) {
//cout << row << ": " << DP[col][row] << ' ';
//cout << '\n';
upd(f1, row+1, DP[col][row] - getsum(col+1, row));
upd(f2, n-row, DP[col][row]);
} //buildfwk
}
return best;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:43:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i=0;i<X.size();i++) {
| ~^~~~~~~~~
fish.cpp:52:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int j=1;j<pts[i].size();j++) {
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
27664 KB |
Output is correct |
2 |
Correct |
133 ms |
30312 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
714 ms |
71208 KB |
Output is correct |
6 |
Correct |
481 ms |
69044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9812 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 |
4 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
54 ms |
18276 KB |
Output is correct |
4 |
Correct |
31 ms |
16152 KB |
Output is correct |
5 |
Correct |
82 ms |
26260 KB |
Output is correct |
6 |
Correct |
85 ms |
25588 KB |
Output is correct |
7 |
Correct |
102 ms |
26244 KB |
Output is correct |
8 |
Correct |
79 ms |
26176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9812 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 |
4 ms |
9812 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 |
4 ms |
9812 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 |
4 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
54 ms |
18276 KB |
Output is correct |
4 |
Correct |
31 ms |
16152 KB |
Output is correct |
5 |
Correct |
82 ms |
26260 KB |
Output is correct |
6 |
Correct |
85 ms |
25588 KB |
Output is correct |
7 |
Correct |
102 ms |
26244 KB |
Output is correct |
8 |
Correct |
79 ms |
26176 KB |
Output is correct |
9 |
Incorrect |
125 ms |
35268 KB |
1st lines differ - on the 1st token, expected: '99999', found: '66666' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
27664 KB |
Output is correct |
2 |
Correct |
133 ms |
30312 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
714 ms |
71208 KB |
Output is correct |
6 |
Correct |
481 ms |
69044 KB |
Output is correct |
7 |
Incorrect |
4 ms |
9812 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |