Submission #630436

# Submission time Handle Problem Language Result Execution time Memory
630436 2022-08-16T11:11:39 Z JustInCase Catfish Farm (IOI22_fish) C++17
70 / 100
364 ms 84576 KB
#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

int64_t solveN300(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
			std::vector<int32_t> w) {
	const int32_t MAX_N = 300;

	std::array<std::array<int64_t, MAX_N>, MAX_N> wPrefSums {};
	std::array<std::array<std::array<int64_t, 3>, MAX_N + 1>, MAX_N + 1> dp {};

	auto getWRangeSum = [&wPrefSums](int32_t startRow, int32_t endRow, int32_t column) -> int64_t {
		if(endRow <= startRow) {
			return 0;
		}

		return wPrefSums[endRow - 1][column] - (startRow == 0 ? 0 : wPrefSums[startRow - 1][column]);
	};

	for(int32_t i = 0; i < m; i++) {
		wPrefSums[y[i]][x[i]] = (int64_t) w[i];
	}

	for(int32_t j = 0; j < n; j++) {
		for(int32_t i = 1; i < n; i++) {
			wPrefSums[i][j] += wPrefSums[i - 1][j];
		}
	}

	for(int32_t j = 1; j < n; j++) {
		for(int32_t lastK = 0; lastK <= n; lastK++) {
			for(int32_t k = 0; k <= n; k++) {
				if(k == 0) {
					remax(dp[j + 1][lastK][2], dp[j][lastK][0] + getWRangeSum(0, lastK, j));
					remax(dp[j + 1][lastK][2], dp[j][lastK][1] + getWRangeSum(0, lastK, j));
				}

				if(lastK <= k) {
					remax(dp[j + 1][k][0], dp[j][lastK][0] + getWRangeSum(lastK, k, j - 1));
					remax(dp[j + 1][k][0], dp[j][lastK][2] + getWRangeSum(lastK, k, j - 1));
				}
				if(lastK >= k) {
					remax(dp[j + 1][k][1], dp[j][lastK][1] + getWRangeSum(k, lastK, j));
					remax(dp[j + 1][k][1], dp[j][lastK][0] + getWRangeSum(k, lastK, j));
					remax(dp[j + 1][k][0], dp[j][lastK][2]);
				}
			}
		}
	}
	
	int64_t ans = 0;
	for(int32_t k = 0; k <= n; k++) {
		remax(ans, dp[n][k][0]);
		remax(ans, dp[n][k][1]);
		remax(ans, dp[n][k][2]);
	}

	return ans;
}

int64_t solveEvenXs(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
			std::vector<int32_t> w) {
	int64_t ans = 0;
	for(int32_t i = 0; i < m; i++) {
		ans += (int64_t) w[i];
	}
	return ans;
}

int64_t solveAllXs0Or1(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
			std::vector<int32_t> w) {
	const int32_t MAX_N = 1e5;

	std::array<std::array<int64_t, 2>, MAX_N> wPrefSums {};

	auto getWRangeSum = [&wPrefSums](int32_t startRow, int32_t endRow, int32_t column) -> int64_t {
		if(endRow <= startRow) {
			return 0;
		}

		return wPrefSums[endRow - 1][column] - (startRow == 0 ? 0 : wPrefSums[startRow - 1][column]);
	};

	for(int32_t i = 0; i < m; i++) {
		wPrefSums[y[i]][x[i]] = (int64_t) w[i];
	}

	for(int32_t j = 0; j < 2; j++) {
		for(int32_t i = 1; i < n; i++) {
			wPrefSums[i][j] += wPrefSums[i - 1][j];
		}
	}

	int64_t ans = 0;
	remax(ans, getWRangeSum(0, n, 1));
	remax(ans, getWRangeSum(0, n, 0));

	if(n > 2) {
		for(int32_t i = 0; i <= n; i++) {
			remax(ans, getWRangeSum(0, i, 0) + getWRangeSum(i, n, 1));
		}
	}

	return ans;
}

