제출 #1260944

#제출 시각아이디문제언어결과실행 시간메모리
1260944rayan_bd메기 농장 (IOI22_fish)C++20
0 / 100
621 ms288096 KiB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int MAX = 3010;

static ll dp[MAX][MAX][3];
static ll cost[MAX][MAX]; 

ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    // reset
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            cost[i][j] = 0;
            dp[i][j][0] = dp[i][j][1] = dp[i][j][2] = 0;
        }
    }

    vector<vector<pair<int,int>>> fish(N+1);
    for (int k = 0; k < M; k++) {
        int cx = X[k] + 1;
        int cy = Y[k] + 1;
        fish[cx].push_back({cy, W[k]});
    }
    for (int i = 1; i <= N; i++) {
        sort(fish[i].begin(), fish[i].end());
        int ptr = 0;
        for (int j = 1; j <= N; j++) {
            cost[i][j] = cost[i][j-1];
            while (ptr < (int)fish[i].size() && fish[i][ptr].first == j) {
                cost[i][j] += fish[i][ptr].second;
                ptr++;
            }
        }
    }

    ll ans = 0;

    for (int i = 1; i <= N; i++) {
        if (i >= 2) {
            for (int j = 1; j <= N; j++) {
                for (int k = 1; k <= N; k++) {
                    dp[i][j][2] = max(dp[i][j][2],
                                      dp[i-2][k][0] + cost[i][j] + cost[i-1][k]);
                }
            }
        }

        for (int j = 1; j <= N; j++) {
            for (int k = j; k <= N; k++) {
                dp[i][j][1] = max(dp[i][j][1],
                                  dp[i-1][k][0] + cost[i][j]);
            }
            ans = max(ans, dp[i][j][1]);
        }
  
        auto get = [&](int col, int l, int r) -> ll {
            if (l > r) return 0;
            return cost[col][r] - cost[col][l-1];
        };
        for (int j = 1; j <= N; j++) {
            for (int k = 1; k <= j; k++) {
                dp[i][j][0] = max(dp[i][j][0],
                                  max({ dp[i-1][k][0] + get(i-1, k+1, j),
                                        dp[i-1][j][1],
                                        dp[i-1][j][2] }));
            }
            ans = max(ans, dp[i][j][0]);
        }

        for (int j = N-1; j >= 1; j--) {
            dp[i][j][0] = max(dp[i][j][0], dp[i][j+1][0]);
        }
    }

    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...