Submission #625735

# Submission time Handle Problem Language Result Execution time Memory
625735 2022-08-10T17:49:05 Z phathnv Catfish Farm (IOI22_fish) C++17
100 / 100
277 ms 24624 KB
#include "fish.h"

#include <bits/stdc++.h>
using namespace std;

long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
    vector<vector<int>> a(n + 1);
    vector<vector<array<int, 2>>> b(n + 1);
    vector<array<long long, 2>> dp[2], nDp[2];

    for (int i = 0; i < m; ++i) {
        b[x[i]].push_back({y[i], w[i]});
        if (0 <= x[i] - 1) {
            a[x[i] - 1].push_back(y[i]);
        }
        if (x[i] + 1 < n) {
            a[x[i] + 1].push_back(y[i]);
        }
    }
    for (int i = 0; i <= n; ++i) {
        a[i].push_back(-1);
        sort(a[i].begin(), a[i].end());
        sort(b[i].begin(), b[i].end());
        a[i].resize(unique(a[i].begin(), a[i].end()) - a[i].begin());
    }

    dp[0].push_back({-1, 0});
    dp[1].push_back({-1, 0});
    vector<array<int, 2>> weights;
    for (int i = 0; i <= n; ++i) {
        nDp[0].clear();
        nDp[1].clear();
        for (auto pos : a[i]) {
            nDp[0].push_back({pos, 0});
            nDp[1].push_back({pos, 0});
        }

        long long sumW1, sumW2;
        long long best;
        int ptrW1, ptrW2, ptrDp;

        // 0
        sumW1 = 0;
        sumW2 = 0;
        best = -1e18;
        ptrW1 = 0;
        ptrW2 = 0;
        ptrDp = 0;
        for (auto &[h, val] : nDp[0]) {
            while (ptrDp < (int)dp[0].size() && h >= dp[0][ptrDp][0]) {
                while (ptrW1 < (int)weights.size() && dp[0][ptrDp][0] >= weights[ptrW1][0]) {
                    sumW1 += weights[ptrW1][1];
                    ++ptrW1;
                }
                best = max(best, dp[0][ptrDp][1] - sumW1);
                ++ptrDp;
            }
            while (ptrW2 < (int)weights.size() && h >= weights[ptrW2][0]) {
                sumW2 += weights[ptrW2][1];
                ++ptrW2;
            }
            val = best + sumW2;
        }


        // 1
        weights = b[i];
        sumW1 = 0;
        sumW2 = 0;
        best = -1e18;
        ptrW1 = weights.size() - 1;
        ptrW2 = weights.size() - 1;
        ptrDp = dp[1].size() - 1;
        for (auto [h, w] : weights) {
            sumW1 += w;
            sumW2 += w;
        }

        for (int j = nDp[1].size() - 1; j >= 0; --j) {
            auto &[h, val] = nDp[1][j];
            while (ptrDp >= 0 && h <= dp[1][ptrDp][0]) {
                while (ptrW1 >= 0 && dp[1][ptrDp][0] < weights[ptrW1][0]) {
                    sumW1 -= weights[ptrW1][1];
                    --ptrW1;
                }
                best = max(best, dp[1][ptrDp][1] + sumW1);
                --ptrDp;
            }
            while (ptrW2 >= 0 && h < weights[ptrW2][0]) {
                sumW2 -= weights[ptrW2][1];
                --ptrW2;
            }
            val = best - sumW2;
        }

        // 1->0
        for (int j = 0; j < (int) nDp[0].size(); ++j) {
            nDp[0][j][1] = max(nDp[0][j][1], dp[1][0][1]);
        }

        // 0->1
        for (int j = 0; j < (int) nDp[0].size(); ++j) {
            nDp[1][j][1] = max(nDp[1][j][1], nDp[0][j][1]);
        }

        swap(dp[0], nDp[0]);
        swap(dp[1], nDp[1]);
    }

    return dp[1][0][1];
}
# Verdict Execution time Memory Grader output
1 Correct 53 ms 13960 KB Output is correct
2 Correct 59 ms 15632 KB Output is correct
3 Correct 12 ms 8128 KB Output is correct
4 Correct 12 ms 8096 KB Output is correct
5 Correct 183 ms 23724 KB Output is correct
6 Correct 248 ms 22156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 99 ms 21464 KB Output is correct
3 Correct 125 ms 24624 KB Output is correct
4 Correct 50 ms 13956 KB Output is correct
5 Correct 60 ms 15712 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 13 ms 8020 KB Output is correct
11 Correct 12 ms 8068 KB Output is correct
12 Correct 64 ms 13860 KB Output is correct
13 Correct 69 ms 15984 KB Output is correct
14 Correct 54 ms 14392 KB Output is correct
15 Correct 63 ms 15804 KB Output is correct
16 Correct 54 ms 14468 KB Output is correct
17 Correct 60 ms 15696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8020 KB Output is correct
2 Correct 13 ms 8048 KB Output is correct
3 Correct 42 ms 10188 KB Output is correct
4 Correct 33 ms 10080 KB Output is correct
5 Correct 84 ms 13504 KB Output is correct
6 Correct 79 ms 13600 KB Output is correct
7 Correct 87 ms 13508 KB Output is correct
8 Correct 83 ms 13556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 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 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 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 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 21 ms 3000 KB Output is correct
18 Correct 21 ms 3632 KB Output is correct
19 Correct 21 ms 3508 KB Output is correct
20 Correct 21 ms 3508 KB Output is correct
21 Correct 20 ms 3508 KB Output is correct
22 Correct 41 ms 6472 KB Output is correct
23 Correct 5 ms 936 KB Output is correct
24 Correct 17 ms 2132 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 5 ms 800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 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 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 21 ms 3000 KB Output is correct
18 Correct 21 ms 3632 KB Output is correct
19 Correct 21 ms 3508 KB Output is correct
20 Correct 21 ms 3508 KB Output is correct
21 Correct 20 ms 3508 KB Output is correct
22 Correct 41 ms 6472 KB Output is correct
23 Correct 5 ms 936 KB Output is correct
24 Correct 17 ms 2132 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 5 ms 800 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
28 Correct 107 ms 12880 KB Output is correct
29 Correct 155 ms 16428 KB Output is correct
30 Correct 159 ms 15532 KB Output is correct
31 Correct 160 ms 15524 KB Output is correct
32 Correct 138 ms 18068 KB Output is correct
33 Correct 159 ms 15380 KB Output is correct
34 Correct 161 ms 14752 KB Output is correct
35 Correct 58 ms 6988 KB Output is correct
36 Correct 160 ms 16812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8020 KB Output is correct
2 Correct 13 ms 8048 KB Output is correct
3 Correct 42 ms 10188 KB Output is correct
4 Correct 33 ms 10080 KB Output is correct
5 Correct 84 ms 13504 KB Output is correct
6 Correct 79 ms 13600 KB Output is correct
7 Correct 87 ms 13508 KB Output is correct
8 Correct 83 ms 13556 KB Output is correct
9 Correct 89 ms 13556 KB Output is correct
10 Correct 62 ms 9596 KB Output is correct
11 Correct 160 ms 18908 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 12 ms 8020 KB Output is correct
19 Correct 14 ms 8020 KB Output is correct
20 Correct 11 ms 8104 KB Output is correct
21 Correct 11 ms 8020 KB Output is correct
22 Correct 85 ms 12612 KB Output is correct
23 Correct 160 ms 19348 KB Output is correct
24 Correct 165 ms 19596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 13960 KB Output is correct
2 Correct 59 ms 15632 KB Output is correct
3 Correct 12 ms 8128 KB Output is correct
4 Correct 12 ms 8096 KB Output is correct
5 Correct 183 ms 23724 KB Output is correct
6 Correct 248 ms 22156 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 99 ms 21464 KB Output is correct
9 Correct 125 ms 24624 KB Output is correct
10 Correct 50 ms 13956 KB Output is correct
11 Correct 60 ms 15712 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 13 ms 8020 KB Output is correct
17 Correct 12 ms 8068 KB Output is correct
18 Correct 64 ms 13860 KB Output is correct
19 Correct 69 ms 15984 KB Output is correct
20 Correct 54 ms 14392 KB Output is correct
21 Correct 63 ms 15804 KB Output is correct
22 Correct 54 ms 14468 KB Output is correct
23 Correct 60 ms 15696 KB Output is correct
24 Correct 13 ms 8020 KB Output is correct
25 Correct 13 ms 8048 KB Output is correct
26 Correct 42 ms 10188 KB Output is correct
27 Correct 33 ms 10080 KB Output is correct
28 Correct 84 ms 13504 KB Output is correct
29 Correct 79 ms 13600 KB Output is correct
30 Correct 87 ms 13508 KB Output is correct
31 Correct 83 ms 13556 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 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 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 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 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 296 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 21 ms 3000 KB Output is correct
49 Correct 21 ms 3632 KB Output is correct
50 Correct 21 ms 3508 KB Output is correct
51 Correct 21 ms 3508 KB Output is correct
52 Correct 20 ms 3508 KB Output is correct
53 Correct 41 ms 6472 KB Output is correct
54 Correct 5 ms 936 KB Output is correct
55 Correct 17 ms 2132 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 5 ms 800 KB Output is correct
58 Correct 2 ms 724 KB Output is correct
59 Correct 107 ms 12880 KB Output is correct
60 Correct 155 ms 16428 KB Output is correct
61 Correct 159 ms 15532 KB Output is correct
62 Correct 160 ms 15524 KB Output is correct
63 Correct 138 ms 18068 KB Output is correct
64 Correct 159 ms 15380 KB Output is correct
65 Correct 161 ms 14752 KB Output is correct
66 Correct 58 ms 6988 KB Output is correct
67 Correct 160 ms 16812 KB Output is correct
68 Correct 89 ms 13556 KB Output is correct
69 Correct 62 ms 9596 KB Output is correct
70 Correct 160 ms 18908 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 12 ms 8020 KB Output is correct
78 Correct 14 ms 8020 KB Output is correct
79 Correct 11 ms 8104 KB Output is correct
80 Correct 11 ms 8020 KB Output is correct
81 Correct 85 ms 12612 KB Output is correct
82 Correct 160 ms 19348 KB Output is correct
83 Correct 165 ms 19596 KB Output is correct
84 Correct 200 ms 23104 KB Output is correct
85 Correct 188 ms 24292 KB Output is correct
86 Correct 259 ms 22928 KB Output is correct
87 Correct 264 ms 23616 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 277 ms 23728 KB Output is correct
90 Correct 200 ms 22348 KB Output is correct
91 Correct 176 ms 22396 KB Output is correct