Submission #625693

# Submission time Handle Problem Language Result Execution time Memory
625693 2022-08-10T17:12:15 Z phathnv Catfish Farm (IOI22_fish) C++17
100 / 100
360 ms 60232 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<vector<array<long long, 2>>> dp[2];
    dp[0].resize(n + 1);
    dp[1].resize(n + 1);

    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());
    }

    for (int i = 0; i <= n; ++i) {
        for (auto pos : a[i]) {
            dp[0][i].push_back({pos, 0});
            dp[1][i].push_back({pos, 0});
        }

        if (i == 0) {
            continue;
        }

        long long sumW;
        long long best;
        int ptrW, ptrDp;
        vector<array<int, 2>> weights;
        auto pre = dp[0][i - 1];

        // 0
        weights = b[i - 1];
        sumW = 0;
        ptrW = 0;
        for (auto &[h, val]: pre) {
            while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
                sumW += weights[ptrW][1];
                ++ptrW;
            }
            val -= sumW;
        }

        sumW = 0;
        ptrW = 0;
        ptrDp = 0;
        best = -1e18;
        for (auto &[h, val]: dp[0][i]) {
            while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
                sumW += weights[ptrW][1];
                ++ptrW;
            }
            while (ptrDp < (int)pre.size() && h >= pre[ptrDp][0]) {
                best = max(best, pre[ptrDp][1]);
                ++ptrDp;
            }
            val = sumW + best;
        }

        // 1
        weights = b[i];
        sumW = 0;
        ptrW = 0;
        pre = dp[1][i - 1];
        for (auto &[h, val]: pre) {
            while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
                sumW += weights[ptrW][1];
                ++ptrW;
            }
            val += sumW;
        }

        ptrW = (int)weights.size() - 1;
        ptrDp = (int)pre.size() - 1;
        best = -1e18;
        sumW = 0;
        for (auto [h, w] : weights) {
            sumW += w;
        }
        for (int j = (int)dp[1][i].size() - 1; j >= 0; --j) {
            auto &[h, val] = dp[1][i][j];

            while (ptrW >= 0 && h < weights[ptrW][0]) {
                sumW -= weights[ptrW][1];
                --ptrW;
            }
            while (ptrDp >= 0 && h <= pre[ptrDp][0]) {
                best = max(best, pre[ptrDp][1]);
                --ptrDp;
            }
            val = best - sumW;
        }

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

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

    return dp[1][n][0][1];
}
# Verdict Execution time Memory Grader output
1 Correct 64 ms 22516 KB Output is correct
2 Correct 76 ms 25700 KB Output is correct
3 Correct 36 ms 19068 KB Output is correct
4 Correct 29 ms 19028 KB Output is correct
5 Correct 209 ms 54320 KB Output is correct
6 Correct 273 ms 59244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 146 ms 32588 KB Output is correct
3 Correct 139 ms 36936 KB Output is correct
4 Correct 64 ms 22584 KB Output is correct
5 Correct 76 ms 25672 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 29 ms 19020 KB Output is correct
11 Correct 30 ms 19040 KB Output is correct
12 Correct 80 ms 26412 KB Output is correct
13 Correct 90 ms 29872 KB Output is correct
14 Correct 69 ms 24132 KB Output is correct
15 Correct 85 ms 26756 KB Output is correct
16 Correct 69 ms 24120 KB Output is correct
17 Correct 93 ms 26700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 18952 KB Output is correct
2 Correct 28 ms 19028 KB Output is correct
3 Correct 67 ms 21908 KB Output is correct
4 Correct 55 ms 22264 KB Output is correct
5 Correct 164 ms 27688 KB Output is correct
6 Correct 132 ms 27716 KB Output is correct
7 Correct 122 ms 27688 KB Output is correct
8 Correct 135 ms 27592 KB Output is correct
# Verdict Execution time Memory 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 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 596 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 212 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 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 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 596 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 26 ms 5332 KB Output is correct
18 Correct 27 ms 5592 KB Output is correct
19 Correct 20 ms 5576 KB Output is correct
20 Correct 21 ms 5192 KB Output is correct
21 Correct 21 ms 5132 KB Output is correct
22 Correct 47 ms 9932 KB Output is correct
23 Correct 5 ms 1492 KB Output is correct
24 Correct 19 ms 4052 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
# Verdict Execution time Memory 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 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 596 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 26 ms 5332 KB Output is correct
18 Correct 27 ms 5592 KB Output is correct
19 Correct 20 ms 5576 KB Output is correct
20 Correct 21 ms 5192 KB Output is correct
21 Correct 21 ms 5132 KB Output is correct
22 Correct 47 ms 9932 KB Output is correct
23 Correct 5 ms 1492 KB Output is correct
24 Correct 19 ms 4052 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
27 Correct 3 ms 1236 KB Output is correct
28 Correct 104 ms 22932 KB Output is correct
29 Correct 157 ms 30848 KB Output is correct
30 Correct 173 ms 38508 KB Output is correct
31 Correct 172 ms 38368 KB Output is correct
32 Correct 162 ms 30816 KB Output is correct
33 Correct 186 ms 38556 KB Output is correct
34 Correct 184 ms 38392 KB Output is correct
35 Correct 64 ms 15864 KB Output is correct
36 Correct 184 ms 35104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 18952 KB Output is correct
2 Correct 28 ms 19028 KB Output is correct
3 Correct 67 ms 21908 KB Output is correct
4 Correct 55 ms 22264 KB Output is correct
5 Correct 164 ms 27688 KB Output is correct
6 Correct 132 ms 27716 KB Output is correct
7 Correct 122 ms 27688 KB Output is correct
8 Correct 135 ms 27592 KB Output is correct
9 Correct 132 ms 33996 KB Output is correct
10 Correct 85 ms 19768 KB Output is correct
11 Correct 208 ms 39260 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 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 30 ms 19028 KB Output is correct
19 Correct 28 ms 18976 KB Output is correct
20 Correct 28 ms 18988 KB Output is correct
21 Correct 31 ms 19048 KB Output is correct
22 Correct 169 ms 32008 KB Output is correct
23 Correct 215 ms 46276 KB Output is correct
24 Correct 226 ms 48724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 22516 KB Output is correct
2 Correct 76 ms 25700 KB Output is correct
3 Correct 36 ms 19068 KB Output is correct
4 Correct 29 ms 19028 KB Output is correct
5 Correct 209 ms 54320 KB Output is correct
6 Correct 273 ms 59244 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 146 ms 32588 KB Output is correct
9 Correct 139 ms 36936 KB Output is correct
10 Correct 64 ms 22584 KB Output is correct
11 Correct 76 ms 25672 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 29 ms 19020 KB Output is correct
17 Correct 30 ms 19040 KB Output is correct
18 Correct 80 ms 26412 KB Output is correct
19 Correct 90 ms 29872 KB Output is correct
20 Correct 69 ms 24132 KB Output is correct
21 Correct 85 ms 26756 KB Output is correct
22 Correct 69 ms 24120 KB Output is correct
23 Correct 93 ms 26700 KB Output is correct
24 Correct 37 ms 18952 KB Output is correct
25 Correct 28 ms 19028 KB Output is correct
26 Correct 67 ms 21908 KB Output is correct
27 Correct 55 ms 22264 KB Output is correct
28 Correct 164 ms 27688 KB Output is correct
29 Correct 132 ms 27716 KB Output is correct
30 Correct 122 ms 27688 KB Output is correct
31 Correct 135 ms 27592 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 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 596 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 2 ms 468 KB Output is correct
48 Correct 26 ms 5332 KB Output is correct
49 Correct 27 ms 5592 KB Output is correct
50 Correct 20 ms 5576 KB Output is correct
51 Correct 21 ms 5192 KB Output is correct
52 Correct 21 ms 5132 KB Output is correct
53 Correct 47 ms 9932 KB Output is correct
54 Correct 5 ms 1492 KB Output is correct
55 Correct 19 ms 4052 KB Output is correct
56 Correct 2 ms 468 KB Output is correct
57 Correct 5 ms 1364 KB Output is correct
58 Correct 3 ms 1236 KB Output is correct
59 Correct 104 ms 22932 KB Output is correct
60 Correct 157 ms 30848 KB Output is correct
61 Correct 173 ms 38508 KB Output is correct
62 Correct 172 ms 38368 KB Output is correct
63 Correct 162 ms 30816 KB Output is correct
64 Correct 186 ms 38556 KB Output is correct
65 Correct 184 ms 38392 KB Output is correct
66 Correct 64 ms 15864 KB Output is correct
67 Correct 184 ms 35104 KB Output is correct
68 Correct 132 ms 33996 KB Output is correct
69 Correct 85 ms 19768 KB Output is correct
70 Correct 208 ms 39260 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 1 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 30 ms 19028 KB Output is correct
78 Correct 28 ms 18976 KB Output is correct
79 Correct 28 ms 18988 KB Output is correct
80 Correct 31 ms 19048 KB Output is correct
81 Correct 169 ms 32008 KB Output is correct
82 Correct 215 ms 46276 KB Output is correct
83 Correct 226 ms 48724 KB Output is correct
84 Correct 216 ms 50572 KB Output is correct
85 Correct 224 ms 50688 KB Output is correct
86 Correct 326 ms 58328 KB Output is correct
87 Correct 360 ms 60132 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 336 ms 60232 KB Output is correct
90 Correct 256 ms 56288 KB Output is correct
91 Correct 211 ms 50524 KB Output is correct