Submission #797804

# Submission time Handle Problem Language Result Execution time Memory
797804 2023-07-29T23:47:46 Z hugo_pm Catfish Farm (IOI22_fish) C++17
100 / 100
232 ms 41756 KB
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
	
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
	
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
	
using ll = long long;
using v64 = vector<ll>;
using a2v64 = array<v64, 2>;
using p64 = pair<ll, ll>;
	
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}
	
template <typename A, typename B>
string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
	
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}
	
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const ll INF = 3e18;
const int INCR = 0, DECR = 1;
ll max_weights(int szCarre, int totFish, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
	++szCarre;
	vector<vector<p64>> fish(szCarre);
	vector<vector<int>> niceTake(szCarre);
	rep(i, 0, totFish) {
		++X[i];
		fish[X[i]].emplace_back(Y[i], W[i]);
		// Y+1 pour couvrir Y
		niceTake[X[i]-1].push_back(Y[i] + 1);
		if (X[i]+1 < szCarre) {
			niceTake[X[i]+1].push_back(Y[i] + 1);
		}
	}
	v64 nbNice(szCarre), nbFish(szCarre);
	vector<v64> prefSum(szCarre);
	rep(col, 0, szCarre) {
		auto &nice = niceTake[col];
		nice.push_back(0);
		sort(all(nice));
		nice.resize(unique(all(nice)) - begin(nice));
		nbNice[col] = sz(nice);
		sort(all(fish[col]));
		nbFish[col] = sz(fish[col]);
		auto &ss = prefSum[col];
		ss.push_back(0);
		for (auto [_, w] : fish[col]) {
			ss.push_back(ss.back() + w);
		}
	}
	a2v64 dpOld;
	dpOld[0] = dpOld[1] = v64(nbNice[0], -INF);
	dpOld[INCR][0] = dpOld[DECR][0] = 0;
	rep(col, 1, szCarre) {
		a2v64 dpNew;
		dpNew[0] = dpNew[1] = v64(nbNice[col], -INF);
		dpNew[INCR][0] = *max_element(all(dpOld[DECR]));
		if (true) {
			ll maxTr = -INF;
			int ptrOld = 0, ptrOldFish = 0, ptrNewFish = 0;
			/// retourne la somme des poissons < upTo dans col-1
			auto sfIncr = [&] (int &ptr, int upTo) {
				while (ptr < nbFish[col-1] && fish[col-1][ptr].first < upTo) {
					++ptr;
				}
				return prefSum[col-1][ptr];
			};
			rep(ptrNew, 0, nbNice[col]) {
				int newTake = niceTake[col][ptrNew];
				while (ptrOld < nbNice[col-1] && niceTake[col-1][ptrOld] <= newTake) {
					int oldTake = niceTake[col-1][ptrOld];
					chmax(maxTr, dpOld[INCR][ptrOld] - sfIncr(ptrOldFish, oldTake));
					++ptrOld;
				}
				chmax(dpNew[INCR][ptrNew], maxTr + sfIncr(ptrNewFish, newTake));
			}
		}
		if (true) {
			ll maxTr = -INF;
			int ptrOld = nbNice[col-1]-1, ptrOldFish = nbFish[col], ptrNewFish = nbFish[col];
			/// retourne la somme des poissons < upTo dans col
			auto sfDecr = [&] (int &ptr, int upTo) {
				while (ptr > 0 && fish[col][ptr-1].first >= upTo) {
					--ptr;
				}
				return prefSum[col][ptr];
			};
			for (int ptrNew = nbNice[col]-1; ptrNew >= 0; --ptrNew) {
				int newTake = niceTake[col][ptrNew];
				while (ptrOld >= 0 && niceTake[col-1][ptrOld] >= newTake) {
					int oldTake = niceTake[col-1][ptrOld];
					chmax(maxTr, dpOld[DECR][ptrOld] + sfDecr(ptrOldFish, oldTake));
					--ptrOld;
				}
				chmax(dpNew[DECR][ptrNew], maxTr - sfDecr(ptrNewFish, newTake));
			}
		}
		rep(take, 0, nbNice[col]) {
			chmax(dpNew[DECR][take], dpNew[INCR][take]);
		}
		dpOld = dpNew;
	}
	return *max_element(all(dpOld[DECR]));
}
# Verdict Execution time Memory Grader output
1 Correct 54 ms 22016 KB Output is correct
2 Correct 69 ms 25536 KB Output is correct
3 Correct 20 ms 15188 KB Output is correct
4 Correct 19 ms 15060 KB Output is correct
5 Correct 157 ms 41512 KB Output is correct
6 Correct 190 ms 41756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 95 ms 28148 KB Output is correct
3 Correct 112 ms 32988 KB Output is correct
4 Correct 54 ms 22024 KB Output is correct
5 Correct 65 ms 25520 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 296 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 20 ms 15148 KB Output is correct
11 Correct 20 ms 15060 KB Output is correct
12 Correct 64 ms 22096 KB Output is correct
13 Correct 67 ms 25516 KB Output is correct
14 Correct 56 ms 20864 KB Output is correct
15 Correct 62 ms 23924 KB Output is correct
16 Correct 54 ms 20960 KB Output is correct
17 Correct 62 ms 23132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 15188 KB Output is correct
2 Correct 20 ms 15172 KB Output is correct
3 Correct 43 ms 17488 KB Output is correct
4 Correct 37 ms 17740 KB Output is correct
5 Correct 74 ms 22284 KB Output is correct
6 Correct 70 ms 21580 KB Output is correct
7 Correct 72 ms 22136 KB Output is correct
8 Correct 76 ms 22224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 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 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 480 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 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 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 480 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 19 ms 3680 KB Output is correct
18 Correct 18 ms 4768 KB Output is correct
19 Correct 17 ms 4520 KB Output is correct
20 Correct 18 ms 4544 KB Output is correct
21 Correct 17 ms 4436 KB Output is correct
22 Correct 35 ms 7980 KB Output is correct
23 Correct 5 ms 1108 KB Output is correct
24 Correct 13 ms 2772 KB Output is correct
25 Correct 1 ms 440 KB Output is correct
26 Correct 4 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 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 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 480 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 19 ms 3680 KB Output is correct
18 Correct 18 ms 4768 KB Output is correct
19 Correct 17 ms 4520 KB Output is correct
20 Correct 18 ms 4544 KB Output is correct
21 Correct 17 ms 4436 KB Output is correct
22 Correct 35 ms 7980 KB Output is correct
23 Correct 5 ms 1108 KB Output is correct
24 Correct 13 ms 2772 KB Output is correct
25 Correct 1 ms 440 KB Output is correct
26 Correct 4 ms 980 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 88 ms 20468 KB Output is correct
29 Correct 127 ms 26580 KB Output is correct
30 Correct 138 ms 25692 KB Output is correct
31 Correct 135 ms 25688 KB Output is correct
32 Correct 128 ms 29016 KB Output is correct
33 Correct 135 ms 25780 KB Output is correct
34 Correct 136 ms 25696 KB Output is correct
35 Correct 49 ms 10972 KB Output is correct
36 Correct 132 ms 28364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 15188 KB Output is correct
2 Correct 20 ms 15172 KB Output is correct
3 Correct 43 ms 17488 KB Output is correct
4 Correct 37 ms 17740 KB Output is correct
5 Correct 74 ms 22284 KB Output is correct
6 Correct 70 ms 21580 KB Output is correct
7 Correct 72 ms 22136 KB Output is correct
8 Correct 76 ms 22224 KB Output is correct
9 Correct 84 ms 22072 KB Output is correct
10 Correct 55 ms 16384 KB Output is correct
11 Correct 128 ms 32464 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 292 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 19 ms 15148 KB Output is correct
19 Correct 20 ms 15060 KB Output is correct
20 Correct 19 ms 15164 KB Output is correct
21 Correct 19 ms 15192 KB Output is correct
22 Correct 80 ms 22892 KB Output is correct
23 Correct 137 ms 32476 KB Output is correct
24 Correct 140 ms 32980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 22016 KB Output is correct
2 Correct 69 ms 25536 KB Output is correct
3 Correct 20 ms 15188 KB Output is correct
4 Correct 19 ms 15060 KB Output is correct
5 Correct 157 ms 41512 KB Output is correct
6 Correct 190 ms 41756 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 95 ms 28148 KB Output is correct
9 Correct 112 ms 32988 KB Output is correct
10 Correct 54 ms 22024 KB Output is correct
11 Correct 65 ms 25520 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 296 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 20 ms 15148 KB Output is correct
17 Correct 20 ms 15060 KB Output is correct
18 Correct 64 ms 22096 KB Output is correct
19 Correct 67 ms 25516 KB Output is correct
20 Correct 56 ms 20864 KB Output is correct
21 Correct 62 ms 23924 KB Output is correct
22 Correct 54 ms 20960 KB Output is correct
23 Correct 62 ms 23132 KB Output is correct
24 Correct 20 ms 15188 KB Output is correct
25 Correct 20 ms 15172 KB Output is correct
26 Correct 43 ms 17488 KB Output is correct
27 Correct 37 ms 17740 KB Output is correct
28 Correct 74 ms 22284 KB Output is correct
29 Correct 70 ms 21580 KB Output is correct
30 Correct 72 ms 22136 KB Output is correct
31 Correct 76 ms 22224 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 280 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 300 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 480 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 312 KB Output is correct
46 Correct 1 ms 300 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 19 ms 3680 KB Output is correct
49 Correct 18 ms 4768 KB Output is correct
50 Correct 17 ms 4520 KB Output is correct
51 Correct 18 ms 4544 KB Output is correct
52 Correct 17 ms 4436 KB Output is correct
53 Correct 35 ms 7980 KB Output is correct
54 Correct 5 ms 1108 KB Output is correct
55 Correct 13 ms 2772 KB Output is correct
56 Correct 1 ms 440 KB Output is correct
57 Correct 4 ms 980 KB Output is correct
58 Correct 2 ms 852 KB Output is correct
59 Correct 88 ms 20468 KB Output is correct
60 Correct 127 ms 26580 KB Output is correct
61 Correct 138 ms 25692 KB Output is correct
62 Correct 135 ms 25688 KB Output is correct
63 Correct 128 ms 29016 KB Output is correct
64 Correct 135 ms 25780 KB Output is correct
65 Correct 136 ms 25696 KB Output is correct
66 Correct 49 ms 10972 KB Output is correct
67 Correct 132 ms 28364 KB Output is correct
68 Correct 84 ms 22072 KB Output is correct
69 Correct 55 ms 16384 KB Output is correct
70 Correct 128 ms 32464 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 1 ms 292 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 19 ms 15148 KB Output is correct
78 Correct 20 ms 15060 KB Output is correct
79 Correct 19 ms 15164 KB Output is correct
80 Correct 19 ms 15192 KB Output is correct
81 Correct 80 ms 22892 KB Output is correct
82 Correct 137 ms 32476 KB Output is correct
83 Correct 140 ms 32980 KB Output is correct
84 Correct 166 ms 38744 KB Output is correct
85 Correct 167 ms 39604 KB Output is correct
86 Correct 215 ms 39572 KB Output is correct
87 Correct 232 ms 41040 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 218 ms 41040 KB Output is correct
90 Correct 180 ms 40284 KB Output is correct
91 Correct 151 ms 40524 KB Output is correct