Submission #630816

# Submission time Handle Problem Language Result Execution time Memory
630816 2022-08-17T07:03:43 Z JustInCase Catfish Farm (IOI22_fish) C++17
100 / 100
498 ms 65836 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

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::vector<Catfish>> catfishByColumn(n);

	for(int32_t i = 0; i < m; i++) {
		catfishByColumn[x[i]].push_back(Catfish(y[i], i, w[i]));
	}

	for(int32_t i = 0; i < n; i++) {
		std::sort(catfishByColumn[i].begin(), catfishByColumn[i].end());

		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 = std::lower_bound(catfishByColumn[column].begin(), catfishByColumn[column].end(), Catfish(row, -1, 0));
		if(p == catfishByColumn[column].begin()) {
			return 0;
		}

		p--;
		return p->prefSum;
	};

	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);
			}
		}
	}

	return std::max(dp.back().begin()->incrVal, dp.back().begin()->decrVal);
}
# Verdict Execution time Memory Grader output
1 Correct 82 ms 20436 KB Output is correct
2 Correct 102 ms 23720 KB Output is correct
3 Correct 16 ms 13548 KB Output is correct
4 Correct 17 ms 13576 KB Output is correct
5 Correct 391 ms 62940 KB Output is correct
6 Correct 293 ms 65476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 208 ms 33840 KB Output is correct
3 Correct 247 ms 40240 KB Output is correct
4 Correct 71 ms 20420 KB Output is correct
5 Correct 94 ms 23740 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 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 14 ms 13552 KB Output is correct
11 Correct 15 ms 13524 KB Output is correct
12 Correct 99 ms 25612 KB Output is correct
13 Correct 124 ms 30000 KB Output is correct
14 Correct 88 ms 23004 KB Output is correct
15 Correct 100 ms 25536 KB Output is correct
16 Correct 94 ms 22972 KB Output is correct
17 Correct 107 ms 25568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13592 KB Output is correct
2 Correct 18 ms 13620 KB Output is correct
3 Correct 49 ms 21844 KB Output is correct
4 Correct 41 ms 20092 KB Output is correct
5 Correct 89 ms 31596 KB Output is correct
6 Correct 79 ms 31564 KB Output is correct
7 Correct 91 ms 31692 KB Output is correct
8 Correct 84 ms 31528 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 724 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 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 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 724 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 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 340 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 54 ms 7888 KB Output is correct
18 Correct 51 ms 8304 KB Output is correct
19 Correct 44 ms 8240 KB Output is correct
20 Correct 51 ms 8236 KB Output is correct
21 Correct 50 ms 8232 KB Output is correct
22 Correct 96 ms 16060 KB Output is correct
23 Correct 10 ms 1876 KB Output is correct
24 Correct 37 ms 5408 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 9 ms 1748 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 724 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 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 340 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 54 ms 7888 KB Output is correct
18 Correct 51 ms 8304 KB Output is correct
19 Correct 44 ms 8240 KB Output is correct
20 Correct 51 ms 8236 KB Output is correct
21 Correct 50 ms 8232 KB Output is correct
22 Correct 96 ms 16060 KB Output is correct
23 Correct 10 ms 1876 KB Output is correct
24 Correct 37 ms 5408 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 9 ms 1748 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 266 ms 37516 KB Output is correct
29 Correct 353 ms 51808 KB Output is correct
30 Correct 353 ms 51304 KB Output is correct
31 Correct 349 ms 51428 KB Output is correct
32 Correct 372 ms 52556 KB Output is correct
33 Correct 346 ms 51292 KB Output is correct
34 Correct 367 ms 51304 KB Output is correct
35 Correct 130 ms 20616 KB Output is correct
36 Correct 380 ms 52836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13592 KB Output is correct
2 Correct 18 ms 13620 KB Output is correct
3 Correct 49 ms 21844 KB Output is correct
4 Correct 41 ms 20092 KB Output is correct
5 Correct 89 ms 31596 KB Output is correct
6 Correct 79 ms 31564 KB Output is correct
7 Correct 91 ms 31692 KB Output is correct
8 Correct 84 ms 31528 KB Output is correct
9 Correct 111 ms 31556 KB Output is correct
10 Correct 73 ms 24396 KB Output is correct
11 Correct 156 ms 48552 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 14 ms 13524 KB Output is correct
19 Correct 14 ms 13524 KB Output is correct
20 Correct 14 ms 13524 KB Output is correct
21 Correct 16 ms 13532 KB Output is correct
22 Correct 95 ms 31052 KB Output is correct
23 Correct 174 ms 48480 KB Output is correct
24 Correct 159 ms 48468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 20436 KB Output is correct
2 Correct 102 ms 23720 KB Output is correct
3 Correct 16 ms 13548 KB Output is correct
4 Correct 17 ms 13576 KB Output is correct
5 Correct 391 ms 62940 KB Output is correct
6 Correct 293 ms 65476 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 208 ms 33840 KB Output is correct
9 Correct 247 ms 40240 KB Output is correct
10 Correct 71 ms 20420 KB Output is correct
11 Correct 94 ms 23740 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 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 14 ms 13552 KB Output is correct
17 Correct 15 ms 13524 KB Output is correct
18 Correct 99 ms 25612 KB Output is correct
19 Correct 124 ms 30000 KB Output is correct
20 Correct 88 ms 23004 KB Output is correct
21 Correct 100 ms 25536 KB Output is correct
22 Correct 94 ms 22972 KB Output is correct
23 Correct 107 ms 25568 KB Output is correct
24 Correct 16 ms 13592 KB Output is correct
25 Correct 18 ms 13620 KB Output is correct
26 Correct 49 ms 21844 KB Output is correct
27 Correct 41 ms 20092 KB Output is correct
28 Correct 89 ms 31596 KB Output is correct
29 Correct 79 ms 31564 KB Output is correct
30 Correct 91 ms 31692 KB Output is correct
31 Correct 84 ms 31528 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 724 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 468 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 340 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 54 ms 7888 KB Output is correct
49 Correct 51 ms 8304 KB Output is correct
50 Correct 44 ms 8240 KB Output is correct
51 Correct 51 ms 8236 KB Output is correct
52 Correct 50 ms 8232 KB Output is correct
53 Correct 96 ms 16060 KB Output is correct
54 Correct 10 ms 1876 KB Output is correct
55 Correct 37 ms 5408 KB Output is correct
56 Correct 2 ms 468 KB Output is correct
57 Correct 9 ms 1748 KB Output is correct
58 Correct 2 ms 1236 KB Output is correct
59 Correct 266 ms 37516 KB Output is correct
60 Correct 353 ms 51808 KB Output is correct
61 Correct 353 ms 51304 KB Output is correct
62 Correct 349 ms 51428 KB Output is correct
63 Correct 372 ms 52556 KB Output is correct
64 Correct 346 ms 51292 KB Output is correct
65 Correct 367 ms 51304 KB Output is correct
66 Correct 130 ms 20616 KB Output is correct
67 Correct 380 ms 52836 KB Output is correct
68 Correct 111 ms 31556 KB Output is correct
69 Correct 73 ms 24396 KB Output is correct
70 Correct 156 ms 48552 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 1 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 14 ms 13524 KB Output is correct
78 Correct 14 ms 13524 KB Output is correct
79 Correct 14 ms 13524 KB Output is correct
80 Correct 16 ms 13532 KB Output is correct
81 Correct 95 ms 31052 KB Output is correct
82 Correct 174 ms 48480 KB Output is correct
83 Correct 159 ms 48468 KB Output is correct
84 Correct 405 ms 61912 KB Output is correct
85 Correct 498 ms 63160 KB Output is correct
86 Correct 297 ms 63948 KB Output is correct
87 Correct 307 ms 65836 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 326 ms 65832 KB Output is correct
90 Correct 325 ms 65392 KB Output is correct
91 Correct 332 ms 65176 KB Output is correct