Submission #1058549

# Submission time Handle Problem Language Result Execution time Memory
1058549 2024-08-14T10:49:12 Z aykhn Catfish Farm (IOI22_fish) C++17
100 / 100
575 ms 380732 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];
*/
int A[9] = {0, 0, 0, -1, -1, -1, 1, 1, 1};
int B[9] = {0, -1, 1, 0, -1, 1, 0, -1, 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 = 1; i <= N; i++) ind[i] = {1};
    for (int i = 0; i < M; i++)
    {
        X[i]++, Y[i]++;
        for (int j = 0; j < 9; j++)
        {
            int x = X[i] + A[j], y = Y[i] + B[j];
            if (min(x, y) >= 1 && max(x, y) <= N)
            {
                ind[x].push_back(y);
            }
        }
        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++;
            if (k1 - 1 >= 0) dp[0][i][j] = max(dp[0][i][j], pre[0][i - 1][k1 - 1] + (lft[i][j] + rig[i][j]));
            if (k2 < ind[i - 1].size()) dp[1][i][j] = max(dp[1][i][j], suf[0][i - 1][k2] + (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]));
        }
    }
    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:50: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]
   50 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:55: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]
   55 |             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:56: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]
   56 |             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:57: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]
   57 |             while (k3 < val[i].size() && val[i][k3][0] <= ind[i][j]) in[i][j] += val[i][k3][1], k3++;
      |                    ~~~^~~~~~~~~~~~~~~
fish.cpp:64: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]
   64 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:69: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]
   69 |             while (k1 < ind[i - 1].size() && ind[i - 1][k1] <= ind[i][j]) k1++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:70: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]
   70 |             while (k2 < ind[i - 1].size() && ind[i - 1][k2] < ind[i][j]) k2++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:72:20: 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]
   72 |             if (k2 < ind[i - 1].size()) dp[1][i][j] = max(dp[1][i][j], suf[0][i - 1][k2] + (rig[i][j] - in[i][j]));
      |                 ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:74: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]
   74 |             while (k3 < ind[i - 2].size() && ind[i - 2][k3] <= ind[i][j]) k3++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:75: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]
   75 |             while (k4 < ind[i - 2].size() && ind[i - 2][k4] < ind[i][j]) k4++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:77: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]
   77 |             long long B = (k4 < ind[i - 2].size() ? suf[1][i - 2][k4] : 0) + rig[i][j];
      |                            ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:84: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]
   84 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:89: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]
   89 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:98: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]
   98 |             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 92 ms 102184 KB Output is correct
