제출 #703200

#제출 시각아이디문제언어결과실행 시간메모리
703200n1kCatfish Farm (IOI22_fish)C++17
52 / 100
867 ms2097152 KiB
#include <bits/stdc++.h>

#define ll long long
#define vt vector
#define pb push_back
#define ar array
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()

using namespace std;

/*
 ## TAKE IT EASY ##
 1. simplify
 2. add new elements
 3. brute force solution
 4. optimize
 5. start implementing
*/

// --- templates ---

// --- code ---

const int N = 3000 + 10, FLOW = 3;

ll dp[N][FLOW][N];

ll max_weights(int n, int m, vt<int> X, vt<int> Y, vt<int> W){
	memset(dp, 0, sizeof dp);

	int h = n + 5;

	vt<vt<ll>> a(n + 5, vt<ll>(h));

	for(int i = 0; i < m; i++){
		a[X[i]][Y[i] + 1] = W[i];
	}

	for(int i = 0; i < n; i++){
		for(int j = 1; j < h; j++){
			a[i][j] += a[i][j - 1];
		}
	}

	for(int i = 0; i < n; i++){
		for(int flow = 0; flow < FLOW; flow++){
			vt<ll> sufmax(h + 5), prefmax(h + 5);
			if(i){
				for(int h1 = h; h1 >= 0; h1--){
					sufmax[h1] = max(sufmax[h1 + 1], dp[i - 1][flow][h1]);
				}
				prefmax[0] = dp[i - 1][flow][0];
				for(int h1 = 1; h1 < h; h1++){
					prefmax[h1] = max(prefmax[h1 - 1], dp[i - 1][flow][h1] - a[i - 1][h1] - (flow == 2 ? 0 : a[i][h1]));
				}
			}

			for(int h2 = 0; h2 < h; h2++){
				if(i == 0){
					dp[i][1][h2] = max(dp[i][1][h2], a[i + 1][h2]);
				}else{
					if(flow == 0){
						// h1 > h2
						dp[i][0][h2] = max(dp[i][0][h2], sufmax[h2 + 1] + a[i + 1][h2] - a[i][h2]);
						// h1 == h2
						dp[i][1][h2] = max(dp[i][1][h2], dp[i - 1][flow][h2] + a[i + 1][h2] - a[i][h2]);
					}else if(flow == 1){
						// h1 > h2
						dp[i][0][h2] = max(dp[i][0][h2], sufmax[h2 + 1] + a[i + 1][h2] - a[i][h2]);
						// h1 <= h2
						dp[i][1][h2] = max(dp[i][1][h2], prefmax[h2] + a[i + 1][h2] + a[i - 1][h2]);
					}else{
						// h1 > h2
						dp[i][1][h2] = max(dp[i][1][h2], sufmax[h2 + 1] + a[i + 1][h2]);
						// h1 <= h2
						dp[i][1][h2] = max(dp[i][1][h2], prefmax[h2] + a[i + 1][h2] + a[i - 1][h2]);
					}
				}
				if(i)
					dp[i][2][h2] = max(dp[i - 1][0][h2], dp[i - 1][1][h2]);
			}
		}
	}

	ll ans = 0;

	for(int flow = 0; flow < FLOW; flow++){
		for(int h1 = 0; h1 < h; h1++){
			ans = max(ans, dp[n - 1][flow][h1]);
		}
	}

	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...