Submission #748656

# Submission time Handle Problem Language Result Execution time Memory
748656 2023-05-26T17:09:52 Z QwertyPi Catfish Farm (IOI22_fish) C++17
100 / 100
587 ms 76776 KB
#include "fish.h"
#include <bits/stdc++.h>
 
#define pii pair<int, int>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
using namespace std;
 
const int N_MAX = 1e5 + 11;
const int INC = 0;
const int DEC = 1;
const int ZERO = 2;
 
vector<pair<int, long long>> fish[N_MAX];
int N;
 
void chmax(long long& x, long long y){
	x = max(x, y);
}

long long g(int x, int y){ // [x, x] times [0, y)
	if(x <= 0 || x > N) return 0;
	auto ptr = lower_bound(fish[x].begin(), fish[x].end(), pair<int, long long>{y, -1LL});
	if(ptr == fish[x].begin()) return 0;
	return (--ptr)->se;
}
 
vector<long long> dp[N_MAX][3];
vector<int> sp[N_MAX]; 
int L[N_MAX];

int at(int x, int y){
	if(0 <= y && y < sp[x].size())
		return sp[x][y];
	assert(0 == 1);
}

long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {

	::N = N;
	for(int i = 0; i < M; i++) X[i]++;
	
	for(int i = 0; i < M; i++){
		fish[X[i]].push_back({Y[i], W[i]});
	}
	for(int i = 1; i <= N; i++){
		sort(fish[i].begin(), fish[i].end());
		for(int j = 1; j < fish[i].size(); j++){
			fish[i][j].se += fish[i][j - 1].se;
		}
	}
	
	for(int i = 0; i < M; i++){
		int x = X[i], y = Y[i];
		if(x > 1) sp[x - 1].push_back(y + 1);
		if(x > 2) sp[x - 2].push_back(y + 1); 
		if(x < N) sp[x + 1].push_back(y + 1);
		if(x < N - 1) sp[x + 2].push_back(y + 1); 
	}
	
	for(int i = 0; i <= N; i++){
		sp[i].push_back(0);
		sort(sp[i].begin(), sp[i].end());
		sp[i].resize(unique(sp[i].begin(), sp[i].end()) - sp[i].begin());
		for(int j = 0; j < 3; j++){
			dp[i][j].resize(sp[i].size());
			fill(all(dp[i][j]), -1E18);
		}
		L[i] = sp[i].size();
	}
	
	dp[0][ZERO][0] = 0;
	
	for(int x = 1; x <= N; x++){
	
		/*
			for(int y = 0; y <= N; y++){
				chmax(dp[ZERO][x + 1][y], dp[INC][x][y]);
				chmax(dp[ZERO][x + 1][y], dp[DEC][x][y]);
				chmax(dp[ZERO][x + 1][0], dp[ZERO][x][y]);
			}
			for(int y = 0; y < N; y++){
				chmax(dp[ZERO][x + 1][y + 1], dp[ZERO][x + 1][y]);
			}
		*/	
		
		// ZERO
		{
			for(int y = 0; y < L[x - 1]; y++){
				chmax(dp[x][ZERO][0], dp[x - 1][ZERO][y]);
			}
			int y1 = 0;
			for(int y2 = 0; y2 < L[x]; y2++){
				while (y1 < L[x - 1] && at(x - 1, y1) <= at(x, y2)) {
					chmax(dp[x][ZERO][y2], dp[x - 1][INC][y1]);
					chmax(dp[x][ZERO][y2], dp[x - 1][DEC][y1]);
					y1++;
				}
			}
			for(int y = 0; y < L[x] - 1; y++){
				chmax(dp[x][ZERO][y + 1], dp[x][ZERO][y]);
			}
		}
		
		long long value = -(1LL << 60);
		
		/*
		
		for(int y2 = 0; y2 <= N; y2++){
			value = max(value, dp[INC][x][y2] - g(x, y2) - g(x - 1, y2));
			chmax(dp[INC][x + 1][y2], value + g(x + 1, y2) + g(x - 1, y2));
		}
		
		value = -(1LL << 60);
		for(int y2 = 0; y2 <= N; y2++){
			chmax(value, dp[ZERO][x][y2] - g(x - 1, y2));
			chmax(dp[INC][x + 1][y2], dp[ZERO][x][y2] + g(x + 1, y2));
			chmax(dp[INC][x + 1][y2], value + g(x + 1, y2) + g(x - 1, y2));
		}
		
		*/
		
		// INC
		{
			int y1 = 0;
			for(int y2 = 0; y2 < L[x]; y2++){
				while (y1 < L[x - 1] && at(x - 1, y1) <= at(x, y2)) {
					chmax(value, dp[x - 1][INC][y1] - g(x, at(x - 1, y1)) - g(x - 1, at(x - 1, y1)));
					y1++;
				}
				chmax(dp[x][INC][y2], value + g(x + 1, at(x, y2)) + g(x - 1, at(x, y2)));
			}
			
			value = -(1LL << 60);
			y1 = 0;
			for(int y2 = 0; y2 < L[x]; y2++){
				while (y1 < L[x - 1] && at(x - 1, y1) <= at(x, y2)) {
					chmax(value, dp[x - 1][ZERO][y1] - g(x - 1, at(x - 1, y1)));
					y1++;
				}
				chmax(dp[x][INC][y2], dp[x - 1][ZERO][L[x - 1] - 1] + g(x + 1, at(x, y2)));
				chmax(dp[x][INC][y2], value + g(x + 1, at(x, y2)) + g(x - 1, at(x, y2)));
			}
		}
		
		/*
		
		for(int y2 = N; y2 >= 0; y2--){
			chmax(value, dp[DEC][x][y2]); chmax(value, dp[INC][x][y2]);
			chmax(dp[DEC][x + 1][y2], value + g(x + 1, y2) - g(x, y2));
		}
		
		*/
		
		// DEC
		{
			value = -(1LL << 60);
			int y1 = L[x - 1] - 1;
			for(int y2 = L[x] - 1; y2 >= 0; y2--){
				while (y1 >= 0 && at(x - 1, y1) >= at(x, y2)) {
					chmax(value, dp[x - 1][DEC][y1]); chmax(value, dp[x - 1][INC][y1]);
					y1--;
				}
				chmax(dp[x][DEC][y2], value + g(x + 1, at(x, y2)) - g(x, at(x, y2)));
			}
		}
	}
	
	long long ans = 0;
	/*
	for(int i = 0; i <= N; i++){
		for(int k = 0; k < 3; k++){
			for(int j = 0; j < L[i]; j++){
				cout << dp[i][k][j] << ' ';
			}
			cout << endl;
		}
		cout << endl;
	}
	*/
	for(int k = 0; k < 3; k++){
		for(int y = 0; y < L[N]; y++){
			ans = max(ans, dp[N][k][y]);
		}
	}
	return ans;
}

