#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <array>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <random>
#include <cstdlib>
#define debug(x) std::cout << #x << " " << (x) << '\n';
#define pb push_back
#define mp std::make_pair
#define remax(a, b) a = std::max((a), (b));
#define remin(a, b) a = std::min((a), (b));
#define maxWeights max_weights
const int32_t MAX_N = 1e5;
struct Catfish {
int32_t row;
int32_t id;
mutable int64_t prefSum;
Catfish(int32_t _row, int32_t _id, int32_t w): row(_row), id(_id), prefSum((int64_t) w) {}
bool operator< (const Catfish &other) const {
if(row != other.row) {
return row <= other.row;
}
else {
return id <= other.id;
}
}
};
struct DpState {
int32_t row;
mutable int64_t incrVal, decrVal;
DpState(int32_t _row): row(_row), incrVal(0), decrVal(0) {}
bool operator< (const DpState &other) const {
return row <= other.row;
}
};
int64_t maxWeights(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
std::vector<int32_t> w) {
std::vector<std::set<Catfish>> catfishByColumn(n);
for(int32_t i = 0; i < m; i++) {
catfishByColumn[x[i]].insert(Catfish(y[i], i, w[i]));
}
for(int32_t i = 0; i < n; i++) {
int64_t total = 0;
for(auto &x : catfishByColumn[i]) {
total += (int64_t) x.prefSum;
x.prefSum = total;
}
}
std::vector<std::set<DpState>> dp(n + 1);
for(int32_t i = 0; i <= n; i++) {
if(i > 0 && i != n) {
for(auto &x : catfishByColumn[i - 1]) {
dp[i].insert(DpState(x.row + 1));
}
}
if(i < n - 1) {
for(auto &x : catfishByColumn[i + 1]) {
dp[i].insert(DpState(x.row + 1));
}
}
dp[i].insert(DpState(0));
}
auto getWPrefSum = [&](int32_t column, int32_t row) -> int64_t {
if(column >= n) {
return 0;
}
auto p = catfishByColumn[column].lower_bound(Catfish(row, -1, 0));
if(p == catfishByColumn[column].begin()) {
return 0;
}
p--;
return p->prefSum;
/**
for(auto it = catfishByColumn[column].rbegin(); it != catfishByColumn[column].rend(); it++) {
if(it->row < row) {
return it->prefSum;
}
}
return 0;
*/
};
for(int32_t i = 1; i <= n; i++) {
{ // compute incrVal
auto prevIt = dp[i - 1].begin();
int64_t pref = 0;
for(auto it = dp[i].begin(); it != dp[i].end(); it++) {
while(prevIt != dp[i - 1].end() && prevIt->row <= it->row) {
remax(pref, prevIt->incrVal - getWPrefSum(i - 1, prevIt->row));
prevIt++;
}
it->incrVal = std::max(pref + getWPrefSum(i - 1, it->row), dp[i - 1].begin()->decrVal);
}
}
{ // compute decrVal
auto prevIt = dp[i - 1].rbegin();
int64_t suff = 0;
for(auto it = dp[i].rbegin(); it != dp[i].rend(); it++) {
while(prevIt != dp[i - 1].rend() && prevIt->row >= it->row) {
remax(suff, std::max(prevIt->incrVal, prevIt->decrVal) + getWPrefSum(i, prevIt->row));
prevIt++;
}
it->decrVal = suff - getWPrefSum(i, it->row);
}
}
}
/**
for(int32_t i = 0; i <= n; i++) {
std::cout << i << ": ";
for(auto &x : dp[i]) {
std::cout << "{ " << x.row << ", " << x.incrVal << ", " << x.decrVal << " }, ";
}
std::cout << '\n';
}
*/
return std::max(dp.back().begin()->incrVal, dp.back().begin()->decrVal);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
26316 KB |
Output is correct |
2 |
Correct |
150 ms |
31144 KB |
Output is correct |
3 |
Correct |
15 ms |
15956 KB |
Output is correct |
4 |
Correct |
14 ms |
15892 KB |
Output is correct |
5 |
Correct |
683 ms |
79860 KB |
Output is correct |
6 |
Correct |
455 ms |
79736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
300 ms |
43624 KB |
Output is correct |
3 |
Correct |
401 ms |
52360 KB |
Output is correct |
4 |
Correct |
105 ms |
26828 KB |
Output is correct |
5 |
Correct |
138 ms |
31140 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
19 ms |
15956 KB |
Output is correct |
11 |
Correct |
15 ms |
15956 KB |
Output is correct |
12 |
Correct |
154 ms |
32032 KB |
Output is correct |
13 |
Correct |
182 ms |
37356 KB |
Output is correct |
14 |
Correct |
157 ms |
29448 KB |
Output is correct |
15 |
Correct |
157 ms |
32608 KB |
Output is correct |
16 |
Correct |
126 ms |
29272 KB |
Output is correct |
17 |
Correct |
159 ms |
32460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
15956 KB |
Output is correct |
2 |
Correct |
16 ms |
15956 KB |
Output is correct |
3 |
Correct |
57 ms |
25580 KB |
Output is correct |
4 |
Correct |
42 ms |
23500 KB |
Output is correct |
5 |
Correct |
101 ms |
37052 KB |
Output is correct |
6 |
Correct |
99 ms |
37064 KB |
Output is correct |
7 |
Correct |
103 ms |
37076 KB |
Output is correct |
8 |
Correct |
101 ms |
36964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
412 KB |
Output is correct |
16 |
Correct |
4 ms |
764 KB |
Output is correct |
17 |
Correct |
78 ms |
9980 KB |
Output is correct |
18 |
Correct |
65 ms |
9824 KB |
Output is correct |
19 |
Correct |
58 ms |
9824 KB |
Output is correct |
20 |
Correct |
60 ms |
9836 KB |
Output is correct |
21 |
Correct |
72 ms |
9824 KB |
Output is correct |
22 |
Correct |
134 ms |
19272 KB |
Output is correct |
23 |
Correct |
12 ms |
2240 KB |
Output is correct |
24 |
Correct |
42 ms |
6532 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
11 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
412 KB |
Output is correct |
16 |
Correct |
4 ms |
764 KB |
Output is correct |
17 |
Correct |
78 ms |
9980 KB |
Output is correct |
18 |
Correct |
65 ms |
9824 KB |
Output is correct |
19 |
Correct |
58 ms |
9824 KB |
Output is correct |
20 |
Correct |
60 ms |
9836 KB |
Output is correct |
21 |
Correct |
72 ms |
9824 KB |
Output is correct |
22 |
Correct |
134 ms |
19272 KB |
Output is correct |
23 |
Correct |
12 ms |
2240 KB |
Output is correct |
24 |
Correct |
42 ms |
6532 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
11 ms |
2004 KB |
Output is correct |
27 |
Correct |
3 ms |
1364 KB |
Output is correct |
28 |
Correct |
429 ms |
46000 KB |
Output is correct |
29 |
Correct |
660 ms |
67244 KB |
Output is correct |
30 |
Correct |
619 ms |
67232 KB |
Output is correct |
31 |
Correct |
615 ms |
67448 KB |
Output is correct |
32 |
Correct |
617 ms |
66072 KB |
Output is correct |
33 |
Correct |
605 ms |
67236 KB |
Output is correct |
34 |
Correct |
620 ms |
66668 KB |
Output is correct |
35 |
Correct |
192 ms |
26284 KB |
Output is correct |
36 |
Correct |
651 ms |
66300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
15956 KB |
Output is correct |
2 |
Correct |
16 ms |
15956 KB |
Output is correct |
3 |
Correct |
57 ms |
25580 KB |
Output is correct |
4 |
Correct |
42 ms |
23500 KB |
Output is correct |
5 |
Correct |
101 ms |
37052 KB |
Output is correct |
6 |
Correct |
99 ms |
37064 KB |
Output is correct |
7 |
Correct |
103 ms |
37076 KB |
Output is correct |
8 |
Correct |
101 ms |
36964 KB |
Output is correct |
9 |
Correct |
102 ms |
37092 KB |
Output is correct |
10 |
Correct |
121 ms |
29260 KB |
Output is correct |
11 |
Correct |
241 ms |
58204 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
15 ms |
15956 KB |
Output is correct |
19 |
Correct |
14 ms |
15956 KB |
Output is correct |
20 |
Correct |
15 ms |
15956 KB |
Output is correct |
21 |
Correct |
15 ms |
15828 KB |
Output is correct |
22 |
Correct |
129 ms |
37020 KB |
Output is correct |
23 |
Correct |
246 ms |
58212 KB |
Output is correct |
24 |
Correct |
230 ms |
58228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
26316 KB |
Output is correct |
2 |
Correct |
150 ms |
31144 KB |
Output is correct |
3 |
Correct |
15 ms |
15956 KB |
Output is correct |
4 |
Correct |
14 ms |
15892 KB |
Output is correct |
5 |
Correct |
683 ms |
79860 KB |
Output is correct |
6 |
Correct |
455 ms |
79736 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
300 ms |
43624 KB |
Output is correct |
9 |
Correct |
401 ms |
52360 KB |
Output is correct |
10 |
Correct |
105 ms |
26828 KB |
Output is correct |
11 |
Correct |
138 ms |
31140 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
19 ms |
15956 KB |
Output is correct |
17 |
Correct |
15 ms |
15956 KB |
Output is correct |
18 |
Correct |
154 ms |
32032 KB |
Output is correct |
19 |
Correct |
182 ms |
37356 KB |
Output is correct |
20 |
Correct |
157 ms |
29448 KB |
Output is correct |
21 |
Correct |
157 ms |
32608 KB |
Output is correct |
22 |
Correct |
126 ms |
29272 KB |
Output is correct |
23 |
Correct |
159 ms |
32460 KB |
Output is correct |
24 |
Correct |
15 ms |
15956 KB |
Output is correct |
25 |
Correct |
16 ms |
15956 KB |
Output is correct |
26 |
Correct |
57 ms |
25580 KB |
Output is correct |
27 |
Correct |
42 ms |
23500 KB |
Output is correct |
28 |
Correct |
101 ms |
37052 KB |
Output is correct |
29 |
Correct |
99 ms |
37064 KB |
Output is correct |
30 |
Correct |
103 ms |
37076 KB |
Output is correct |
31 |
Correct |
101 ms |
36964 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
412 KB |
Output is correct |
47 |
Correct |
4 ms |
764 KB |
Output is correct |
48 |
Correct |
78 ms |
9980 KB |
Output is correct |
49 |
Correct |
65 ms |
9824 KB |
Output is correct |
50 |
Correct |
58 ms |
9824 KB |
Output is correct |
51 |
Correct |
60 ms |
9836 KB |
Output is correct |
52 |
Correct |
72 ms |
9824 KB |
Output is correct |
53 |
Correct |
134 ms |
19272 KB |
Output is correct |
54 |
Correct |
12 ms |
2240 KB |
Output is correct |
55 |
Correct |
42 ms |
6532 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
11 ms |
2004 KB |
Output is correct |
58 |
Correct |
3 ms |
1364 KB |
Output is correct |
59 |
Correct |
429 ms |
46000 KB |
Output is correct |
60 |
Correct |
660 ms |
67244 KB |
Output is correct |
61 |
Correct |
619 ms |
67232 KB |
Output is correct |
62 |
Correct |
615 ms |
67448 KB |
Output is correct |
63 |
Correct |
617 ms |
66072 KB |
Output is correct |
64 |
Correct |
605 ms |
67236 KB |
Output is correct |
65 |
Correct |
620 ms |
66668 KB |
Output is correct |
66 |
Correct |
192 ms |
26284 KB |
Output is correct |
67 |
Correct |
651 ms |
66300 KB |
Output is correct |
68 |
Correct |
102 ms |
37092 KB |
Output is correct |
69 |
Correct |
121 ms |
29260 KB |
Output is correct |
70 |
Correct |
241 ms |
58204 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
15 ms |
15956 KB |
Output is correct |
78 |
Correct |
14 ms |
15956 KB |
Output is correct |
79 |
Correct |
15 ms |
15956 KB |
Output is correct |
80 |
Correct |
15 ms |
15828 KB |
Output is correct |
81 |
Correct |
129 ms |
37020 KB |
Output is correct |
82 |
Correct |
246 ms |
58212 KB |
Output is correct |
83 |
Correct |
230 ms |
58228 KB |
Output is correct |
84 |
Correct |
809 ms |
80384 KB |
Output is correct |
85 |
Correct |
773 ms |
81872 KB |
Output is correct |
86 |
Correct |
456 ms |
78252 KB |
Output is correct |
87 |
Correct |
480 ms |
79836 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
458 ms |
80584 KB |
Output is correct |
90 |
Correct |
540 ms |
79616 KB |
Output is correct |
91 |
Correct |
553 ms |
79732 KB |
Output is correct |