2 Correct 106 ms 109848 KB Output is correct
3 Correct 51 ms 84052 KB Output is correct
4 Correct 41 ms 84060 KB Output is correct
5 Correct 315 ms 212360 KB Output is correct
6 Correct 545 ms 378664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 52056 KB Output is correct
2 Correct 153 ms 120376 KB Output is correct
3 Correct 184 ms 133608 KB Output is correct
4 Correct 97 ms 102228 KB Output is correct
5 Correct 106 ms 108968 KB Output is correct
6 Correct 7 ms 52060 KB Output is correct
7 Correct 7 ms 52060 KB Output is correct
8 Correct 7 ms 51888 KB Output is correct
9 Correct 8 ms 52104 KB Output is correct
10 Correct 46 ms 84112 KB Output is correct
11 Correct 41 ms 84052 KB Output is correct
12 Correct 116 ms 111004 KB Output is correct
13 Correct 126 ms 121728 KB Output is correct
14 Correct 101 ms 107296 KB Output is correct
15 Correct 107 ms 109332 KB Output is correct
16 Correct 99 ms 107292 KB Output is correct
17 Correct 113 ms 116504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 84048 KB Output is correct
2 Correct 43 ms 84056 KB Output is correct
3 Correct 74 ms 87632 KB Output is correct
4 Correct 61 ms 88400 KB Output is correct
5 Correct 102 ms 96336 KB Output is correct
6 Correct 101 ms 95524 KB Output is correct
7 Correct 109 ms 96336 KB Output is correct
8 Correct 105 ms 96276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 52060 KB Output is correct
2 Correct 7 ms 52060 KB Output is correct
3 Correct 7 ms 52116 KB Output is correct
4 Correct 7 ms 52116 KB Output is correct
5 Correct 7 ms 51908 KB Output is correct
6 Correct 7 ms 52060 KB Output is correct
7 Correct 7 ms 52060 KB Output is correct
8 Correct 7 ms 51868 KB Output is correct
9 Correct 7 ms 52108 KB Output is correct
10 Correct 9 ms 52832 KB Output is correct
11 Correct 8 ms 52316 KB Output is correct
12 Correct 8 ms 52572 KB Output is correct
13 Correct 7 ms 52056 KB Output is correct
14 Correct 8 ms 52316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 52060 KB Output is correct
2 Correct 7 ms 52060 KB Output is correct
3 Correct 7 ms 52116 KB Output is correct
4 Correct 7 ms 52116 KB Output is correct
5 Correct 7 ms 51908 KB Output is correct
6 Correct 7 ms 52060 KB Output is correct
7 Correct 7 ms 52060 KB Output is correct
8 Correct 7 ms 51868 KB Output is correct
9 Correct 7 ms 52108 KB Output is correct
10 Correct 9 ms 52832 KB Output is correct
11 Correct 8 ms 52316 KB Output is correct
12 Correct 8 ms 52572 KB Output is correct
13 Correct 7 ms 52056 KB Output is correct
14 Correct 8 ms 52316 KB Output is correct
15 Correct 8 ms 52316 KB Output is correct
16 Correct 8 ms 52672 KB Output is correct
17 Correct 34 ms 66232 KB Output is correct
18 Correct 34 ms 65360 KB Output is correct
19 Correct 33 ms 65420 KB Output is correct
20 Correct 32 ms 64660 KB Output is correct
21 Correct 33 ms 64604 KB Output is correct
22 Correct 57 ms 76116 KB Output is correct
23 Correct 16 ms 58712 KB Output is correct
24 Correct 30 ms 66136 KB Output is correct
25 Correct 8 ms 52572 KB Output is correct
26 Correct 14 ms 56664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 52060 KB Output is correct
2 Correct 7 ms 52060 KB Output is correct
3 Correct 7 ms 52116 KB Output is correct
4 Correct 7 ms 52116 KB Output is correct
5 Correct 7 ms 51908 KB Output is correct
6 Correct 7 ms 52060 KB Output is correct
7 Correct 7 ms 52060 KB Output is correct
8 Correct 7 ms 51868 KB Output is correct
9 Correct 7 ms 52108 KB Output is correct
10 Correct 9 ms 52832 KB Output is correct
11 Correct 8 ms 52316 KB Output is correct
12 Correct 8 ms 52572 KB Output is correct
13 Correct 7 ms 52056 KB Output is correct
14 Correct 8 ms 52316 KB Output is correct
15 Correct 8 ms 52316 KB Output is correct
16 Correct 8 ms 52672 KB Output is correct
17 Correct 34 ms 66232 KB Output is correct
18 Correct 34 ms 65360 KB Output is correct
19 Correct 33 ms 65420 KB Output is correct
20 Correct 32 ms 64660 KB Output is correct
21 Correct 33 ms 64604 KB Output is correct
22 Correct 57 ms 76116 KB Output is correct
23 Correct 16 ms 58712 KB Output is correct
24 Correct 30 ms 66136 KB Output is correct
25 Correct 8 ms 52572 KB Output is correct
26 Correct 14 ms 56664 KB Output is correct
27 Correct 11 ms 54620 KB Output is correct
28 Correct 151 ms 110680 KB Output is correct
29 Correct 210 ms 137544 KB Output is correct
30 Correct 293 ms 306000 KB Output is correct
31 Correct 292 ms 305724 KB Output is correct
32 Correct 185 ms 136272 KB Output is correct
33 Correct 314 ms 308048 KB Output is correct
34 Correct 327 ms 308048 KB Output is correct
35 Correct 96 ms 108108 KB Output is correct
36 Correct 292 ms 233044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 84048 KB Output is correct
2 Correct 43 ms 84056 KB Output is correct
3 Correct 74 ms 87632 KB Output is correct
4 Correct 61 ms 88400 KB Output is correct
5 Correct 102 ms 96336 KB Output is correct
6 Correct 101 ms 95524 KB Output is correct
7 Correct 109 ms 96336 KB Output is correct
8 Correct 105 ms 96276 KB Output is correct
9 Correct 173 ms 141464 KB Output is correct
10 Correct 87 ms 85844 KB Output is correct
11 Correct 227 ms 119344 KB Output is correct
12 Correct 9 ms 51904 KB Output is correct
13 Correct 8 ms 52060 KB Output is correct
14 Correct 7 ms 52096 KB Output is correct
15 Correct 8 ms 51892 KB Output is correct
16 Correct 8 ms 52112 KB Output is correct
17 Correct 7 ms 52092 KB Output is correct
18 Correct 45 ms 84176 KB Output is correct
19 Correct 46 ms 84132 KB Output is correct
20 Correct 43 ms 83944 KB Output is correct
21 Correct 41 ms 84056 KB Output is correct
22 Correct 305 ms 171688 KB Output is correct
23 Correct 298 ms 159784 KB Output is correct
24 Correct 292 ms 162228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 102184 KB Output is correct
2 Correct 106 ms 109848 KB Output is correct
3 Correct 51 ms 84052 KB Output is correct
4 Correct 41 ms 84060 KB Output is correct
5 Correct 315 ms 212360 KB Output is correct
6 Correct 545 ms 378664 KB Output is correct
7 Correct 7 ms 52056 KB Output is correct
8 Correct 153 ms 120376 KB Output is correct
9 Correct 184 ms 133608 KB Output is correct
10 Correct 97 ms 102228 KB Output is correct
11 Correct 106 ms 108968 KB Output is correct
12 Correct 7 ms 52060 KB Output is correct
13 Correct 7 ms 52060 KB Output is correct
14 Correct 7 ms 51888 KB Output is correct
15 Correct 8 ms 52104 KB Output is correct
16 Correct 46 ms 84112 KB Output is correct
17 Correct 41 ms 84052 KB Output is correct
18 Correct 116 ms 111004 KB Output is correct
19 Correct 126 ms 121728 KB Output is correct
20 Correct 101 ms 107296 KB Output is correct
21 Correct 107 ms 109332 KB Output is correct
22 Correct 99 ms 107292 KB Output is correct
23 Correct 113 ms 116504 KB Output is correct
24 Correct 42 ms 84048 KB Output is correct
25 Correct 43 ms 84056 KB Output is correct
26 Correct 74 ms 87632 KB Output is correct
27 Correct 61 ms 88400 KB Output is correct
28 Correct 102 ms 96336 KB Output is correct
29 Correct 101 ms 95524 KB Output is correct
30 Correct 109 ms 96336 KB Output is correct
31 Correct 105 ms 96276 KB Output is correct
32 Correct 7 ms 52060 KB Output is correct
33 Correct 7 ms 52060 KB Output is correct
34 Correct 7 ms 52116 KB Output is correct
35 Correct 7 ms 52116 KB Output is correct
36 Correct 7 ms 51908 KB Output is correct
37 Correct 7 ms 52060 KB Output is correct
38 Correct 7 ms 52060 KB Output is correct
39 Correct 7 ms 51868 KB Output is correct
40 Correct 7 ms 52108 KB Output is correct
41 Correct 9 ms 52832 KB Output is correct
42 Correct 8 ms 52316 KB Output is correct
43 Correct 8 ms 52572 KB Output is correct
44 Correct 7 ms 52056 KB Output is correct
45 Correct 8 ms 52316 KB Output is correct
46 Correct 8 ms 52316 KB Output is correct
47 Correct 8 ms 52672 KB Output is correct
48 Correct 34 ms 66232 KB Output is correct
49 Correct 34 ms 65360 KB Output is correct
50 Correct 33 ms 65420 KB Output is correct
51 Correct 32 ms 64660 KB Output is correct
52 Correct 33 ms 64604 KB Output is correct
53 Correct 57 ms 76116 KB Output is correct
54 Correct 16 ms 58712 KB Output is correct
55 Correct 30 ms 66136 KB Output is correct
56 Correct 8 ms 52572 KB Output is correct
57 Correct 14 ms 56664 KB Output is correct
58 Correct 11 ms 54620 KB Output is correct
59 Correct 151 ms 110680 KB Output is correct
60 Correct 210 ms 137544 KB Output is correct
61 Correct 293 ms 306000 KB Output is correct
62 Correct 292 ms 305724 KB Output is correct
63 Correct 185 ms 136272 KB Output is correct
64 Correct 314 ms 308048 KB Output is correct
65 Correct 327 ms 308048 KB Output is correct
66 Correct 96 ms 108108 KB Output is correct
67 Correct 292 ms 233044 KB Output is correct
68 Correct 173 ms 141464 KB Output is correct
69 Correct 87 ms 85844 KB Output is correct
70 Correct 227 ms 119344 KB Output is correct
71 Correct 9 ms 51904 KB Output is correct
72 Correct 8 ms 52060 KB Output is correct
73 Correct 7 ms 52096 KB Output is correct
74 Correct 8 ms 51892 KB Output is correct
75 Correct 8 ms 52112 KB Output is correct
76 Correct 7 ms 52092 KB Output is correct
77 Correct 45 ms 84176 KB Output is correct
78 Correct 46 ms 84132 KB Output is correct
79 Correct 43 ms 83944 KB Output is correct
80 Correct 41 ms 84056 KB Output is correct
81 Correct 305 ms 171688 KB Output is correct
82 Correct 298 ms 159784 KB Output is correct
83 Correct 292 ms 162228 KB Output is correct
84 Correct 332 ms 242732 KB Output is correct
85 Correct 324 ms 234820 KB Output is correct
86 Correct 537 ms 379220 KB Output is correct
87 Correct 551 ms 379988 KB Output is correct
88 Correct 7 ms 52060 KB Output is correct
89 Correct 575 ms 380732 KB Output is correct
90 Correct 346 ms 220864 KB Output is correct
91 Correct 256 ms 169808 KB Output is correct