Submission #626238

# Submission time Handle Problem Language Result Execution time Memory
626238 2022-08-11T10:18:34 Z Temmie Catfish Farm (IOI22_fish) C++17
100 / 100
275 ms 52400 KB
#include <bits/stdc++.h>

long long max_weights(int n, int m, std::vector <int> _x, std::vector <int> _y, std::vector <int> _w) {
	std::vector <std::vector <std::pair <int, int>>> of(n);
	for (int i = 0; i < m; i++) {
		of[_x[i]].emplace_back(_y[i], _w[i]);
	}
	for (int i = 0; i < n; i++) {
		of[i].emplace_back(0, 0);
		of[i].emplace_back(n, 0);
		std::sort(of[i].begin(), of[i].end());
		if (!of[i][1].first) {
			of[i].erase(of[i].begin());
		}
	}
	std::vector <std::vector <std::vector <long long>>> dp(n, std::vector <std::vector <long long>> (3));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 3; j++) {
			dp[i][j].resize(of[i].size());
		}
	}
	std::vector <std::vector <long long>> pre(n);
	for (int i = 0; i < n; i++) {
		pre[i].resize(of[i].size());
		pre[i][0] = of[i][0].second;
		for (int j = 1; j < (int) of[i].size(); j++) {
			pre[i][j] = pre[i][j - 1] + of[i][j].second;
		}
	}
	dp[0][0].assign(dp[0][0].size(), 0);
	for (int nxt = 0, j = 1; j < (int) of[0].size(); j++) {
		while (nxt + 1 < (int) of[1].size() && of[1][nxt + 1].first < of[0][j].first) {
			nxt++;
		}
		dp[0][1][j] = pre[1][nxt];
	}
	for (int i = 1; i < n; i++) {
		std::vector <long long> pre_mx0(dp[i - 1][0].size());
		pre_mx0[0] = dp[i - 1][0][0];
		for (int j = 1; j < (int) dp[i - 1][0].size(); j++) {
			pre_mx0[j] = std::max(dp[i - 1][0][j], pre_mx0[j - 1]);
		}
		std::vector <long long> suf_mx[2];
		for (int k = 0; k <= 1; k++) {
			suf_mx[k].resize(dp[i - 1][k + 1].size());
			suf_mx[k].back() = dp[i - 1][k + 1].back();
			for (int j = (int) dp[i - 1][k + 1].size() - 2; j >= 0; j--) {
				suf_mx[k][j] = std::max(dp[i - 1][k + 1][j], suf_mx[k][j + 1]);
			}
		}
		std::vector <long long> psuf_mx[2];
		std::vector <long long> ppre_mx[2];
		for (int k = 0; k <= 1; k++) {
			psuf_mx[k].resize(i > 1 ? dp[i - 2][k + 1].size() : 1, 0);
			if (i > 1) {
				ppre_mx[k].resize(dp[i - 2][k + 1].size());
				for (int ptr = 0, j = 0; j < (int) dp[i - 2][k + 1].size(); j++) {
					while (ptr + 1 < (int) of[i - 1].size() && of[i - 1][ptr + 1].first < of[i - 2][j].first) {
						ptr++;
					}
					ppre_mx[k][j] = dp[i - 2][k + 1][j] - pre[i - 1][ptr];
					if (j) {
						ppre_mx[k][j] = std::max(ppre_mx[k][j], ppre_mx[k][j - 1]);
					}
				}
				psuf_mx[k].back() = dp[i - 2][k + 1].back();
				for (int j = (int) dp[i - 2][k + 1].size() - 2; j >= 0; j--) {
					psuf_mx[k][j] = std::max(dp[i - 2][k + 1][j], psuf_mx[k][j + 1]);
				}
			}
		}
		dp[i][0][0] = dp[i][0][0] = std::max({ pre_mx0[0], psuf_mx[0][0], psuf_mx[1][0] });
		dp[i][1][0] = pre_mx0[0];
		dp[i][2][0] = std::max(suf_mx[0][0], suf_mx[1][0]);
		if (i > 1) {
			for (int prv = 0, nxt = 0, nnxt = 0, j = 0; j < (int) dp[i][0].size(); j++) {
				while (prv + 1 < (int) of[i - 2].size() && of[i - 2][prv + 1].first < of[i][j].first) {
					prv++;
				}
				while (nxt + 1 < (int) of[i - 1].size() && of[i - 1][nxt + 1].first < of[i][j].first) {
					nxt++;
				}
				while (i + 1 < n && nnxt + 1 < (int) of[i + 1].size() && of[i + 1][nnxt + 1].first < of[i][j].first) {
					nnxt++;
				}
				dp[i][0][j] = std::max(dp[i][0][j], std::max({ ppre_mx[0][prv] + pre[i - 1][nxt],
				ppre_mx[1][prv] + pre[i - 1][nxt], psuf_mx[0][prv], psuf_mx[1][prv] }) - (j ? pre[i][j - 1] : 0LL));
				dp[i][1][j] = std::max(dp[i][1][j], std::max({ ppre_mx[0][prv] + pre[i - 1][nxt],
				ppre_mx[1][prv] + pre[i - 1][nxt], psuf_mx[0][prv], psuf_mx[1][prv] }) + (i + 1 < n ? pre[i + 1][nnxt] : 0LL));
			}
		}
		for (int prv = 0, nxt = 0, j = 1; j < (int) of[i].size(); j++) {
			while (prv + 1 < (int) of[i - 1].size() && of[i - 1][prv + 1].first < of[i][j].first) {
				prv++;
			}
			while (i + 1 < n && nxt + 1 < (int) of[i + 1].size() && of[i + 1][nxt + 1].first < of[i][j].first) {
				nxt++;
			}
			dp[i][0][j] = std::max(dp[i][0][j], pre_mx0[prv] + pre[i - 1][prv] - pre[i][j - 1]);
			dp[i][1][j] = std::max(dp[i][1][j], pre_mx0[prv] + pre[i - 1][prv] + (i + 1 < n ? pre[i + 1][nxt] : 0LL));
			dp[i][2][j] = std::max(
			suf_mx[0][prv] + (i + 1 < n ? pre[i + 1][nxt] : 0LL) - pre[i][j - 1],
			suf_mx[1][prv] + (i + 1 < n ? pre[i + 1][nxt] : 0LL) - pre[i][j - 1]);
		}
	}
	long long ans = 0;
	for (int k = 0; k < 3; k++) {
		for (long long x : dp[n - 1][k]) {
			ans = std::max(ans, x);
		}
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 120 ms 35256 KB Output is correct
2 Correct 165 ms 40140 KB Output is correct
3 Correct 94 ms 30800 KB Output is correct
4 Correct 86 ms 30824 KB Output is correct
5 Correct 266 ms 52400 KB Output is correct
6 Correct 259 ms 50192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 151 ms 42340 KB Output is correct
3 Correct 169 ms 48788 KB Output is correct
4 Correct 113 ms 35332 KB Output is correct
5 Correct 126 ms 40144 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 97 ms 30708 KB Output is correct
11 Correct 92 ms 30796 KB Output is correct
12 Correct 124 ms 35324 KB Output is correct
13 Correct 131 ms 40208 KB Output is correct
14 Correct 115 ms 35116 KB Output is correct
15 Correct 115 ms 38884 KB Output is correct
16 Correct 115 ms 35036 KB Output is correct
17 Correct 128 ms 38820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 30864 KB Output is correct
2 Correct 93 ms 30752 KB Output is correct
3 Correct 131 ms 29928 KB Output is correct
4 Correct 160 ms 32248 KB Output is correct
5 Correct 161 ms 35240 KB Output is correct
6 Correct 193 ms 35148 KB Output is correct
7 Correct 129 ms 35236 KB Output is correct
8 Correct 181 ms 35260 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 1 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 468 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 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 1 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 468 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 29 ms 3284 KB Output is correct
18 Correct 21 ms 3524 KB Output is correct
19 Correct 18 ms 3524 KB Output is correct
20 Correct 17 ms 3412 KB Output is correct
21 Correct 18 ms 3412 KB Output is correct
22 Correct 34 ms 6596 KB Output is correct
23 Correct 5 ms 1008 KB Output is correct
24 Correct 14 ms 2308 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 4 ms 852 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 1 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 468 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 29 ms 3284 KB Output is correct
18 Correct 21 ms 3524 KB Output is correct
19 Correct 18 ms 3524 KB Output is correct
20 Correct 17 ms 3412 KB Output is correct
21 Correct 18 ms 3412 KB Output is correct
22 Correct 34 ms 6596 KB Output is correct
23 Correct 5 ms 1008 KB Output is correct
24 Correct 14 ms 2308 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 4 ms 852 KB Output is correct
27 Correct 4 ms 1336 KB Output is correct
28 Correct 98 ms 15340 KB Output is correct
29 Correct 144 ms 21212 KB Output is correct
30 Correct 139 ms 20716 KB Output is correct
31 Correct 140 ms 20712 KB Output is correct
32 Correct 120 ms 21604 KB Output is correct
33 Correct 147 ms 20712 KB Output is correct
34 Correct 148 ms 20720 KB Output is correct
35 Correct 58 ms 8844 KB Output is correct
36 Correct 141 ms 21444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 30864 KB Output is correct
2 Correct 93 ms 30752 KB Output is correct
3 Correct 131 ms 29928 KB Output is correct
4 Correct 160 ms 32248 KB Output is correct
5 Correct 161 ms 35240 KB Output is correct
6 Correct 193 ms 35148 KB Output is correct
7 Correct 129 ms 35236 KB Output is correct
8 Correct 181 ms 35260 KB Output is correct
9 Correct 148 ms 35264 KB Output is correct
10 Correct 94 ms 18940 KB Output is correct
11 Correct 158 ms 37580 KB Output is correct
12 Correct 1 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 80 ms 30820 KB Output is correct
19 Correct 87 ms 30796 KB Output is correct
20 Correct 88 ms 30820 KB Output is correct
21 Correct 79 ms 30796 KB Output is correct
22 Correct 145 ms 36300 KB Output is correct
23 Correct 187 ms 43476 KB Output is correct
24 Correct 191 ms 44252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 35256 KB Output is correct
2 Correct 165 ms 40140 KB Output is correct
3 Correct 94 ms 30800 KB Output is correct
4 Correct 86 ms 30824 KB Output is correct
5 Correct 266 ms 52400 KB Output is correct
6 Correct 259 ms 50192 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 151 ms 42340 KB Output is correct
9 Correct 169 ms 48788 KB Output is correct
10 Correct 113 ms 35332 KB Output is correct
11 Correct 126 ms 40144 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 97 ms 30708 KB Output is correct
17 Correct 92 ms 30796 KB Output is correct
18 Correct 124 ms 35324 KB Output is correct
19 Correct 131 ms 40208 KB Output is correct
20 Correct 115 ms 35116 KB Output is correct
21 Correct 115 ms 38884 KB Output is correct
22 Correct 115 ms 35036 KB Output is correct
23 Correct 128 ms 38820 KB Output is correct
24 Correct 86 ms 30864 KB Output is correct
25 Correct 93 ms 30752 KB Output is correct
26 Correct 131 ms 29928 KB Output is correct
27 Correct 160 ms 32248 KB Output is correct
28 Correct 161 ms 35240 KB Output is correct
29 Correct 193 ms 35148 KB Output is correct
30 Correct 129 ms 35236 KB Output is correct
31 Correct 181 ms 35260 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 1 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 468 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 4 ms 340 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 29 ms 3284 KB Output is correct
49 Correct 21 ms 3524 KB Output is correct
50 Correct 18 ms 3524 KB Output is correct
51 Correct 17 ms 3412 KB Output is correct
52 Correct 18 ms 3412 KB Output is correct
53 Correct 34 ms 6596 KB Output is correct
54 Correct 5 ms 1008 KB Output is correct
55 Correct 14 ms 2308 KB Output is correct
56 Correct 2 ms 384 KB Output is correct
57 Correct 4 ms 852 KB Output is correct
58 Correct 4 ms 1336 KB Output is correct
59 Correct 98 ms 15340 KB Output is correct
60 Correct 144 ms 21212 KB Output is correct
61 Correct 139 ms 20716 KB Output is correct
62 Correct 140 ms 20712 KB Output is correct
63 Correct 120 ms 21604 KB Output is correct
64 Correct 147 ms 20712 KB Output is correct
65 Correct 148 ms 20720 KB Output is correct
66 Correct 58 ms 8844 KB Output is correct
67 Correct 141 ms 21444 KB Output is correct
68 Correct 148 ms 35264 KB Output is correct
69 Correct 94 ms 18940 KB Output is correct
70 Correct 158 ms 37580 KB Output is correct
71 Correct 1 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 80 ms 30820 KB Output is correct
78 Correct 87 ms 30796 KB Output is correct
79 Correct 88 ms 30820 KB Output is correct
80 Correct 79 ms 30796 KB Output is correct
81 Correct 145 ms 36300 KB Output is correct
82 Correct 187 ms 43476 KB Output is correct
83 Correct 191 ms 44252 KB Output is correct
84 Correct 218 ms 48256 KB Output is correct
85 Correct 232 ms 51168 KB Output is correct
86 Correct 248 ms 46276 KB Output is correct
87 Correct 275 ms 49356 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 266 ms 49380 KB Output is correct
90 Correct 231 ms 49520 KB Output is correct
91 Correct 206 ms 49780 KB Output is correct