답안 #838104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838104 2023-08-26T08:46:22 Z erekle 메기 농장 (IOI22_fish) C++17
100 / 100
237 ms 82892 KB
#include "fish.h"
#include <vector>
#include <algorithm>

using namespace std;

struct Catfish{
    int x, y, w;
    Catfish() {}
    Catfish(int X, int Y, int W): x(X), y(Y), w(W) {}
    bool operator < (const Catfish &c2) {
        return pair<int, int>{x, y} < pair<int, int>{c2.x, c2.y};
    }
};

const long long INF = 1e18;

long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    vector<vector<Catfish>> inCol(1+N);
    for (int i = 0; i < M; ++i) {
        ++X[i], ++Y[i];
        inCol[X[i]].emplace_back(X[i], Y[i], W[i]);
    }
    for (int i = 0; i <= N; ++i) inCol[i].emplace_back(i, 0, 0), sort(inCol[i].begin(), inCol[i].end());

    vector<vector<long long>> sumBelow(1+N);
    for (int i = 0; i <= N; ++i) {
        sumBelow[i].resize(inCol[i].size());
        for (size_t j = 0; j < sumBelow[i].size(); ++j) {
            sumBelow[i][j] = (j == 0 ? 0 : sumBelow[i][j-1]) + inCol[i][j].w;
        }
    }

    vector<vector<int>> heights(1+N);
    for (int i = 1; i <= N; ++i) {
        if (i) {
            for (const Catfish &c : inCol[i-1]) heights[i].push_back(c.y);
        }
        if (i<N) {
            for (const Catfish &c : inCol[i+1]) heights[i].push_back(c.y);
        }
        sort(heights[i].begin(), heights[i].end());
        heights[i].resize(unique(heights[i].begin(), heights[i].end()) - heights[i].begin());        
    }
    heights[0].push_back(0);

    vector<vector<long long>> inside(1+N), insideLeft(1+N), insideRight(1+N);
    for (int i = 0; i <= N; ++i) {
        inside[i] = insideLeft[i] = insideRight[i] = vector<long long>(heights[i].size());
        size_t nxt = 0, nxtLeft = 0, nxtRight = 0;
        long long sum = 0, sumLeft = 0, sumRight = 0;
        for (size_t j = 0; j < heights[i].size(); ++j) {
            int h = heights[i][j];
            while (nxt < inCol[i].size() && inCol[i][nxt].y <= h) sum += inCol[i][nxt++].w;
            if (i) {
                while (nxtLeft < inCol[i-1].size() && inCol[i-1][nxtLeft].y <= h) sumLeft += inCol[i-1][nxtLeft++].w;
            }
            if (i<N) {
                while (nxtRight < inCol[i+1].size() && inCol[i+1][nxtRight].y <= h) sumRight += inCol[i+1][nxtRight++].w;
            }
            inside[i][j] = sum, insideLeft[i][j] = sumLeft, insideRight[i][j] = sumRight;
        }
    }

    vector<vector<long long>> dp[2]; // 0 means >= previous, 1 means <= previous
    dp[0] = dp[1] = vector<vector<long long>>(1+N);

    /*
    observation: exists solution in which all local minimums have height 0
     and all local maximums have height effectively equal to N
    */

    long long ans = 0;
    dp[0][0] = dp[1][0] = {0};
    for (int x = 1; x <= N; ++x) {
        dp[0][x] = dp[1][x] = vector<long long>(heights[x].size());
        { // increasing
            long long maxDif = -INF; // max score - inside
            size_t nxtLeft = 0;
            for (size_t j = 0; j < heights[x].size(); ++j) {
                int y = heights[x][j];
                while (nxtLeft < heights[x-1].size() && heights[x-1][nxtLeft] <= y) {
                    maxDif = max(maxDif, dp[0][x-1][nxtLeft] - inside[x-1][nxtLeft]);
                    ++nxtLeft;
                }
                dp[0][x][j] = max(dp[0][x][j], insideLeft[x][j] + maxDif);
            }
        }
        { // decreasing
            long long maxSum = -INF; // max score + inside right
            int nxtLeft = (int)heights[x-1].size()-1;
            for (int j = (int)heights[x].size()-1; j >= 0; --j) {
                int y = heights[x][j];
                while (nxtLeft >= 0 && heights[x-1][nxtLeft] >= y) {
                    maxSum = max(maxSum, dp[1][x-1][nxtLeft] + insideRight[x-1][nxtLeft]);
                    --nxtLeft;
                }
                dp[1][x][j] = max(dp[1][x][j], maxSum - inside[x][j]);
            }
        }
        { // after local maximum
            size_t j1 = heights[x-1].size()-1;
            int h1 = heights[x-1][j1];
            for (size_t j = 0; j < heights[x].size() && heights[x][j] <= h1; ++j) {
                long long score = max(dp[0][x-1][j1], dp[1][x-1][j1]);
                score += insideRight[x-1][j1] - inside[x][j];
                dp[1][x][j] = max(dp[1][x][j], score);
                if (heights[x][j] == h1) dp[0][x][j] = max(dp[0][x][j], score);
            }
        }
        if (x > 1) { // after local minimum
            // case: column before 0 is higher than column after
            long long maxSum = 0;
            for (size_t j = 0; j < heights[x-2].size(); ++j) {
                maxSum = max(maxSum, max(dp[0][x-2][j], dp[1][x-2][j]) + insideRight[x-2][j]);
            }
            for (size_t j = 0; j < heights[x].size(); ++j) {
                dp[0][x][j] = max(dp[0][x][j], maxSum);
                if (!j) dp[1][x][j] = max(dp[1][x][j], maxSum);
            }

            // case: column after 0 is higher than column before
            long long maxScore = 0;
            for (size_t j = 0; j < heights[x-2].size(); ++j) {
                maxScore = max(maxScore, max(dp[0][x-2][j], dp[1][x-2][j]));
            }
            for (size_t j = 0; j < heights[x].size(); ++j) {
                dp[0][x][j] = max(dp[0][x][j], maxScore + insideLeft[x][j]);
                if (!j) dp[1][x][j] = max(dp[1][x][j], maxScore + insideLeft[x][j]);
            }
        }

        for (size_t j = 0; j < heights[x].size(); ++j) {
            ans = max(ans, max(dp[0][x][j], dp[1][x][j]));
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 46660 KB Output is correct
2 Correct 89 ms 52704 KB Output is correct
3 Correct 55 ms 44024 KB Output is correct
4 Correct 55 ms 44108 KB Output is correct
5 Correct 184 ms 82892 KB Output is correct
6 Correct 215 ms 81236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 116 ms 57148 KB Output is correct
3 Correct 136 ms 65776 KB Output is correct
4 Correct 80 ms 46640 KB Output is correct
5 Correct 91 ms 52728 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 0 ms 212 KB Output is correct
10 Correct 55 ms 44068 KB Output is correct
11 Correct 54 ms 44100 KB Output is correct
12 Correct 86 ms 50204 KB Output is correct
13 Correct 93 ms 57008 KB Output is correct
14 Correct 80 ms 48476 KB Output is correct
15 Correct 87 ms 53764 KB Output is correct
16 Correct 79 ms 48400 KB Output is correct
17 Correct 88 ms 53700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 44148 KB Output is correct
2 Correct 53 ms 44044 KB Output is correct
3 Correct 69 ms 40964 KB Output is correct
4 Correct 69 ms 44888 KB Output is correct
5 Correct 86 ms 46392 KB Output is correct
6 Correct 87 ms 46376 KB Output is correct
7 Correct 94 ms 46392 KB Output is correct
8 Correct 92 ms 46352 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 1 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 596 KB Output is correct
11 Correct 1 ms 340 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 468 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 1 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 596 KB Output is correct
11 Correct 1 ms 340 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 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 16 ms 5056 KB Output is correct
18 Correct 16 ms 5408 KB Output is correct
19 Correct 14 ms 5460 KB Output is correct
20 Correct 14 ms 5076 KB Output is correct
21 Correct 14 ms 5080 KB Output is correct
22 Correct 28 ms 9792 KB Output is correct
23 Correct 4 ms 1492 KB Output is correct
24 Correct 11 ms 4052 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 3 ms 1364 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 1 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 596 KB Output is correct
11 Correct 1 ms 340 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 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 16 ms 5056 KB Output is correct
18 Correct 16 ms 5408 KB Output is correct
19 Correct 14 ms 5460 KB Output is correct
20 Correct 14 ms 5076 KB Output is correct
21 Correct 14 ms 5080 KB Output is correct
22 Correct 28 ms 9792 KB Output is correct
23 Correct 4 ms 1492 KB Output is correct
24 Correct 11 ms 4052 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 3 ms 1364 KB Output is correct
27 Correct 3 ms 1620 KB Output is correct
28 Correct 79 ms 24580 KB Output is correct
29 Correct 123 ms 36872 KB Output is correct
30 Correct 124 ms 41044 KB Output is correct
31 Correct 129 ms 40928 KB Output is correct
32 Correct 101 ms 32104 KB Output is correct
33 Correct 127 ms 41400 KB Output is correct
34 Correct 126 ms 41312 KB Output is correct
35 Correct 47 ms 16140 KB Output is correct
36 Correct 124 ms 41428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 44148 KB Output is correct
2 Correct 53 ms 44044 KB Output is correct
3 Correct 69 ms 40964 KB Output is correct
4 Correct 69 ms 44888 KB Output is correct
5 Correct 86 ms 46392 KB Output is correct
6 Correct 87 ms 46376 KB Output is correct
7 Correct 94 ms 46392 KB Output is correct
8 Correct 92 ms 46352 KB Output is correct
9 Correct 92 ms 46392 KB Output is correct
10 Correct 61 ms 26840 KB Output is correct
11 Correct 132 ms 53508 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 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 54 ms 44096 KB Output is correct
19 Correct 55 ms 44036 KB Output is correct
20 Correct 54 ms 44124 KB Output is correct
21 Correct 57 ms 44000 KB Output is correct
22 Correct 110 ms 50508 KB Output is correct
23 Correct 143 ms 60356 KB Output is correct
24 Correct 137 ms 61332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 46660 KB Output is correct
2 Correct 89 ms 52704 KB Output is correct
3 Correct 55 ms 44024 KB Output is correct
4 Correct 55 ms 44108 KB Output is correct
5 Correct 184 ms 82892 KB Output is correct
6 Correct 215 ms 81236 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 116 ms 57148 KB Output is correct
9 Correct 136 ms 65776 KB Output is correct
10 Correct 80 ms 46640 KB Output is correct
11 Correct 91 ms 52728 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 0 ms 212 KB Output is correct
16 Correct 55 ms 44068 KB Output is correct
17 Correct 54 ms 44100 KB Output is correct
18 Correct 86 ms 50204 KB Output is correct
19 Correct 93 ms 57008 KB Output is correct
20 Correct 80 ms 48476 KB Output is correct
21 Correct 87 ms 53764 KB Output is correct
22 Correct 79 ms 48400 KB Output is correct
23 Correct 88 ms 53700 KB Output is correct
24 Correct 55 ms 44148 KB Output is correct
25 Correct 53 ms 44044 KB Output is correct
26 Correct 69 ms 40964 KB Output is correct
27 Correct 69 ms 44888 KB Output is correct
28 Correct 86 ms 46392 KB Output is correct
29 Correct 87 ms 46376 KB Output is correct
30 Correct 94 ms 46392 KB Output is correct
31 Correct 92 ms 46352 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 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 596 KB Output is correct
42 Correct 1 ms 340 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 468 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 16 ms 5056 KB Output is correct
49 Correct 16 ms 5408 KB Output is correct
50 Correct 14 ms 5460 KB Output is correct
51 Correct 14 ms 5076 KB Output is correct
52 Correct 14 ms 5080 KB Output is correct
53 Correct 28 ms 9792 KB Output is correct
54 Correct 4 ms 1492 KB Output is correct
55 Correct 11 ms 4052 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 3 ms 1364 KB Output is correct
58 Correct 3 ms 1620 KB Output is correct
59 Correct 79 ms 24580 KB Output is correct
60 Correct 123 ms 36872 KB Output is correct
61 Correct 124 ms 41044 KB Output is correct
62 Correct 129 ms 40928 KB Output is correct
63 Correct 101 ms 32104 KB Output is correct
64 Correct 127 ms 41400 KB Output is correct
65 Correct 126 ms 41312 KB Output is correct
66 Correct 47 ms 16140 KB Output is correct
67 Correct 124 ms 41428 KB Output is correct
68 Correct 92 ms 46392 KB Output is correct
69 Correct 61 ms 26840 KB Output is correct
70 Correct 132 ms 53508 KB Output is correct
71 Correct 0 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 212 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 54 ms 44096 KB Output is correct
78 Correct 55 ms 44036 KB Output is correct
79 Correct 54 ms 44124 KB Output is correct
80 Correct 57 ms 44000 KB Output is correct
81 Correct 110 ms 50508 KB Output is correct
82 Correct 143 ms 60356 KB Output is correct
83 Correct 137 ms 61332 KB Output is correct
84 Correct 184 ms 78048 KB Output is correct
85 Correct 192 ms 81852 KB Output is correct
86 Correct 229 ms 73160 KB Output is correct
87 Correct 237 ms 77016 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 231 ms 77024 KB Output is correct
90 Correct 200 ms 77924 KB Output is correct
91 Correct 170 ms 74600 KB Output is correct