# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1161118 | jj_master | Catfish Farm (IOI22_fish) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int64_t max_weights(int N, int M, vector<int>& X, vector<int>& Y, vector<int>& W) {
// Group catfish by their columns
map<int, vector<pair<int, int>>> catfish_in_column;
for (int i = 0; i < M; ++i) {
catfish_in_column[X[i]].push_back({Y[i], W[i]});
}
// Sort the catfish in each column by their row position (Y[i])
for (auto& entry : catfish_in_column) {
sort(entry.second.begin(), entry.second.end());
}
// DP array to store maximum weight of catfish caught up to each column
vector<int64_t> dp(N, 0);
// Process each column
for (int col = 0; col < N; ++col) {
// Max weight up to column `col` without any pier in this column
if (col > 0) {
dp[col] = dp[col - 1];
}
// Catfish caught in the current column if we build a pier here
int64_t catfish_caught = 0;
for (auto& fish : catfish_in_column[col]) {
int row = fish.first;
int weight = fish.second;
// Check if catfish is eligible to be caught
bool can_be_caught = false;
if (col > 0) {
// Check if there is no catfish directly in column `col-1` at the same row
bool caught_from_left = true;
for (auto& left_fish : catfish_in_column[col - 1]) {
if (left_fish.first == row) {
caught_from_left = false;
break;
}
}
if (caught_from_left) can_be_caught = true;
}
if (col < N - 1) {
// Check if there is no catfish directly in column `col+1` at the same row
bool caught_from_right = true;
for (auto& right_fish : catfish_in_column[col + 1]) {
if (right_fish.first == row) {
caught_from_right = false;
break;
}
}
if (caught_from_right) can_be_caught = true;
}
if (can_be_caught) {
catfish_caught += weight;
}
}
// Add the catfish caught in this column if we build a pier here
if (col > 0) {
dp[col] = max(dp[col], dp[col - 1] + catfish_caught);
} else {
dp[col] = max(dp[col], catfish_caught);
}
}
// The result is the maximum weight that can be caught up to the last column
return dp[N - 1];
}
int main() {
// Input
int N, M;
cin >> N >> M;
vector<int> X(M), Y(M), W(M);
for (int i = 0; i < M; ++i) {
cin >> X[i] >> Y[i] >> W[i];
}
// Calculate and output the maximum weight of catfish that can be caught
cout << max_weights(N, M, X, Y, W) << endl;
return 0;
}