Submission #630195

# Submission time Handle Problem Language Result Execution time Memory
630195 2022-08-15T23:38:15 Z ffao Catfish Farm (IOI22_fish) C++17
100 / 100
520 ms 81676 KB
#include "fish.h"

using namespace std;
#include <vector>
#include <algorithm>

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
    vector< vector<int> > Ys_per_col(N);
    
    vector<long long> colsum(N);
    for (int i = 0; i < M; i++) colsum[X[i]] += W[i];
    
    auto find = [&](int x, int y){ return (upper_bound(Ys_per_col[x].begin(), Ys_per_col[x].end(), y) - Ys_per_col[x].begin()) - 1; };
    
    for (int i = 0; i < M; i++) { X.push_back(X[i]); Y.push_back(Y[i]+1); W.push_back(0); }
    for (int i = 0; i < N; i++) { X.push_back(i); Y.push_back(-1); W.push_back(0); }
    for (int i = 0; i < N; i++) { X.push_back(i); Y.push_back(N); W.push_back(0); }
    for (int i = 0; i < X.size(); i++) {
        Ys_per_col[X[i]].push_back(Y[i]);
        if (X[i] > 0) Ys_per_col[X[i]-1].push_back(Y[i]);
        if (X[i]+1 < N) Ys_per_col[X[i]+1].push_back(Y[i]);
    }
    
    for (int i = 0; i < N; i++) {
        sort(Ys_per_col[i].begin(), Ys_per_col[i].end());
        Ys_per_col[i].erase( unique(Ys_per_col[i].begin(), Ys_per_col[i].end()), Ys_per_col[i].end());
    }
    
    vector< vector<int> > WW(N);
    for (int i = 0; i < N; i++) WW[i].resize(Ys_per_col[i].size());
    for (int i = 0; i < M; i++) {
        int idx = find(X[i], Y[i]);
        WW[X[i]][idx] = max(WW[X[i]][idx], W[i]);
    }
    
    vector< vector<long long> > dp[2];
    for (int i = 0; i < 2; i++) {
        dp[i].resize(N);
        for (int j = 0; j < N; j++) dp[i][j].resize(Ys_per_col[j].size());
    }
    
    long long ans = 0;    
    for (int i = 1; i < N; i++) {
        if (i >= 2) dp[0][i][0] = dp[1][i-2][0];
        if (i >= 2) dp[0][i][ Ys_per_col[i].size() - 1 ] = dp[0][i-2][ Ys_per_col[i-2].size() - 1 ] + colsum[i-1];
        
        for (int j = 1; j < (int)Ys_per_col[i].size(); j++) {
            dp[0][i][j] = max(dp[0][i][j], dp[0][i][j-1] + WW[i-1][ find(i-1, Ys_per_col[i][j-1]) ]);
            dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][ find(i-1, Ys_per_col[i][j]) ]);
        }
        
        dp[1][i][ Ys_per_col[i].size() - 1 ] = dp[0][i-1][ Ys_per_col[i-1].size() - 1 ];
        for (int j = (int)Ys_per_col[i].size() - 2; j >= 0; j--) {
            dp[1][i][j] = dp[1][i][j+1] + WW[i][j]; 
            dp[1][i][j] = max( dp[1][i][j], dp[1][i-1][ find(i-1, Ys_per_col[i][j] - 1) + 1 ]);
        }
        
        for (int j = 0; j < (int)Ys_per_col[i].size(); j++) {
            ans = max(ans, dp[0][i][j]);
            ans = max(ans, dp[1][i][j]);
        }
    }
    
    return ans;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 0; i < X.size(); i++) {
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 113 ms 31780 KB Output is correct
2 Correct 137 ms 35972 KB Output is correct
3 Correct 49 ms 26780 KB Output is correct
4 Correct 51 ms 26876 KB Output is correct
5 Correct 430 ms 64988 KB Output is correct
6 Correct 429 ms 81676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 203 ms 38480 KB Output is correct
3 Correct 242 ms 44076 KB Output is correct
4 Correct 120 ms 31868 KB Output is correct
5 Correct 137 ms 35872 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 49 ms 26796 KB Output is correct
11 Correct 47 ms 26800 KB Output is correct
12 Correct 139 ms 34568 KB Output is correct
13 Correct 165 ms 38900 KB Output is correct
14 Correct 139 ms 33240 KB Output is correct
15 Correct 136 ms 34976 KB Output is correct
16 Correct 126 ms 33288 KB Output is correct
17 Correct 149 ms 36740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 26816 KB Output is correct
2 Correct 49 ms 26792 KB Output is correct
3 Correct 86 ms 29980 KB Output is correct
4 Correct 77 ms 30756 KB Output is correct
5 Correct 133 ms 37112 KB Output is correct
6 Correct 167 ms 37136 KB Output is correct
7 Correct 127 ms 37088 KB Output is correct
8 Correct 132 ms 37100 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 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 1 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 1 ms 340 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 468 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 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 1 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 1 ms 340 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 34 ms 4608 KB Output is correct
18 Correct 32 ms 4292 KB Output is correct
19 Correct 34 ms 4240 KB Output is correct
20 Correct 32 ms 4060 KB Output is correct
21 Correct 30 ms 4068 KB Output is correct
22 Correct 66 ms 7744 KB Output is correct
23 Correct 10 ms 1736 KB Output is correct
24 Correct 32 ms 4292 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 7 ms 1620 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 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 1 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 1 ms 340 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 34 ms 4608 KB Output is correct
18 Correct 32 ms 4292 KB Output is correct
19 Correct 34 ms 4240 KB Output is correct
20 Correct 32 ms 4060 KB Output is correct
21 Correct 30 ms 4068 KB Output is correct
22 Correct 66 ms 7744 KB Output is correct
23 Correct 10 ms 1736 KB Output is correct
24 Correct 32 ms 4292 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 7 ms 1620 KB Output is correct
27 Correct 3 ms 1492 KB Output is correct
28 Correct 175 ms 19276 KB Output is correct
29 Correct 298 ms 30380 KB Output is correct
30 Correct 355 ms 54292 KB Output is correct
31 Correct 355 ms 53528 KB Output is correct
32 Correct 228 ms 25804 KB Output is correct
33 Correct 385 ms 56088 KB Output is correct
34 Correct 380 ms 56348 KB Output is correct
35 Correct 98 ms 15680 KB Output is correct
36 Correct 328 ms 42388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 26816 KB Output is correct
2 Correct 49 ms 26792 KB Output is correct
3 Correct 86 ms 29980 KB Output is correct
4 Correct 77 ms 30756 KB Output is correct
5 Correct 133 ms 37112 KB Output is correct
6 Correct 167 ms 37136 KB Output is correct
7 Correct 127 ms 37088 KB Output is correct
8 Correct 132 ms 37100 KB Output is correct
9 Correct 152 ms 40228 KB Output is correct
10 Correct 91 ms 23536 KB Output is correct
11 Correct 215 ms 46764 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 216 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 216 KB Output is correct
18 Correct 50 ms 26816 KB Output is correct
19 Correct 50 ms 26932 KB Output is correct
20 Correct 47 ms 26836 KB Output is correct
21 Correct 51 ms 26788 KB Output is correct
22 Correct 191 ms 44224 KB Output is correct
23 Correct 232 ms 51348 KB Output is correct
24 Correct 237 ms 51764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 31780 KB Output is correct
2 Correct 137 ms 35972 KB Output is correct
3 Correct 49 ms 26780 KB Output is correct
4 Correct 51 ms 26876 KB Output is correct
5 Correct 430 ms 64988 KB Output is correct
6 Correct 429 ms 81676 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 203 ms 38480 KB Output is correct
9 Correct 242 ms 44076 KB Output is correct
10 Correct 120 ms 31868 KB Output is correct
11 Correct 137 ms 35872 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 49 ms 26796 KB Output is correct
17 Correct 47 ms 26800 KB Output is correct
18 Correct 139 ms 34568 KB Output is correct
19 Correct 165 ms 38900 KB Output is correct
20 Correct 139 ms 33240 KB Output is correct
21 Correct 136 ms 34976 KB Output is correct
22 Correct 126 ms 33288 KB Output is correct
23 Correct 149 ms 36740 KB Output is correct
24 Correct 55 ms 26816 KB Output is correct
25 Correct 49 ms 26792 KB Output is correct
26 Correct 86 ms 29980 KB Output is correct
27 Correct 77 ms 30756 KB Output is correct
28 Correct 133 ms 37112 KB Output is correct
29 Correct 167 ms 37136 KB Output is correct
30 Correct 127 ms 37088 KB Output is correct
31 Correct 132 ms 37100 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 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 1 ms 212 KB Output is correct
38 Correct 0 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 2 ms 468 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 2 ms 468 KB Output is correct
48 Correct 34 ms 4608 KB Output is correct
49 Correct 32 ms 4292 KB Output is correct
50 Correct 34 ms 4240 KB Output is correct
51 Correct 32 ms 4060 KB Output is correct
52 Correct 30 ms 4068 KB Output is correct
53 Correct 66 ms 7744 KB Output is correct
54 Correct 10 ms 1736 KB Output is correct
55 Correct 32 ms 4292 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 7 ms 1620 KB Output is correct
58 Correct 3 ms 1492 KB Output is correct
59 Correct 175 ms 19276 KB Output is correct
60 Correct 298 ms 30380 KB Output is correct
61 Correct 355 ms 54292 KB Output is correct
62 Correct 355 ms 53528 KB Output is correct
63 Correct 228 ms 25804 KB Output is correct
64 Correct 385 ms 56088 KB Output is correct
65 Correct 380 ms 56348 KB Output is correct
66 Correct 98 ms 15680 KB Output is correct
67 Correct 328 ms 42388 KB Output is correct
68 Correct 152 ms 40228 KB Output is correct
69 Correct 91 ms 23536 KB Output is correct
70 Correct 215 ms 46764 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 1 ms 216 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 0 ms 216 KB Output is correct
77 Correct 50 ms 26816 KB Output is correct
78 Correct 50 ms 26932 KB Output is correct
79 Correct 47 ms 26836 KB Output is correct
80 Correct 51 ms 26788 KB Output is correct
81 Correct 191 ms 44224 KB Output is correct
82 Correct 232 ms 51348 KB Output is correct
83 Correct 237 ms 51764 KB Output is correct
84 Correct 520 ms 68124 KB Output is correct
85 Correct 490 ms 69668 KB Output is correct
86 Correct 375 ms 78164 KB Output is correct
87 Correct 411 ms 81420 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 395 ms 81320 KB Output is correct
90 Correct 320 ms 64780 KB Output is correct
91 Correct 283 ms 55080 KB Output is correct