#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
struct Fish {
int x, y, w;
} fish[3 * MAXN];
ll f[303][303][303], maxPre[3003][303], maxSuf[3003][303];
ll dp[3003][3003], dp2[3003][3003], sum[3003][3003];
int val[MAXN], nSize, numFish;
ll sub1(void) {
ll sum(0);
for (int i = 0; i < numFish; ++i)
sum += fish[i].w;
return sum;
}
ll sub2(void) {
ll ans(0), sum0(0), sum1(0);
int j(numFish);
for (int i = 0; i < numFish; ++i) {
if(fish[i].x == 0) {
sum0 += fish[i].w;
} else {
if(j == numFish)
j = i;
sum1 += fish[i].w;
}
}
ans = max(sum0, sum1);
if(nSize == 2)
return ans;
sum0 = 0;
for (int i = 0; i < numFish; ++i) {
if(fish[i].x == 1)
break;
sum0 += fish[i].w;
while(j < numFish && fish[j].y <= fish[i].y) {
sum1 -= fish[j].w;
++j;
}
ans = max(ans, sum0 + sum1);
}
return ans;
}
ll sub3(void) {
for (int i = 0; i <= 1; ++i)
dp[i][0] = dp[i][1] = dp[i][2] = -1e18;
for (int i = 0; i < numFish; ++i)
val[fish[i].x] = fish[i].w;
dp[0][2] = 0;
for (int i = 0; i < nSize; ++i) {
int pre(i & 1), cur(!pre);
dp[cur][0] = max(dp[pre][0], max(dp[pre][1], dp[pre][2] + (i > 0 ? val[i - 1] : 0)));
dp[cur][1] = dp[pre][0] + val[i];
dp[cur][2] = max(dp[pre][2], dp[pre][1]);
}
return max({dp[nSize & 1][0], dp[nSize & 1][1], dp[nSize & 1][2]});
}
ll sub5(void) {
for (int i = 0; i < nSize; ++i) {
for (int j = 0; j <= nSize; ++j)
sum[i][j] = 0;
}
for (int i = 0; i < numFish; ++i)
sum[fish[i].x][fish[i].y + 1] += fish[i].w;
for (int i = 0; i < nSize; ++i) {
for (int j = 1; j <= nSize; ++j)
sum[i][j] += sum[i][j - 1];
}
for (int j = 0; j <= nSize; ++j) {
for (int k = 0; k <= nSize; ++k) {
f[0][j][k] = -1e18;
}
}
f[0][0][0] = 0;
for (int j = 0; j <= nSize; ++j) {
for (int k = 0; k <= nSize; ++k) {
maxPre[j][k] = f[0][j][k];
if(k > 0)
maxPre[j][k] = max(maxPre[j][k], maxPre[j][k - 1]);
}
}
for (int j = nSize; j >= 0; --j) {
for (int k = nSize; k >= 0; --k) {
maxSuf[j][k] = f[0][j][k];
if(k + 1 < nSize)
maxSuf[j][k] = max(maxSuf[j][k], maxSuf[j][k + 1]);
}
}
for (int i = 0; i < nSize; ++i) {
for (int coln = 0; coln <= nSize; ++coln) {
for (int pcol = 0; pcol <= nSize; ++pcol) {
if(coln <= pcol) {
f[i + 1][coln][pcol] = maxSuf[pcol][0] + sum[i][pcol] - sum[i][coln];
continue;
}
f[i + 1][coln][pcol] = maxPre[pcol][nSize] + (i > 0 ? sum[i - 1][coln] : 0);
f[i + 1][coln][pcol] = max(f[i + 1][coln][pcol], maxSuf[pcol][0]);
}
}
for (int j = 0; j <= nSize; ++j) {
for (int k = 0; k <= nSize; ++k) {
maxPre[j][k] = f[i + 1][j][k] - sum[i][max(j, k)];
if(k > 0)
maxPre[j][k] = max(maxPre[j][k], maxPre[j][k - 1]);
}
}
for (int j = nSize; j >= 0; --j) {
for (int k = nSize; k >= 0; --k) {
maxSuf[j][k] = f[i + 1][j][k];
if(k < nSize)
maxSuf[j][k] = max(maxSuf[j][k], maxSuf[j][k + 1]);
}
}
}
ll res(0);
for (int j = 0; j <= nSize; ++j)
res = max(res, maxSuf[j][0]);
return res;
}
ll sub6(void) {
for (int i = 0; i <= nSize; ++i) {
for (int j = 0; j <= nSize; ++j)
sum[i][j] = 0;
}
for (int i = 0; i < numFish; ++i)
sum[fish[i].x][fish[i].y + 1] += fish[i].w;
for (int i = 0; i < nSize; ++i) {
for (int j = 1; j <= nSize; ++j)
sum[i][j] += sum[i][j - 1];
}
for (int j = 0; j <= nSize; ++j)
dp[0][j] = dp2[0][j] = -1e18;
dp[0][0] = dp2[0][0] = 0;
for (int j = 0; j <= nSize; ++j) {
ll val = dp2[0][j];
maxPre[j][0] = (j == 0) ? val : max(maxPre[j - 1][0], val);
}
for (int j = nSize; j >= 0; --j) {
ll val = dp[0][j] + sum[0][j];
ll val2 = dp[0][j];
maxSuf[j][0] = (j == nSize) ? val : max(maxSuf[j + 1][0], val);
maxSuf[j][1] = (j == nSize) ? val2 : max(maxSuf[j + 1][1], val2);
}
for (int i = 0; i < nSize; ++i) {
for (int coln = 0; coln <= nSize; ++coln) {
dp[i + 1][coln] = maxSuf[coln][0] - sum[i][coln];
dp[i + 1][coln] = max(dp[i + 1][coln], dp[i][coln]);
dp[i + 1][coln] = max(dp[i + 1][coln], maxPre[coln][0] + (i > 0 ? sum[i - 1][coln] : 0));
dp2[i + 1][coln] = maxPre[coln][0] + (i > 0 ? sum[i - 1][coln] : 0) - sum[i][coln];
dp2[i + 1][coln] = max(dp2[i + 1][coln], maxSuf[coln][1]);
dp2[i + 1][coln] = max(dp2[i + 1][coln], dp[i][coln] - sum[i][coln]);
}
for (int j = 0; j <= nSize; ++j) {
ll val = dp2[i + 1][j];
maxPre[j][0] = (j == 0) ? val : max(maxPre[j - 1][0], val);
}
for (int j = nSize; j >= 0; --j) {
ll val = dp[i + 1][j] + sum[i + 1][j];
ll val2 = dp[i + 1][j];
maxSuf[j][0] = (j == nSize) ? val : max(maxSuf[j + 1][0], val);
maxSuf[j][1] = (j == nSize) ? val2 : max(maxSuf[j + 1][1], val2);
}
}
ll res(0);
for (int j = 0; j <= nSize; ++j)
res = max(res, dp[nSize][j]);
return res;
}
ll max_weights(int _N, int _M, vector<int> _X, vector<int> _Y, vector<int> _W) {
nSize = _N, numFish = _M;
bool check1(1), check2(1), check3(1);
for (int i = 0; i < numFish; ++i) {
fish[i] = {_X[i], _Y[i], _W[i]};
check1 &= !(fish[i].x & 1);
check2 &= (fish[i].x <= 1);
check3 &= (fish[i].y == 0);
}
sort(fish, fish + numFish, [](const Fish &a, const Fish &b) {
return (a.x != b.x) ? a.x < b.x : a.y < b.y;
});
if(check1) {
return sub1();
} else
if(check2) {
return sub2();
} else
if(check3) {
return sub3();
} else
if(nSize <= 300) {
return sub5();
} else
if(nSize <= 3000) {
return sub6();
}
}
#ifdef Nhoksocqt1
bool dx[3003][3003];
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "fish"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> _X, _Y, _W;
int _N, _M;
cin >> _N >> _M;
_X.resize(_M), _Y.resize(_M), _W.resize(_M);
for (int i = 0; i < _M; ++i) {
cin >> _X[i] >> _Y[i] >> _W[i];
do { _X[i] = Random(0, _N - 1), _Y[i] = Random(0, _N - 1), _W[i] = Random(1, 100); } while(dx[_X[i]][_Y[i]]); dx[_X[i]][_Y[i]] = 1; cout << _X[i] << ' ' << _Y[i] << ' ' << _W[i] << '\n';
}
ll ans = max_weights(_N, _M, _X, _Y, _W);
cout << "ANSWER: " << ans << '\n';
cout << "SUB6: " << sub6() << '\n';
return 0;
}
#endif // Nhoksocqt1
Compilation message
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:257:1: warning: control reaches end of non-void function [-Wreturn-type]
257 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
5724 KB |
Output is correct |
2 |
Correct |
29 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2440 KB |
Output is correct |
5 |
Correct |
95 ms |
18024 KB |
Output is correct |
6 |
Correct |
103 ms |
18260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
50 ms |
10924 KB |
Output is correct |
3 |
Correct |
62 ms |
12724 KB |
Output is correct |
4 |
Correct |
23 ms |
5724 KB |
Output is correct |
5 |
Correct |
31 ms |
6608 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
24 ms |
5744 KB |
Output is correct |
13 |
Correct |
30 ms |
6736 KB |
Output is correct |
14 |
Correct |
25 ms |
5720 KB |
Output is correct |
15 |
Correct |
28 ms |
5980 KB |
Output is correct |
16 |
Correct |
25 ms |
5612 KB |
Output is correct |
17 |
Correct |
33 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
19 ms |
8784 KB |
Output is correct |
4 |
Correct |
12 ms |
8024 KB |
Output is correct |
5 |
Correct |
32 ms |
10580 KB |
Output is correct |
6 |
Correct |
27 ms |
10028 KB |
Output is correct |
7 |
Correct |
32 ms |
10584 KB |
Output is correct |
8 |
Correct |
30 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
65 ms |
119380 KB |
Output is correct |
10 |
Correct |
279 ms |
233548 KB |
Output is correct |
11 |
Correct |
40 ms |
119380 KB |
Output is correct |
12 |
Correct |
260 ms |
233680 KB |
Output is correct |
13 |
Correct |
11 ms |
63836 KB |
Output is correct |
14 |
Correct |
256 ms |
233552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
65 ms |
119380 KB |
Output is correct |
10 |
Correct |
279 ms |
233548 KB |
Output is correct |
11 |
Correct |
40 ms |
119380 KB |
Output is correct |
12 |
Correct |
260 ms |
233680 KB |
Output is correct |
13 |
Correct |
11 ms |
63836 KB |
Output is correct |
14 |
Correct |
256 ms |
233552 KB |
Output is correct |
15 |
Correct |
257 ms |
233392 KB |
Output is correct |
16 |
Correct |
11 ms |
64092 KB |
Output is correct |
17 |
Correct |
268 ms |
235280 KB |
Output is correct |
18 |
Correct |
264 ms |
235300 KB |
Output is correct |
19 |
Correct |
270 ms |
235396 KB |
Output is correct |
20 |
Correct |
267 ms |
235228 KB |
Output is correct |
21 |
Correct |
272 ms |
235336 KB |
Output is correct |
22 |
Correct |
277 ms |
237068 KB |
Output is correct |
23 |
Correct |
259 ms |
233900 KB |
Output is correct |
24 |
Correct |
260 ms |
234748 KB |
Output is correct |
25 |
Correct |
253 ms |
233556 KB |
Output is correct |
26 |
Correct |
257 ms |
234012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
65 ms |
119380 KB |
Output is correct |
10 |
Correct |
279 ms |
233548 KB |
Output is correct |
11 |
Correct |
40 ms |
119380 KB |
Output is correct |
12 |
Correct |
260 ms |
233680 KB |
Output is correct |
13 |
Correct |
11 ms |
63836 KB |
Output is correct |
14 |
Correct |
256 ms |
233552 KB |
Output is correct |
15 |
Correct |
257 ms |
233392 KB |
Output is correct |
16 |
Correct |
11 ms |
64092 KB |
Output is correct |
17 |
Correct |
268 ms |
235280 KB |
Output is correct |
18 |
Correct |
264 ms |
235300 KB |
Output is correct |
19 |
Correct |
270 ms |
235396 KB |
Output is correct |
20 |
Correct |
267 ms |
235228 KB |
Output is correct |
21 |
Correct |
272 ms |
235336 KB |
Output is correct |
22 |
Correct |
277 ms |
237068 KB |
Output is correct |
23 |
Correct |
259 ms |
233900 KB |
Output is correct |
24 |
Correct |
260 ms |
234748 KB |
Output is correct |
25 |
Correct |
253 ms |
233556 KB |
Output is correct |
26 |
Correct |
257 ms |
234012 KB |
Output is correct |
27 |
Correct |
161 ms |
230308 KB |
Output is correct |
28 |
Correct |
79 ms |
81104 KB |
Output is correct |
29 |
Correct |
273 ms |
244884 KB |
Output is correct |
30 |
Correct |
263 ms |
244984 KB |
Output is correct |
31 |
Correct |
277 ms |
244980 KB |
Output is correct |
32 |
Correct |
104 ms |
68208 KB |
Output is correct |
33 |
Correct |
288 ms |
244972 KB |
Output is correct |
34 |
Correct |
278 ms |
244976 KB |
Output is correct |
35 |
Correct |
221 ms |
236884 KB |
Output is correct |
36 |
Correct |
252 ms |
244844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
19 ms |
8784 KB |
Output is correct |
4 |
Correct |
12 ms |
8024 KB |
Output is correct |
5 |
Correct |
32 ms |
10580 KB |
Output is correct |
6 |
Correct |
27 ms |
10028 KB |
Output is correct |
7 |
Correct |
32 ms |
10584 KB |
Output is correct |
8 |
Correct |
30 ms |
10588 KB |
Output is correct |
9 |
Runtime error |
886 ms |
912568 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
5724 KB |
Output is correct |
2 |
Correct |
29 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2440 KB |
Output is correct |
5 |
Correct |
95 ms |
18024 KB |
Output is correct |
6 |
Correct |
103 ms |
18260 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
50 ms |
10924 KB |
Output is correct |
9 |
Correct |
62 ms |
12724 KB |
Output is correct |
10 |
Correct |
23 ms |
5724 KB |
Output is correct |
11 |
Correct |
31 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
24 ms |
5744 KB |
Output is correct |
19 |
Correct |
30 ms |
6736 KB |
Output is correct |
20 |
Correct |
25 ms |
5720 KB |
Output is correct |
21 |
Correct |
28 ms |
5980 KB |
Output is correct |
22 |
Correct |
25 ms |
5612 KB |
Output is correct |
23 |
Correct |
33 ms |
5980 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
6488 KB |
Output is correct |
26 |
Correct |
19 ms |
8784 KB |
Output is correct |
27 |
Correct |
12 ms |
8024 KB |
Output is correct |
28 |
Correct |
32 ms |
10580 KB |
Output is correct |
29 |
Correct |
27 ms |
10028 KB |
Output is correct |
30 |
Correct |
32 ms |
10584 KB |
Output is correct |
31 |
Correct |
30 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
12636 KB |
Output is correct |
33 |
Correct |
2 ms |
16732 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2496 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
2 ms |
12636 KB |
Output is correct |
40 |
Correct |
65 ms |
119380 KB |
Output is correct |
41 |
Correct |
279 ms |
233548 KB |
Output is correct |
42 |
Correct |
40 ms |
119380 KB |
Output is correct |
43 |
Correct |
260 ms |
233680 KB |
Output is correct |
44 |
Correct |
11 ms |
63836 KB |
Output is correct |
45 |
Correct |
256 ms |
233552 KB |
Output is correct |
46 |
Correct |
257 ms |
233392 KB |
Output is correct |
47 |
Correct |
11 ms |
64092 KB |
Output is correct |
48 |
Correct |
268 ms |
235280 KB |
Output is correct |
49 |
Correct |
264 ms |
235300 KB |
Output is correct |
50 |
Correct |
270 ms |
235396 KB |
Output is correct |
51 |
Correct |
267 ms |
235228 KB |
Output is correct |
52 |
Correct |
272 ms |
235336 KB |
Output is correct |
53 |
Correct |
277 ms |
237068 KB |
Output is correct |
54 |
Correct |
259 ms |
233900 KB |
Output is correct |
55 |
Correct |
260 ms |
234748 KB |
Output is correct |
56 |
Correct |
253 ms |
233556 KB |
Output is correct |
57 |
Correct |
257 ms |
234012 KB |
Output is correct |
58 |
Correct |
161 ms |
230308 KB |
Output is correct |
59 |
Correct |
79 ms |
81104 KB |
Output is correct |
60 |
Correct |
273 ms |
244884 KB |
Output is correct |
61 |
Correct |
263 ms |
244984 KB |
Output is correct |
62 |
Correct |
277 ms |
244980 KB |
Output is correct |
63 |
Correct |
104 ms |
68208 KB |
Output is correct |
64 |
Correct |
288 ms |
244972 KB |
Output is correct |
65 |
Correct |
278 ms |
244976 KB |
Output is correct |
66 |
Correct |
221 ms |
236884 KB |
Output is correct |
67 |
Correct |
252 ms |
244844 KB |
Output is correct |
68 |
Runtime error |
886 ms |
912568 KB |
Execution killed with signal 11 |
69 |
Halted |
0 ms |
0 KB |
- |