답안 #823886

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
823886 2023-08-13T09:40:28 Z esomer 메기 농장 (IOI22_fish) C++17
100 / 100
228 ms 56336 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int try_next(int i, vector<pair<int, int>>& v){
	if(i >= (int)v.size()) return 1e9;
	else return v[i].first;
}

ll precalcgreater(vector<vector<ll>>& dp, vector<vector<int>>& which, vector<vector<pair<int, int>>>& fish, int i){
	int ind = 0;
	ll mx = 0;
	ll add = 0;
	for(int j = 0; j < (int)dp[i-1].size(); j++){
		int y = which[i-1][j];
		while(ind < (int)fish[i].size() && fish[i][ind].first <= y){
			add += fish[i][ind].second;
			ind++;
		}
		mx = max(mx, dp[i-1][j] + add);
	}
	return mx;
}

ll precalcsmaller(vector<vector<ll>>& dpmine, vector<vector<int>>& which, vector<vector<pair<int, int>>>& fish, int i){
	int ind = (int)fish[i-1].size() - 1;
	ll mx = 0;
	ll add = 0;
	for(int j = (int)dpmine[i-1].size() - 1; j >= 0; j--){
		int y = which[i-1][j];
		while(ind >= 0 && fish[i-1][ind].first > y){
			add += fish[i-1][ind].second;
			ind--;
		}
		mx = max(mx, dpmine[i-1][j] + add);
	}
	return mx;
}

ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W){
	vector<vector<pair<int, int>>> fish(N);
	vector<vector<ll>> dp(N); //DP stores the maximum ans if I build a pier up to some state.
							//It is always optimal that this pier is in one of the catfishes of adjacent.
	vector<vector<ll>> dpmine(N); //DPmine stores the maximum if I take my catfishes up to some state.
	vector<vector<int>> which(N);
	for(int i = 0; i < M; i++){
		fish[X[i]].push_back({Y[i], W[i]});
	}
	for(int i = 0; i < N; i++){
		sort(fish[i].begin(), fish[i].end());
	}
	for(int i = 0; i < N; i++){
		int ind1 = 0;
		int ind2 = 0;
		int ind3 = 0;
		which[i].push_back(-1);
		dp[i].push_back(0);
		vector<pair<int, int>> prev, next, act;
		if(i > 0) prev = fish[i-1];
		if(i < N - 1) next = fish[i+1];
		act = fish[i];
		while(ind1 < (int)prev.size() || ind2 < (int)next.size() || ind3 < (int)act.size()){
			int mn1 = try_next(ind1, prev);
			int mn2 = try_next(ind2, next);
			int mn3 = try_next(ind3, act);
			if(mn1 <= mn2 && mn1 <= mn3){
				dp[i].push_back(0);
				which[i].push_back(prev[ind1].first);
				ind1++;
			}else if(mn2 <= mn1 && mn2 <= mn3){
				dp[i].push_back(0);
				which[i].push_back(next[ind2].first);
				ind2++;
			}else{
				dp[i].push_back(0);
				which[i].push_back(act[ind3].first);
				ind3++;
			}
		}
	}
	dpmine = dp;
	for(int i = 1; i < N; i++){
		int ind = 0;
		ll subs = 0;
		ll mx = precalcgreater(dp, which, fish, i); //The initial value for each of the DP of the last one. In order.
		for(int j = 0; j < (int)dp[i].size(); j++){
			int y = which[i][j];
			while(ind < (int)fish[i].size() && fish[i][ind].first <= y){
				subs += fish[i][ind].second;
				ind++;
			}
			dp[i][j] = max(dp[i][j], mx - subs);
		}
		ind = (int)fish[i-1].size() - 1;
		subs = 0;
		mx = precalcsmaller(dpmine, which, fish, i);
		for(int j = (int)dp[i].size() - 1; j >= 0; j--){
			int y = which[i][j];
			while(ind >= 0 && fish[i-1][ind].first > y){
				subs += fish[i-1][ind].second;
				ind--;
			}
			dpmine[i][j] = max(dpmine[i][j], mx - subs);
			dp[i][j] = max(dp[i][j], mx - subs);
		}
		vector<ll> prefix((int)dp[i].size(), 0);
		ind = 0;
		subs = 0;
		for(int j = 0; j < (int)dp[i].size(); j++){
			int y = which[i][j];
			while(ind < (int)fish[i].size() && fish[i][ind].first <= y){
				subs += fish[i][ind].second;
				ind++;
			}
			prefix[j] = subs;
		}
		mx = -1e18;
		ind = (int)dp[i-1].size() - 1;
		for(int j = (int)dp[i].size() - 1; j >= 0; j--){
			int y = which[i][j];
			while(ind >= 0 && which[i-1][ind] >= y){
				mx = max(mx, dp[i-1][ind]);
				ind--;
			}
			dpmine[i][j] = max(dpmine[i][j], mx + prefix[j]);
		}
	}
	ll ans = 0;
	for(ll x : dp[N - 1]) ans = max(ans, x);
	for(ll x : dpmine[N - 1]) ans = max(ans, x);
	return ans;
}