int64_t solveAllYs0(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> w) {
	const int32_t MAX_N = 1e5;

	std::array<int32_t, MAX_N + 3> wByColumn {};
	for(int32_t i = 0; i < m; i++) {
		wByColumn[x[i]] = w[i];
	}
	
	if(n == 2) {
		return std::max(wByColumn[0], wByColumn[1]);
	}

	std::array<std::array<std::array<int64_t, 2>, 2>, MAX_N + 3> dp {};
	dp[2][0][0] = 0;
	dp[2][0][1] = wByColumn[0];
	dp[2][1][0] = 0;
	dp[2][1][1] = 0;
	for(int32_t j = 2; j <= n + 1; j++) {
		for(int32_t p = 0; p < 2; p++) {
			for(int32_t q = 0; q < 2; q++) {
				remax(dp[j + 1][q][0], dp[j][p][q] + ((p == 1 && q != 1) ? wByColumn[j - 1] : 0));
				remax(dp[j + 1][q][1], dp[j][p][q] + (q != 1 ? wByColumn[j - 1] : 0));
			}
		}
	}

	return dp[n + 2][0][0];
}

int64_t solveN3000(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
			std::vector<int32_t> w) {
	const int32_t MAX_N = 3000;

	std::array<std::array<int64_t, MAX_N>, MAX_N> wPrefSums{};

	auto getWRangeSum = [&wPrefSums](int32_t startRow, int32_t endRow, int32_t column) -> int64_t {
		if(endRow <= startRow || column >= wPrefSums.size()) {
			return 0;
		}

		return wPrefSums[endRow - 1][column] - (startRow == 0 ? 0 : wPrefSums[startRow - 1][column]);
	};

	for(int32_t i = 0; i < m; i++) {
		wPrefSums[y[i]][x[i]] = (int64_t) w[i];
	}

	for(int32_t j = 0; j < n; j++) {
		for(int32_t i = 1; i < n; i++) {
			wPrefSums[i][j] += wPrefSums[i - 1][j];
		}
	}
	
	std::array<int64_t, MAX_N + 1> dpIncr{}, dpDecr{}, nDpIncr{}, nDpDecr{};
	for(int32_t j = 1; j <= n; j++) {
		int64_t pref = 0;
		for(int32_t k = 0; k <= n; k++) {
			remax(pref, dpIncr[k] - getWRangeSum(0, k, j - 1));
			nDpIncr[k] = std::max(pref + getWRangeSum(0, k, j - 1), dpDecr[0]);
		}

		int64_t suff = 0;
		for(int32_t k = n; k >= 0; k--) {
			remax(suff, std::max(dpIncr[k], dpDecr[k]) + getWRangeSum(0, k, j));
			nDpDecr[k] = suff - getWRangeSum(0, k, j);
		}

		std::swap(nDpIncr, dpIncr);
		std::swap(nDpDecr, dpDecr);
	}

	return std::max(dpIncr[0], dpDecr[0]);
}

int64_t maxWeights(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
                      std::vector<int32_t> w) {
	if(n <= 3000) {
		return solveN3000(n, m, x, y, w);
	}
	else if(std::all_of(x.begin(), x.end(), [](int32_t v) { return !(v & 1); })) {
		return solveEvenXs(n, m, x, y, w);
	}
	else if(std::all_of(x.begin(), x.end(), [](int32_t v) { return (v < 2); })) {
		return solveAllXs0Or1(n, m, x, y, w);
	}
	else if(std::all_of(y.begin(), y.end(), [](int32_t v) { return (v == 0); })) {
		return solveAllYs0(n, m, x, w);
	}

	return 0;
}

Compilation message

