Submission #1058568

# Submission time Handle Problem Language Result Execution time Memory
1058568 2024-08-14T11:02:46 Z aykhn Catfish Farm (IOI22_fish) C++17
100 / 100
321 ms 141396 KB
#include "fish.h"
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MXN = 2e5 + 5;


long long mx[MXN];
vector<long long> lft[MXN], rig[MXN], in[MXN];
vector<long long> pre[2][MXN], suf[2][MXN];
vector<long long> dp[2][MXN];
vector<long long> ind[MXN];
vector<array<long long, 2>> val[MXN];

/*
0 = len[i] < len[i + 1];
1 = len[i] > len[i + 1];
*/

long long max_weights(int32_t N, int32_t M, vector<int32_t> X, vector<int32_t> Y, vector<int32_t> W) 
{
    for (int i = 0; i < M; i++)
    {
        X[i]++, Y[i]++;
        if (X[i] - 1 >= 1) ind[X[i] - 1].push_back(Y[i]);
        if (X[i] + 1 <= N) ind[X[i] + 1].push_back(Y[i]);
        val[X[i]].push_back({Y[i], W[i]});
    }
    for (int i = 1; i <= N; i++)
    {
        sort(ind[i].begin(), ind[i].end());
        ind[i].resize(unique(ind[i].begin(), ind[i].end()) - ind[i].begin());
        sort(val[i].begin(), val[i].end());
    }
    for (int i = 1; i <= N; i++) 
    {
        int k1 = 0, k2 = 0, k3 = 0;
        for (int j = 0; j < ind[i].size(); j++)
        {
            lft[i].push_back((lft[i].empty() ? 0LL : lft[i].back()));
            rig[i].push_back((rig[i].empty() ? 0LL : rig[i].back()));
            in[i].push_back((in[i].empty() ? 0LL : in[i].back()));
            while (k1 < val[i - 1].size() && val[i - 1][k1][0] <= ind[i][j]) lft[i][j] += val[i - 1][k1][1], k1++;
            while (k2 < val[i + 1].size() && val[i + 1][k2][0] <= ind[i][j]) rig[i][j] += val[i + 1][k2][1], k2++;
            while (k3 < val[i].size() && val[i][k3][0] <= ind[i][j]) in[i][j] += val[i][k3][1], k3++;
        }
        
    }
    for (int i = 1; i <= N; i++)
    {
        int k1 = 0, k2 = 0, k3 = 0, k4 = 0;
        for (int j = 0; j < ind[i].size(); j++)
        {
            dp[0][i].push_back(lft[i][j] + rig[i][j]);
            dp[1][i].push_back(lft[i][j] + rig[i][j]);
            if (i <= 1) continue;
            while (k1 < ind[i - 1].size() && ind[i - 1][k1] <= ind[i][j]) k1++;
            while (k2 < ind[i - 1].size() && ind[i - 1][k2] < ind[i][j]) k2++;
            dp[0][i][j] = max(dp[0][i][j], (k1 - 1 >= 0 ? pre[0][i - 1][k1 - 1] : 0) + (lft[i][j] + rig[i][j]));
            dp[1][i][j] = max(dp[1][i][j], (k2 < ind[i - 1].size() ? suf[0][i - 1][k2] : 0) + (rig[i][j] - in[i][j]));
            if (i <= 2) continue;
            while (k3 < ind[i - 2].size() && ind[i - 2][k3] <= ind[i][j]) k3++;
            while (k4 < ind[i - 2].size() && ind[i - 2][k4] < ind[i][j]) k4++;
            long long A = (k3 - 1 >= 0 ? pre[1][i - 2][k3 - 1] : 0) + lft[i][j] + rig[i][j];
            long long B = (k4 < ind[i - 2].size() ? suf[1][i - 2][k4] : 0) + rig[i][j];
            dp[0][i][j] = max({dp[0][i][j], A, B});
            dp[1][i][j] = max({dp[1][i][j], A, B});
            if (i <= 3) continue;
            dp[0][i][j] = max(dp[0][i][j], mx[i - 3] + lft[i][j] + rig[i][j]);
            dp[1][i][j] = max(dp[1][i][j], mx[i - 3] + lft[i][j] + rig[i][j]);
        }
        for (int j = 0; j < ind[i].size(); j++)
        {
            dp[1][i][j] = max(dp[1][i][j], dp[0][i][j]);
            mx[i] = max({mx[i], dp[0][i][j], dp[1][i][j]});
        }
        for (int j = 0; j < ind[i].size(); j++) 
        {
            pre[0][i].push_back(max((pre[0][i].empty() ? 0LL : pre[0][i].back()), dp[0][i][j] - rig[i][j] - in[i][j]));
            pre[1][i].push_back(max((pre[1][i].empty() ? 0LL : pre[1][i].back()), max(dp[0][i][j], dp[1][i][j]) - rig[i][j]));
        }
        suf[0][i].assign(ind[i].size(), 0);
        suf[1][i].assign(ind[i].size(), 0);
        for (int j = (int)ind[i].size() - 1; j >= 0; j--)
        {
            if (j + 1 < ind[i].size()) suf[0][i][j] = suf[0][i][j + 1], suf[1][i][j] = suf[1][i][j + 1];
            suf[0][i][j] = max(suf[0][i][j], dp[1][i][j]);
            suf[1][i][j] = max(suf[1][i][j], max(dp[0][i][j], dp[1][i][j]));
        }
        mx[i] = max(mx[i], mx[i - 1]);
    }
    long long ans = 0;
    for (int i = 1; i <= N; i++) 
    {
        for (long long &j : dp[0][i]) ans = max(ans, j);
        for (long long &j : dp[1][i]) ans = max(ans, j);
    }
    return ans;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int32_t, int32_t, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:41:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:46:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |             while (k1 < val[i - 1].size() && val[i - 1][k1][0] <= ind[i][j]) lft[i][j] += val[i - 1][k1][1], k1++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:47:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |             while (k2 < val[i + 1].size() && val[i + 1][k2][0] <= ind[i][j]) rig[i][j] += val[i + 1][k2][1], k2++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:48:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |             while (k3 < val[i].size() && val[i][k3][0] <= ind[i][j]) in[i][j] += val[i][k3][1], k3++;
      |                    ~~~^~~~~~~~~~~~~~~
fish.cpp:55:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:60:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |             while (k1 < ind[i - 1].size() && ind[i - 1][k1] <= ind[i][j]) k1++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:61:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |             while (k2 < ind[i - 1].size() && ind[i - 1][k2] < ind[i][j]) k2++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:63:48: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |             dp[1][i][j] = max(dp[1][i][j], (k2 < ind[i - 1].size() ? suf[0][i - 1][k2] : 0) + (rig[i][j] - in[i][j]));
      |                                             ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:65:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             while (k3 < ind[i - 2].size() && ind[i - 2][k3] <= ind[i][j]) k3++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:66:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |             while (k4 < ind[i - 2].size() && ind[i - 2][k4] < ind[i][j]) k4++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:68:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |             long long B = (k4 < ind[i - 2].size() ? suf[1][i - 2][k4] : 0) + rig[i][j];
      |                            ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:75:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:80:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:89:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |             if (j + 1 < ind[i].size()) suf[0][i][j] = suf[0][i][j + 1], suf[1][i][j] = suf[1][i][j + 1];
      |                 ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 41 ms 63932 KB Output is correct
2 Correct 44 ms 65940 KB Output is correct
3 Correct 14 ms 52652 KB Output is correct
4 Correct 15 ms 52828 KB Output is correct
5 Correct 149 ms 116100 KB Output is correct
6 Correct 242 ms 134988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 52060 KB Output is correct
2 Correct 74 ms 81148 KB Output is correct
3 Correct 91 ms 86696 KB Output is correct
4 Correct 39 ms 63936 KB Output is correct
5 Correct 44 ms 65980 KB Output is correct
6 Correct 12 ms 52060 KB Output is correct
7 Correct 12 ms 52056 KB Output is correct
8 Correct 11 ms 52060 KB Output is correct
9 Correct 12 ms 51912 KB Output is correct
10 Correct 13 ms 52828 KB Output is correct
11 Correct 14 ms 52832 KB Output is correct
12 Correct 47 ms 70792 KB Output is correct
13 Correct 54 ms 74164 KB Output is correct
14 Correct 44 ms 66992 KB Output is correct
15 Correct 47 ms 68148 KB Output is correct
16 Correct 45 ms 67232 KB Output is correct
17 Correct 48 ms 68744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 52828 KB Output is correct
2 Correct 13 ms 52748 KB Output is correct
3 Correct 46 ms 73812 KB Output is correct
4 Correct 43 ms 67408 KB Output is correct
5 Correct 75 ms 89424 KB Output is correct
6 Correct 82 ms 89624 KB Output is correct
7 Correct 94 ms 89672 KB Output is correct
8 Correct 83 ms 89632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 52060 KB Output is correct
2 Correct 12 ms 52108 KB Output is correct
3 Correct 12 ms 52060 KB Output is correct
4 Correct 12 ms 52060 KB Output is correct
5 Correct 12 ms 52108 KB Output is correct
6 Correct 12 ms 52060 KB Output is correct
7 Correct 12 ms 52044 KB Output is correct
8 Correct 13 ms 52056 KB Output is correct
9 Correct 12 ms 52124 KB Output is correct
10 Correct 15 ms 52316 KB Output is correct
11 Correct 12 ms 52260 KB Output is correct
12 Correct 12 ms 52144 KB Output is correct
13 Correct 13 ms 52060 KB Output is correct
14 Correct 13 ms 52244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 52060 KB Output is correct
2 Correct 12 ms 52108 KB Output is correct
3 Correct 12 ms 52060 KB Output is correct
4 Correct 12 ms 52060 KB Output is correct
5 Correct 12 ms 52108 KB Output is correct
6 Correct 12 ms 52060 KB Output is correct
7 Correct 12 ms 52044 KB Output is correct
8 Correct 13 ms 52056 KB Output is correct
9 Correct 12 ms 52124 KB Output is correct
10 Correct 15 ms 52316 KB Output is correct
11 Correct 12 ms 52260 KB Output is correct
12 Correct 12 ms 52144 KB Output is correct
13 Correct 13 ms 52060 KB Output is correct
14 Correct 13 ms 52244 KB Output is correct
15 Correct 12 ms 52224 KB Output is correct
16 Correct 13 ms 52396 KB Output is correct
17 Correct 28 ms 61096 KB Output is correct
18 Correct 27 ms 61528 KB Output is correct
19 Correct 27 ms 61528 KB Output is correct
20 Correct 28 ms 60460 KB Output is correct
21 Correct 35 ms 60488 KB Output is correct
22 Correct 51 ms 68784 KB Output is correct
23 Correct 16 ms 54108 KB Output is correct
24 Correct 25 ms 59112 KB Output is correct
25 Correct 14 ms 52316 KB Output is correct
26 Correct 16 ms 53916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 52060 KB Output is correct
2 Correct 12 ms 52108 KB Output is correct
3 Correct 12 ms 52060 KB Output is correct
4 Correct 12 ms 52060 KB Output is correct
5 Correct 12 ms 52108 KB Output is correct
6 Correct 12 ms 52060 KB Output is correct
7 Correct 12 ms 52044 KB Output is correct
8 Correct 13 ms 52056 KB Output is correct
9 Correct 12 ms 52124 KB Output is correct
10 Correct 15 ms 52316 KB Output is correct
11 Correct 12 ms 52260 KB Output is correct
12 Correct 12 ms 52144 KB Output is correct
13 Correct 13 ms 52060 KB Output is correct
14 Correct 13 ms 52244 KB Output is correct
15 Correct 12 ms 52224 KB Output is correct
16 Correct 13 ms 52396 KB Output is correct
17 Correct 28 ms 61096 KB Output is correct
18 Correct 27 ms 61528 KB Output is correct
19 Correct 27 ms 61528 KB Output is correct
20 Correct 28 ms 60460 KB Output is correct
21 Correct 35 ms 60488 KB Output is correct
22 Correct 51 ms 68784 KB Output is correct
23 Correct 16 ms 54108 KB Output is correct
24 Correct 25 ms 59112 KB Output is correct
25 Correct 14 ms 52316 KB Output is correct
26 Correct 16 ms 53916 KB Output is correct
27 Correct 14 ms 53164 KB Output is correct
28 Correct 85 ms 92696 KB Output is correct
29 Correct 127 ms 109652 KB Output is correct
30 Correct 150 ms 123472 KB Output is correct
31 Correct 154 ms 123476 KB Output is correct
32 Correct 110 ms 110416 KB Output is correct
33 Correct 153 ms 123724 KB Output is correct
34 Correct 143 ms 123724 KB Output is correct
35 Correct 62 ms 79956 KB Output is correct
36 Correct 144 ms 116844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 52828 KB Output is correct
2 Correct 13 ms 52748 KB Output is correct
3 Correct 46 ms 73812 KB Output is correct
4 Correct 43 ms 67408 KB Output is correct
5 Correct 75 ms 89424 KB Output is correct
6 Correct 82 ms 89624 KB Output is correct
7 Correct 94 ms 89672 KB Output is correct
8 Correct 83 ms 89632 KB Output is correct
9 Correct 89 ms 89644 KB Output is correct
10 Correct 65 ms 73812 KB Output is correct
11 Correct 136 ms 95684 KB Output is correct
12 Correct 12 ms 52060 KB Output is correct
13 Correct 12 ms 52060 KB Output is correct
14 Correct 12 ms 52060 KB Output is correct
15 Correct 13 ms 52060 KB Output is correct
16 Correct 13 ms 52060 KB Output is correct
17 Correct 13 ms 52056 KB Output is correct
18 Correct 13 ms 52824 KB Output is correct
19 Correct 15 ms 52720 KB Output is correct
20 Correct 14 ms 52840 KB Output is correct
21 Correct 13 ms 52896 KB Output is correct
22 Correct 114 ms 89940 KB Output is correct
23 Correct 223 ms 106996 KB Output is correct
24 Correct 175 ms 108880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 63932 KB Output is correct
2 Correct 44 ms 65940 KB Output is correct
3 Correct 14 ms 52652 KB Output is correct
4 Correct 15 ms 52828 KB Output is correct
5 Correct 149 ms 116100 KB Output is correct
6 Correct 242 ms 134988 KB Output is correct
7 Correct 12 ms 52060 KB Output is correct
8 Correct 74 ms 81148 KB Output is correct
9 Correct 91 ms 86696 KB Output is correct
10 Correct 39 ms 63936 KB Output is correct
11 Correct 44 ms 65980 KB Output is correct
12 Correct 12 ms 52060 KB Output is correct
13 Correct 12 ms 52056 KB Output is correct
14 Correct 11 ms 52060 KB Output is correct
15 Correct 12 ms 51912 KB Output is correct
16 Correct 13 ms 52828 KB Output is correct
17 Correct 14 ms 52832 KB Output is correct
18 Correct 47 ms 70792 KB Output is correct
19 Correct 54 ms 74164 KB Output is correct
20 Correct 44 ms 66992 KB Output is correct
21 Correct 47 ms 68148 KB Output is correct
22 Correct 45 ms 67232 KB Output is correct
23 Correct 48 ms 68744 KB Output is correct
24 Correct 13 ms 52828 KB Output is correct
25 Correct 13 ms 52748 KB Output is correct
26 Correct 46 ms 73812 KB Output is correct
27 Correct 43 ms 67408 KB Output is correct
28 Correct 75 ms 89424 KB Output is correct
29 Correct 82 ms 89624 KB Output is correct
30 Correct 94 ms 89672 KB Output is correct
31 Correct 83 ms 89632 KB Output is correct
32 Correct 12 ms 52060 KB Output is correct
33 Correct 12 ms 52108 KB Output is correct
34 Correct 12 ms 52060 KB Output is correct
35 Correct 12 ms 52060 KB Output is correct
36 Correct 12 ms 52108 KB Output is correct
37 Correct 12 ms 52060 KB Output is correct
38 Correct 12 ms 52044 KB Output is correct
39 Correct 13 ms 52056 KB Output is correct
40 Correct 12 ms 52124 KB Output is correct
41 Correct 15 ms 52316 KB Output is correct
42 Correct 12 ms 52260 KB Output is correct
43 Correct 12 ms 52144 KB Output is correct
44 Correct 13 ms 52060 KB Output is correct
45 Correct 13 ms 52244 KB Output is correct
46 Correct 12 ms 52224 KB Output is correct
47 Correct 13 ms 52396 KB Output is correct
48 Correct 28 ms 61096 KB Output is correct
49 Correct 27 ms 61528 KB Output is correct
50 Correct 27 ms 61528 KB Output is correct
51 Correct 28 ms 60460 KB Output is correct
52 Correct 35 ms 60488 KB Output is correct
53 Correct 51 ms 68784 KB Output is correct
54 Correct 16 ms 54108 KB Output is correct
55 Correct 25 ms 59112 KB Output is correct
56 Correct 14 ms 52316 KB Output is correct
57 Correct 16 ms 53916 KB Output is correct
58 Correct 14 ms 53164 KB Output is correct
59 Correct 85 ms 92696 KB Output is correct
60 Correct 127 ms 109652 KB Output is correct
61 Correct 150 ms 123472 KB Output is correct
62 Correct 154 ms 123476 KB Output is correct
63 Correct 110 ms 110416 KB Output is correct
64 Correct 153 ms 123724 KB Output is correct
65 Correct 143 ms 123724 KB Output is correct
66 Correct 62 ms 79956 KB Output is correct
67 Correct 144 ms 116844 KB Output is correct
68 Correct 89 ms 89644 KB Output is correct
69 Correct 65 ms 73812 KB Output is correct
70 Correct 136 ms 95684 KB Output is correct
71 Correct 12 ms 52060 KB Output is correct
72 Correct 12 ms 52060 KB Output is correct
73 Correct 12 ms 52060 KB Output is correct
74 Correct 13 ms 52060 KB Output is correct
75 Correct 13 ms 52060 KB Output is correct
76 Correct 13 ms 52056 KB Output is correct
77 Correct 13 ms 52824 KB Output is correct
78 Correct 15 ms 52720 KB Output is correct
79 Correct 14 ms 52840 KB Output is correct
80 Correct 13 ms 52896 KB Output is correct
81 Correct 114 ms 89940 KB Output is correct
82 Correct 223 ms 106996 KB Output is correct
83 Correct 175 ms 108880 KB Output is correct
84 Correct 142 ms 112544 KB Output is correct
85 Correct 150 ms 113252 KB Output is correct
86 Correct 305 ms 139604 KB Output is correct
87 Correct 318 ms 141376 KB Output is correct
88 Correct 12 ms 52060 KB Output is correct
89 Correct 321 ms 141396 KB Output is correct
90 Correct 208 ms 128340 KB Output is correct
91 Correct 165 ms 112984 KB Output is correct