Compilation message

fish.cpp: In function 'int at(int, int)':
fish.cpp:34:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  if(0 <= y && y < sp[x].size())
      |               ~~^~~~~~~~~~~~~~
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:49:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   for(int j = 1; j < fish[i].size(); j++){
      |                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 86 ms 31420 KB Output is correct
2 Correct 104 ms 34588 KB Output is correct
3 Correct 43 ms 24908 KB Output is correct
4 Correct 33 ms 24916 KB Output is correct
5 Correct 365 ms 70280 KB Output is correct
6 Correct 476 ms 70060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 199 ms 39720 KB Output is correct
3 Correct 226 ms 44328 KB Output is correct
4 Correct 93 ms 31428 KB Output is correct
5 Correct 103 ms 34396 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 7 ms 12052 KB Output is correct
8 Correct 6 ms 12112 KB Output is correct
9 Correct 7 ms 11932 KB Output is correct
10 Correct 32 ms 24916 KB Output is correct
11 Correct 33 ms 24916 KB Output is correct
12 Correct 127 ms 33800 KB Output is correct
13 Correct 131 ms 37144 KB Output is correct
14 Correct 111 ms 32576 KB Output is correct
15 Correct 137 ms 33832 KB Output is correct
16 Correct 120 ms 32564 KB Output is correct
17 Correct 119 ms 34796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 24916 KB Output is correct
2 Correct 34 ms 24908 KB Output is correct
3 Correct 69 ms 27244 KB Output is correct
4 Correct 65 ms 27380 KB Output is correct
5 Correct 126 ms 31904 KB Output is correct
6 Correct 106 ms 31948 KB Output is correct
7 Correct 108 ms 32000 KB Output is correct
8 Correct 113 ms 31888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 7 ms 12016 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 8 ms 12204 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 7 ms 12016 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 8 ms 12204 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
15 Correct 7 ms 12116 KB Output is correct
16 Correct 9 ms 12244 KB Output is correct
17 Correct 57 ms 16364 KB Output is correct
18 Correct 53 ms 16984 KB Output is correct
19 Correct 48 ms 17060 KB Output is correct
20 Correct 50 ms 16676 KB Output is correct
21 Correct 54 ms 16588 KB Output is correct
22 Correct 87 ms 21276 KB Output is correct
23 Correct 24 ms 13448 KB Output is correct
24 Correct 50 ms 16216 KB Output is correct
25 Correct 9 ms 12244 KB Output is correct
26 Correct 23 ms 13352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 8 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 7 ms 12016 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 8 ms 12204 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
15 Correct 7 ms 12116 KB Output is correct
16 Correct 9 ms 12244 KB Output is correct
17 Correct 57 ms 16364 KB Output is correct
18 Correct 53 ms 16984 KB Output is correct
19 Correct 48 ms 17060 KB Output is correct
20 Correct 50 ms 16676 KB Output is correct
21 Correct 54 ms 16588 KB Output is correct
22 Correct 87 ms 21276 KB Output is correct
23 Correct 24 ms 13448 KB Output is correct
24 Correct 50 ms 16216 KB Output is correct
25 Correct 9 ms 12244 KB Output is correct
26 Correct 23 ms 13352 KB Output is correct
27 Correct 12 ms 12848 KB Output is correct
28 Correct 256 ms 38084 KB Output is correct
29 Correct 414 ms 52788 KB Output is correct
30 Correct 501 ms 64076 KB Output is correct
31 Correct 527 ms 63464 KB Output is correct
32 Correct 301 ms 47248 KB Output is correct
33 Correct 550 ms 64924 KB Output is correct
34 Correct 545 ms 64848 KB Output is correct
35 Correct 163 ms 29788 KB Output is correct
36 Correct 506 ms 61736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 24916 KB Output is correct
2 Correct 34 ms 24908 KB Output is correct
3 Correct 69 ms 27244 KB Output is correct
4 Correct 65 ms 27380 KB Output is correct
5 Correct 126 ms 31904 KB Output is correct
6 Correct 106 ms 31948 KB Output is correct
7 Correct 108 ms 32000 KB Output is correct
8 Correct 113 ms 31888 KB Output is correct
9 Correct 147 ms 37492 KB Output is correct
10 Correct 92 ms 26060 KB Output is correct
11 Correct 202 ms 39996 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 7 ms 11968 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 12048 KB Output is correct
18 Correct 33 ms 24908 KB Output is correct
19 Correct 35 ms 24920 KB Output is correct
20 Correct 34 ms 24908 KB Output is correct
21 Correct 43 ms 24820 KB Output is correct
22 Correct 177 ms 36888 KB Output is correct
23 Correct 228 ms 50856 KB Output is correct
24 Correct 232 ms 54376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 31420 KB Output is correct
2 Correct 104 ms 34588 KB Output is correct
3 Correct 43 ms 24908 KB Output is correct
4 Correct 33 ms 24916 KB Output is correct
5 Correct 365 ms 70280 KB Output is correct
6 Correct 476 ms 70060 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 199 ms 39720 KB Output is correct
9 Correct 226 ms 44328 KB Output is correct
10 Correct 93 ms 31428 KB Output is correct
11 Correct 103 ms 34396 KB Output is correct
12 Correct 7 ms 11988 KB Output is correct
13 Correct 7 ms 12052 KB Output is correct
14 Correct 6 ms 12112 KB Output is correct
15 Correct 7 ms 11932 KB Output is correct
16 Correct 32 ms 24916 KB Output is correct
17 Correct 33 ms 24916 KB Output is correct
18 Correct 127 ms 33800 KB Output is correct
19 Correct 131 ms 37144 KB Output is correct
20 Correct 111 ms 32576 KB Output is correct
21 Correct 137 ms 33832 KB Output is correct
22 Correct 120 ms 32564 KB Output is correct
23 Correct 119 ms 34796 KB Output is correct
24 Correct 33 ms 24916 KB Output is correct
25 Correct 34 ms 24908 KB Output is correct
26 Correct 69 ms 27244 KB Output is correct
27 Correct 65 ms 27380 KB Output is correct
28 Correct 126 ms 31904 KB Output is correct
29 Correct 106 ms 31948 KB Output is correct
30 Correct 108 ms 32000 KB Output is correct
31 Correct 113 ms 31888 KB Output is correct
32 Correct 7 ms 11988 KB Output is correct
33 Correct 8 ms 11988 KB Output is correct
34 Correct 7 ms 11988 KB Output is correct
35 Correct 7 ms 11988 KB Output is correct
36 Correct 6 ms 11988 KB Output is correct
37 Correct 7 ms 11988 KB Output is correct
38 Correct 6 ms 11988 KB Output is correct
39 Correct 7 ms 12016 KB Output is correct
40 Correct 7 ms 12116 KB Output is correct
41 Correct 8 ms 12204 KB Output is correct
42 Correct 7 ms 12116 KB Output is correct
43 Correct 8 ms 12116 KB Output is correct
44 Correct 7 ms 11988 KB Output is correct
45 Correct 8 ms 12060 KB Output is correct
46 Correct 7 ms 12116 KB Output is correct
47 Correct 9 ms 12244 KB Output is correct
48 Correct 57 ms 16364 KB Output is correct
49 Correct 53 ms 16984 KB Output is correct
50 Correct 48 ms 17060 KB Output is correct
51 Correct 50 ms 16676 KB Output is correct
52 Correct 54 ms 16588 KB Output is correct
53 Correct 87 ms 21276 KB Output is correct
54 Correct 24 ms 13448 KB Output is correct
55 Correct 50 ms 16216 KB Output is correct
56 Correct 9 ms 12244 KB Output is correct
57 Correct 23 ms 13352 KB Output is correct
58 Correct 12 ms 12848 KB Output is correct
59 Correct 256 ms 38084 KB Output is correct
60 Correct 414 ms 52788 KB Output is correct
61 Correct 501 ms 64076 KB Output is correct
62 Correct 527 ms 63464 KB Output is correct
63 Correct 301 ms 47248 KB Output is correct
64 Correct 550 ms 64924 KB Output is correct
65 Correct 545 ms 64848 KB Output is correct
66 Correct 163 ms 29788 KB Output is correct
67 Correct 506 ms 61736 KB Output is correct
68 Correct 147 ms 37492 KB Output is correct
69 Correct 92 ms 26060 KB Output is correct
70 Correct 202 ms 39996 KB Output is correct
71 Correct 6 ms 11988 KB Output is correct
72 Correct 6 ms 11988 KB Output is correct
73 Correct 6 ms 11988 KB Output is correct
74 Correct 7 ms 11968 KB Output is correct
75 Correct 6 ms 11988 KB Output is correct
76 Correct 7 ms 12048 KB Output is correct
77 Correct 33 ms 24908 KB Output is correct
78 Correct 35 ms 24920 KB Output is correct
79 Correct 34 ms 24908 KB Output is correct
80 Correct 43 ms 24820 KB Output is correct
81 Correct 177 ms 36888 KB Output is correct
82 Correct 228 ms 50856 KB Output is correct
83 Correct 232 ms 54376 KB Output is correct
84 Correct 587 ms 72024 KB Output is correct
85 Correct 554 ms 72444 KB Output is correct
86 Correct 516 ms 75420 KB Output is correct
87 Correct 510 ms 76636 KB Output is correct
88 Correct 7 ms 11988 KB Output is correct
89 Correct 562 ms 76776 KB Output is correct
90 Correct 443 ms 67276 KB Output is correct
91 Correct 317 ms 59200 KB Output is correct