답안 #1058559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1058559 2024-08-14T10:54:18 Z aykhn 메기 농장 (IOI22_fish) C++17
100 / 100
341 ms 155732 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 = 1; i <= N; i++) ind[i] = {0};
    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]));
        }
    }
    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:42: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]
   42 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
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 (k1 < val[i - 1].size() && val[i - 1][k1][0] <= ind[i][j]) lft[i][j] += val[i - 1][k1][1], k1++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
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 (k2 < val[i + 1].size() && val[i + 1][k2][0] <= ind[i][j]) rig[i][j] += val[i + 1][k2][1], k2++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:49: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]
   49 |             while (k3 < val[i].size() && val[i][k3][0] <= ind[i][j]) in[i][j] += val[i][k3][1], k3++;
      |                    ~~~^~~~~~~~~~~~~~~
fish.cpp:56: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]
   56 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
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 (k1 < ind[i - 1].size() && ind[i - 1][k1] <= ind[i][j]) k1++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:62: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]
   62 |             while (k2 < ind[i - 1].size() && ind[i - 1][k2] < ind[i][j]) k2++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:64: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]
   64 |             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: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 (k3 < ind[i - 2].size() && ind[i - 2][k3] <= ind[i][j]) k3++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:67: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]
   67 |             while (k4 < ind[i - 2].size() && ind[i - 2][k4] < ind[i][j]) k4++;
      |                    ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:69: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]
   69 |             long long B = (k4 < ind[i - 2].size() ? suf[1][i - 2][k4] : 0) + rig[i][j];
      |                            ~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:76: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]
   76 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:81: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]
   81 |         for (int j = 0; j < ind[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:90: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]
   90 |             if (j + 1 < ind[i].size()) suf[0][i][j] = suf[0][i][j + 1], suf[1][i][j] = suf[1][i][j + 1];
      |                 ~~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 90812 KB Output is correct
2 Correct 82 ms 96184 KB Output is correct
3 Correct 45 ms 84048 KB Output is correct
4 Correct 45 ms 84048 KB Output is correct
5 Correct 177 ms 144748 KB Output is correct
6 Correct 289 ms 155732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 52060 KB Output is correct
2 Correct 106 ms 107596 KB Output is correct
3 Correct 122 ms 116132 KB Output is correct
4 Correct 73 ms 90904 KB Output is correct
5 Correct 86 ms 96224 KB Output is correct
6 Correct 12 ms 52112 KB Output is correct
7 Correct 12 ms 51888 KB Output is correct
8 Correct 12 ms 52056 KB Output is correct
9 Correct 11 ms 52112 KB Output is correct
10 Correct 45 ms 84180 KB Output is correct
11 Correct 44 ms 84048 KB Output is correct
12 Correct 83 ms 97324 KB Output is correct
13 Correct 92 ms 103980 KB Output is correct
14 Correct 74 ms 94176 KB Output is correct
15 Correct 83 ms 98732 KB Output is correct
16 Correct 80 ms 94628 KB Output is correct
17 Correct 84 ms 98784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 84060 KB Output is correct
2 Correct 45 ms 84052 KB Output is correct
3 Correct 71 ms 84560 KB Output is correct
4 Correct 64 ms 86552 KB Output is correct
5 Correct 96 ms 91180 KB Output is correct
6 Correct 90 ms 91220 KB Output is correct
7 Correct 95 ms 91216 KB Output is correct
8 Correct 94 ms 91256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 52060 KB Output is correct
2 Correct 12 ms 52112 KB Output is correct
3 Correct 12 ms 52056 KB Output is correct
4 Correct 11 ms 52052 KB Output is correct
5 Correct 12 ms 52060 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 51916 KB Output is correct
9 Correct 13 ms 52060 KB Output is correct
10 Correct 12 ms 52540 KB Output is correct
11 Correct 12 ms 52060 KB Output is correct
12 Correct 14 ms 52416 KB Output is correct
13 Correct 12 ms 52148 KB Output is correct
14 Correct 12 ms 52360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 52060 KB Output is correct
2 Correct 12 ms 52112 KB Output is correct
3 Correct 12 ms 52056 KB Output is correct
4 Correct 11 ms 52052 KB Output is correct
5 Correct 12 ms 52060 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 51916 KB Output is correct
9 Correct 13 ms 52060 KB Output is correct
10 Correct 12 ms 52540 KB Output is correct
11 Correct 12 ms 52060 KB Output is correct
12 Correct 14 ms 52416 KB Output is correct
13 Correct 12 ms 52148 KB Output is correct
14 Correct 12 ms 52360 KB Output is correct
15 Correct 11 ms 52056 KB Output is correct
16 Correct 12 ms 52316 KB Output is correct
17 Correct 31 ms 61272 KB Output is correct
18 Correct 27 ms 61336 KB Output is correct
19 Correct 28 ms 61484 KB Output is correct
20 Correct 27 ms 60500 KB Output is correct
21 Correct 26 ms 60592 KB Output is correct
22 Correct 40 ms 69040 KB Output is correct
23 Correct 16 ms 54108 KB Output is correct
24 Correct 26 ms 59108 KB Output is correct
25 Correct 13 ms 52316 KB Output is correct
26 Correct 15 ms 53852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 52060 KB Output is correct
2 Correct 12 ms 52112 KB Output is correct
3 Correct 12 ms 52056 KB Output is correct
4 Correct 11 ms 52052 KB Output is correct
5 Correct 12 ms 52060 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 51916 KB Output is correct
9 Correct 13 ms 52060 KB Output is correct
10 Correct 12 ms 52540 KB Output is correct
11 Correct 12 ms 52060 KB Output is correct
12 Correct 14 ms 52416 KB Output is correct
13 Correct 12 ms 52148 KB Output is correct
14 Correct 12 ms 52360 KB Output is correct
15 Correct 11 ms 52056 KB Output is correct
16 Correct 12 ms 52316 KB Output is correct
17 Correct 31 ms 61272 KB Output is correct
18 Correct 27 ms 61336 KB Output is correct
19 Correct 28 ms 61484 KB Output is correct
20 Correct 27 ms 60500 KB Output is correct
21 Correct 26 ms 60592 KB Output is correct
22 Correct 40 ms 69040 KB Output is correct
23 Correct 16 ms 54108 KB Output is correct
24 Correct 26 ms 59108 KB Output is correct
25 Correct 13 ms 52316 KB Output is correct
26 Correct 15 ms 53852 KB Output is correct
27 Correct 15 ms 53592 KB Output is correct
28 Correct 84 ms 92244 KB Output is correct
29 Correct 129 ms 110156 KB Output is correct
30 Correct 137 ms 123728 KB Output is correct
31 Correct 139 ms 123468 KB Output is correct
32 Correct 110 ms 110420 KB Output is correct
33 Correct 135 ms 123732 KB Output is correct
34 Correct 139 ms 123968 KB Output is correct
35 Correct 60 ms 80424 KB Output is correct
36 Correct 137 ms 117060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 84060 KB Output is correct
2 Correct 45 ms 84052 KB Output is correct
3 Correct 71 ms 84560 KB Output is correct
4 Correct 64 ms 86552 KB Output is correct
5 Correct 96 ms 91180 KB Output is correct
6 Correct 90 ms 91220 KB Output is correct
7 Correct 95 ms 91216 KB Output is correct
8 Correct 94 ms 91256 KB Output is correct
9 Correct 118 ms 102224 KB Output is correct
10 Correct 78 ms 80720 KB Output is correct
11 Correct 151 ms 109568 KB Output is correct
12 Correct 11 ms 52060 KB Output is correct
13 Correct 12 ms 52060 KB Output is correct
14 Correct 13 ms 52112 KB Output is correct
15 Correct 12 ms 52064 KB Output is correct
16 Correct 13 ms 52060 KB Output is correct
17 Correct 12 ms 52060 KB Output is correct
18 Correct 44 ms 84152 KB Output is correct
19 Correct 46 ms 84060 KB Output is correct
20 Correct 46 ms 84300 KB Output is correct
21 Correct 45 ms 84052 KB Output is correct
22 Correct 127 ms 101204 KB Output is correct
23 Correct 193 ms 124756 KB Output is correct
24 Correct 200 ms 129580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 90812 KB Output is correct
2 Correct 82 ms 96184 KB Output is correct
3 Correct 45 ms 84048 KB Output is correct
4 Correct 45 ms 84048 KB Output is correct
5 Correct 177 ms 144748 KB Output is correct
6 Correct 289 ms 155732 KB Output is correct
7 Correct 13 ms 52060 KB Output is correct
8 Correct 106 ms 107596 KB Output is correct
9 Correct 122 ms 116132 KB Output is correct
10 Correct 73 ms 90904 KB Output is correct
11 Correct 86 ms 96224 KB Output is correct
12 Correct 12 ms 52112 KB Output is correct
13 Correct 12 ms 51888 KB Output is correct
14 Correct 12 ms 52056 KB Output is correct
15 Correct 11 ms 52112 KB Output is correct
16 Correct 45 ms 84180 KB Output is correct
17 Correct 44 ms 84048 KB Output is correct
18 Correct 83 ms 97324 KB Output is correct
19 Correct 92 ms 103980 KB Output is correct
20 Correct 74 ms 94176 KB Output is correct
21 Correct 83 ms 98732 KB Output is correct
22 Correct 80 ms 94628 KB Output is correct
23 Correct 84 ms 98784 KB Output is correct
24 Correct 46 ms 84060 KB Output is correct
25 Correct 45 ms 84052 KB Output is correct
26 Correct 71 ms 84560 KB Output is correct
27 Correct 64 ms 86552 KB Output is correct
28 Correct 96 ms 91180 KB Output is correct
29 Correct 90 ms 91220 KB Output is correct
30 Correct 95 ms 91216 KB Output is correct
31 Correct 94 ms 91256 KB Output is correct
32 Correct 11 ms 52060 KB Output is correct
33 Correct 12 ms 52112 KB Output is correct
34 Correct 12 ms 52056 KB Output is correct
35 Correct 11 ms 52052 KB Output is correct
36 Correct 12 ms 52060 KB Output is correct
37 Correct 12 ms 52060 KB Output is correct
38 Correct 12 ms 52056 KB Output is correct
39 Correct 11 ms 51916 KB Output is correct
40 Correct 13 ms 52060 KB Output is correct
41 Correct 12 ms 52540 KB Output is correct
42 Correct 12 ms 52060 KB Output is correct
43 Correct 14 ms 52416 KB Output is correct
44 Correct 12 ms 52148 KB Output is correct
45 Correct 12 ms 52360 KB Output is correct
46 Correct 11 ms 52056 KB Output is correct
47 Correct 12 ms 52316 KB Output is correct
48 Correct 31 ms 61272 KB Output is correct
49 Correct 27 ms 61336 KB Output is correct
50 Correct 28 ms 61484 KB Output is correct
51 Correct 27 ms 60500 KB Output is correct
52 Correct 26 ms 60592 KB Output is correct
53 Correct 40 ms 69040 KB Output is correct
54 Correct 16 ms 54108 KB Output is correct
55 Correct 26 ms 59108 KB Output is correct
56 Correct 13 ms 52316 KB Output is correct
57 Correct 15 ms 53852 KB Output is correct
58 Correct 15 ms 53592 KB Output is correct
59 Correct 84 ms 92244 KB Output is correct
60 Correct 129 ms 110156 KB Output is correct
61 Correct 137 ms 123728 KB Output is correct
62 Correct 139 ms 123468 KB Output is correct
63 Correct 110 ms 110420 KB Output is correct
64 Correct 135 ms 123732 KB Output is correct
65 Correct 139 ms 123968 KB Output is correct
66 Correct 60 ms 80424 KB Output is correct
67 Correct 137 ms 117060 KB Output is correct
68 Correct 118 ms 102224 KB Output is correct
69 Correct 78 ms 80720 KB Output is correct
70 Correct 151 ms 109568 KB Output is correct
71 Correct 11 ms 52060 KB Output is correct
72 Correct 12 ms 52060 KB Output is correct
73 Correct 13 ms 52112 KB Output is correct
74 Correct 12 ms 52064 KB Output is correct
75 Correct 13 ms 52060 KB Output is correct
76 Correct 12 ms 52060 KB Output is correct
77 Correct 44 ms 84152 KB Output is correct
78 Correct 46 ms 84060 KB Output is correct
79 Correct 46 ms 84300 KB Output is correct
80 Correct 45 ms 84052 KB Output is correct
81 Correct 127 ms 101204 KB Output is correct
82 Correct 193 ms 124756 KB Output is correct
83 Correct 200 ms 129580 KB Output is correct
84 Correct 176 ms 139556 KB Output is correct
85 Correct 180 ms 148016 KB Output is correct
86 Correct 327 ms 149588 KB Output is correct
87 Correct 330 ms 152156 KB Output is correct
88 Correct 12 ms 52312 KB Output is correct
89 Correct 341 ms 152148 KB Output is correct
90 Correct 234 ms 149580 KB Output is correct
91 Correct 192 ms 139092 KB Output is correct