// int main() {
//   int N, M;
//   assert(2 == scanf("%d %d", &N, &M));

//   std::vector<int> X(M), Y(M), W(M);
//   for (int i = 0; i < M; ++i) {
//     assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
//   }

//   long long result = max_weights(N, M, X, Y, W);
//   printf("%lld\n", result);
//   return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 23876 KB Output is correct
2 Correct 58 ms 28800 KB Output is correct
3 Correct 24 ms 18960 KB Output is correct
4 Correct 24 ms 19052 KB Output is correct
5 Correct 141 ms 54664 KB Output is correct
6 Correct 205 ms 55392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 80 ms 32004 KB Output is correct
3 Correct 98 ms 40468 KB Output is correct
4 Correct 50 ms 25288 KB Output is correct
5 Correct 70 ms 28892 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 212 KB Output is correct
10 Correct 24 ms 18980 KB Output is correct
11 Correct 23 ms 19044 KB Output is correct
12 Correct 52 ms 27128 KB Output is correct
13 Correct 59 ms 30880 KB Output is correct
14 Correct 53 ms 26084 KB Output is correct
15 Correct 62 ms 28848 KB Output is correct
16 Correct 51 ms 26052 KB Output is correct
17 Correct 63 ms 28828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 19092 KB Output is correct
2 Correct 30 ms 19072 KB Output is correct
3 Correct 51 ms 22604 KB Output is correct
4 Correct 48 ms 22600 KB Output is correct
5 Correct 80 ms 29260 KB Output is correct
6 Correct 83 ms 28604 KB Output is correct
7 Correct 85 ms 29224 KB Output is correct
8 Correct 84 ms 29164 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 1 ms 468 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 1 ms 468 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 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 18 ms 5072 KB Output is correct
18 Correct 16 ms 5200 KB Output is correct
19 Correct 15 ms 4872 KB Output is correct
20 Correct 15 ms 4948 KB Output is correct
21 Correct 15 ms 4932 KB Output is correct
22 Correct 30 ms 9428 KB Output is correct
23 Correct 6 ms 1312 KB Output is correct
24 Correct 12 ms 3924 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 4 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 1 ms 468 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 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 18 ms 5072 KB Output is correct
18 Correct 16 ms 5200 KB Output is correct
19 Correct 15 ms 4872 KB Output is correct
20 Correct 15 ms 4948 KB Output is correct
21 Correct 15 ms 4932 KB Output is correct
22 Correct 30 ms 9428 KB Output is correct
23 Correct 6 ms 1312 KB Output is correct
24 Correct 12 ms 3924 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 4 ms 1236 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
28 Correct 86 ms 26148 KB Output is correct
29 Correct 116 ms 36572 KB Output is correct
30 Correct 129 ms 41844 KB Output is correct
31 Correct 130 ms 41832 KB Output is correct
32 Correct 100 ms 37460 KB Output is correct
33 Correct 125 ms 41696 KB Output is correct
34 Correct 130 ms 41828 KB Output is correct
35 Correct 50 ms 15572 KB Output is correct
36 Correct 123 ms 38340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 19092 KB Output is correct
2 Correct 30 ms 19072 KB Output is correct
3 Correct 51 ms 22604 KB Output is correct
4 Correct 48 ms 22600 KB Output is correct
5 Correct 80 ms 29260 KB Output is correct
6 Correct 83 ms 28604 KB Output is correct
7 Correct 85 ms 29224 KB Output is correct
8 Correct 84 ms 29164 KB Output is correct
9 Correct 80 ms 28980 KB Output is correct
10 Correct 60 ms 19936 KB Output is correct
11 Correct 125 ms 39756 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 300 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 296 KB Output is correct
18 Correct 23 ms 19088 KB Output is correct
19 Correct 23 ms 19016 KB Output is correct
20 Correct 23 ms 19028 KB Output is correct
21 Correct 22 ms 18984 KB Output is correct
22 Correct 106 ms 29880 KB Output is correct
23 Correct 121 ms 39760 KB Output is correct
24 Correct 119 ms 40264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 23876 KB Output is correct
2 Correct 58 ms 28800 KB Output is correct
3 Correct 24 ms 18960 KB Output is correct
4 Correct 24 ms 19052 KB Output is correct
5 Correct 141 ms 54664 KB Output is correct
6 Correct 205 ms 55392 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 80 ms 32004 KB Output is correct
9 Correct 98 ms 40468 KB Output is correct
10 Correct 50 ms 25288 KB Output is correct
11 Correct 70 ms 28892 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 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 24 ms 18980 KB Output is correct
17 Correct 23 ms 19044 KB Output is correct
18 Correct 52 ms 27128 KB Output is correct
19 Correct 59 ms 30880 KB Output is correct
20 Correct 53 ms 26084 KB Output is correct
21 Correct 62 ms 28848 KB Output is correct
22 Correct 51 ms 26052 KB Output is correct
23 Correct 63 ms 28828 KB Output is correct
24 Correct 23 ms 19092 KB Output is correct
25 Correct 30 ms 19072 KB Output is correct
26 Correct 51 ms 22604 KB Output is correct
27 Correct 48 ms 22600 KB Output is correct
28 Correct 80 ms 29260 KB Output is correct
29 Correct 83 ms 28604 KB Output is correct
30 Correct 85 ms 29224 KB Output is correct
31 Correct 84 ms 29164 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 0 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 1 ms 468 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 0 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 18 ms 5072 KB Output is correct
49 Correct 16 ms 5200 KB Output is correct
50 Correct 15 ms 4872 KB Output is correct
51 Correct 15 ms 4948 KB Output is correct
52 Correct 15 ms 4932 KB Output is correct
53 Correct 30 ms 9428 KB Output is correct
54 Correct 6 ms 1312 KB Output is correct
55 Correct 12 ms 3924 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 4 ms 1236 KB Output is correct
58 Correct 2 ms 1108 KB Output is correct
59 Correct 86 ms 26148 KB Output is correct
60 Correct 116 ms 36572 KB Output is correct
61 Correct 129 ms 41844 KB Output is correct
62 Correct 130 ms 41832 KB Output is correct
63 Correct 100 ms 37460 KB Output is correct
64 Correct 125 ms 41696 KB Output is correct
65 Correct 130 ms 41828 KB Output is correct
66 Correct 50 ms 15572 KB Output is correct
67 Correct 123 ms 38340 KB Output is correct
68 Correct 80 ms 28980 KB Output is correct
69 Correct 60 ms 19936 KB Output is correct
70 Correct 125 ms 39756 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 300 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 0 ms 296 KB Output is correct
77 Correct 23 ms 19088 KB Output is correct
78 Correct 23 ms 19016 KB Output is correct
79 Correct 23 ms 19028 KB Output is correct
80 Correct 22 ms 18984 KB Output is correct
81 Correct 106 ms 29880 KB Output is correct
82 Correct 121 ms 39760 KB Output is correct
83 Correct 119 ms 40264 KB Output is correct
84 Correct 155 ms 52744 KB Output is correct
85 Correct 145 ms 53652 KB Output is correct
86 Correct 205 ms 54016 KB Output is correct
87 Correct 228 ms 56096 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 214 ms 56068 KB Output is correct
90 Correct 179 ms 56140 KB Output is correct
91 Correct 157 ms 56336 KB Output is correct