fish.cpp: In lambda function:
fish.cpp:165:35: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::array<std::array<long int, 3000>, 3000>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |   if(endRow <= startRow || column >= wPrefSums.size()) {
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 3156 KB Output is correct
2 Correct 28 ms 3796 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 84 ms 10848 KB Output is correct
6 Correct 85 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 70740 KB Output is correct
2 Correct 50 ms 7456 KB Output is correct
3 Correct 56 ms 8912 KB Output is correct
4 Correct 21 ms 3048 KB Output is correct
5 Correct 27 ms 3792 KB Output is correct
6 Correct 35 ms 70860 KB Output is correct
7 Correct 33 ms 70740 KB Output is correct
8 Correct 34 ms 70740 KB Output is correct
9 Correct 33 ms 70844 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 26 ms 4720 KB Output is correct
13 Correct 40 ms 5316 KB Output is correct
14 Correct 26 ms 4720 KB Output is correct
15 Correct 27 ms 4960 KB Output is correct
16 Correct 23 ms 4740 KB Output is correct
17 Correct 28 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 3 ms 3796 KB Output is correct
3 Correct 20 ms 5428 KB Output is correct
4 Correct 12 ms 4960 KB Output is correct
5 Correct 27 ms 6852 KB Output is correct
6 Correct 23 ms 6856 KB Output is correct
7 Correct 27 ms 6868 KB Output is correct
8 Correct 27 ms 6860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70716 KB Output is correct
2 Correct 38 ms 70748 KB Output is correct
3 Correct 34 ms 70792 KB Output is correct
4 Correct 34 ms 70840 KB Output is correct
5 Correct 38 ms 70716 KB Output is correct
6 Correct 35 ms 70792 KB Output is correct
7 Correct 34 ms 70776 KB Output is correct
8 Correct 36 ms 70732 KB Output is correct
9 Correct 34 ms 70820 KB Output is correct
10 Correct 37 ms 70772 KB Output is correct
11 Correct 37 ms 70740 KB Output is correct
12 Correct 35 ms 70868 KB Output is correct
13 Correct 34 ms 70800 KB Output is correct
14 Correct 36 ms 70772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70716 KB Output is correct
2 Correct 38 ms 70748 KB Output is correct
3 Correct 34 ms 70792 KB Output is correct
4 Correct 34 ms 70840 KB Output is correct
5 Correct 38 ms 70716 KB Output is correct
6 Correct 35 ms 70792 KB Output is correct
7 Correct 34 ms 70776 KB Output is correct
8 Correct 36 ms 70732 KB Output is correct
9 Correct 34 ms 70820 KB Output is correct
10 Correct 37 ms 70772 KB Output is correct
11 Correct 37 ms 70740 KB Output is correct
12 Correct 35 ms 70868 KB Output is correct
13 Correct 34 ms 70800 KB Output is correct
14 Correct 36 ms 70772 KB Output is correct
15 Correct 35 ms 70740 KB Output is correct
16 Correct 36 ms 70856 KB Output is correct
17 Correct 48 ms 72396 KB Output is correct
18 Correct 46 ms 72424 KB Output is correct
19 Correct 50 ms 72432 KB Output is correct
20 Correct 55 ms 72396 KB Output is correct
21 Correct 59 ms 72412 KB Output is correct
22 Correct 63 ms 73968 KB Output is correct
23 Correct 44 ms 71020 KB Output is correct
24 Correct 56 ms 71852 KB Output is correct
25 Correct 38 ms 70864 KB Output is correct
26 Correct 50 ms 71040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70716 KB Output is correct
2 Correct 38 ms 70748 KB Output is correct
3 Correct 34 ms 70792 KB Output is correct
4 Correct 34 ms 70840 KB Output is correct
5 Correct 38 ms 70716 KB Output is correct
6 Correct 35 ms 70792 KB Output is correct
7 Correct 34 ms 70776 KB Output is correct
8 Correct 36 ms 70732 KB Output is correct
9 Correct 34 ms 70820 KB Output is correct
10 Correct 37 ms 70772 KB Output is correct
11 Correct 37 ms 70740 KB Output is correct
12 Correct 35 ms 70868 KB Output is correct
13 Correct 34 ms 70800 KB Output is correct
14 Correct 36 ms 70772 KB Output is correct
15 Correct 35 ms 70740 KB Output is correct
16 Correct 36 ms 70856 KB Output is correct
17 Correct 48 ms 72396 KB Output is correct
18 Correct 46 ms 72424 KB Output is correct
19 Correct 50 ms 72432 KB Output is correct
20 Correct 55 ms 72396 KB Output is correct
21 Correct 59 ms 72412 KB Output is correct
22 Correct 63 ms 73968 KB Output is correct
23 Correct 44 ms 71020 KB Output is correct
24 Correct 56 ms 71852 KB Output is correct
25 Correct 38 ms 70864 KB Output is correct
26 Correct 50 ms 71040 KB Output is correct
27 Correct 242 ms 70920 KB Output is correct
28 Correct 103 ms 81264 KB Output is correct
29 Correct 326 ms 84484 KB Output is correct
30 Correct 364 ms 84484 KB Output is correct
31 Correct 317 ms 84476 KB Output is correct
32 Correct 130 ms 84476 KB Output is correct
33 Correct 363 ms 84492 KB Output is correct
34 Correct 359 ms 84576 KB Output is correct
35 Correct 302 ms 76380 KB Output is correct
36 Correct 348 ms 84492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 3 ms 3796 KB Output is correct
3 Correct 20 ms 5428 KB Output is correct
4 Correct 12 ms 4960 KB Output is correct
5 Correct 27 ms 6852 KB Output is correct
6 Correct 23 ms 6856 KB Output is correct
7 Correct 27 ms 6868 KB Output is correct
8 Correct 27 ms 6860 KB Output is correct
9 Incorrect 25 ms 2624 KB 1st lines differ - on the 1st token, expected: '99999', found: '0'
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 3156 KB Output is correct
2 Correct 28 ms 3796 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 84 ms 10848 KB Output is correct
6 Correct 85 ms 10848 KB Output is correct
7 Correct 36 ms 70740 KB Output is correct
8 Correct 50 ms 7456 KB Output is correct
9 Correct 56 ms 8912 KB Output is correct
10 Correct 21 ms 3048 KB Output is correct
11 Correct 27 ms 3792 KB Output is correct
12 Correct 35 ms 70860 KB Output is correct
13 Correct 33 ms 70740 KB Output is correct
14 Correct 34 ms 70740 KB Output is correct
15 Correct 33 ms 70844 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 26 ms 4720 KB Output is correct
19 Correct 40 ms 5316 KB Output is correct
20 Correct 26 ms 4720 KB Output is correct
21 Correct 27 ms 4960 KB Output is correct
22 Correct 23 ms 4740 KB Output is correct
23 Correct 28 ms 4956 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 3 ms 3796 KB Output is correct
26 Correct 20 ms 5428 KB Output is correct
27 Correct 12 ms 4960 KB Output is correct
28 Correct 27 ms 6852 KB Output is correct
29 Correct 23 ms 6856 KB Output is correct
30 Correct 27 ms 6868 KB Output is correct
31 Correct 27 ms 6860 KB Output is correct
32 Correct 35 ms 70716 KB Output is correct
33 Correct 38 ms 70748 KB Output is correct
34 Correct 34 ms 70792 KB Output is correct
35 Correct 34 ms 70840 KB Output is correct
36 Correct 38 ms 70716 KB Output is correct
37 Correct 35 ms 70792 KB Output is correct
38 Correct 34 ms 70776 KB Output is correct
39 Correct 36 ms 70732 KB Output is correct
40 Correct 34 ms 70820 KB Output is correct
41 Correct 37 ms 70772 KB Output is correct
42 Correct 37 ms 70740 KB Output is correct
43 Correct 35 ms 70868 KB Output is correct
44 Correct 34 ms 70800 KB Output is correct
45 Correct 36 ms 70772 KB Output is correct
46 Correct 35 ms 70740 KB Output is correct
47 Correct 36 ms 70856 KB Output is correct
48 Correct 48 ms 72396 KB Output is correct
49 Correct 46 ms 72424 KB Output is correct
50 Correct 50 ms 72432 KB Output is correct
51 Correct 55 ms 72396 KB Output is correct
52 Correct 59 ms 72412 KB Output is correct
53 Correct 63 ms 73968 KB Output is correct
54 Correct 44 ms 71020 KB Output is correct
55 Correct 56 ms 71852 KB Output is correct
56 Correct 38 ms 70864 KB Output is correct
57 Correct 50 ms 71040 KB Output is correct
58 Correct 242 ms 70920 KB Output is correct
59 Correct 103 ms 81264 KB Output is correct
60 Correct 326 ms 84484 KB Output is correct
61 Correct 364 ms 84484 KB Output is correct
62 Correct 317 ms 84476 KB Output is correct
63 Correct 130 ms 84476 KB Output is correct
64 Correct 363 ms 84492 KB Output is correct
65 Correct 359 ms 84576 KB Output is correct
66 Correct 302 ms 76380 KB Output is correct
67 Correct 348 ms 84492 KB Output is correct
68 Incorrect 25 ms 2624 KB 1st lines differ - on the 1st token, expected: '99999', found: '0'
69 Halted 0 ms 0 KB -