Submission #628221

# Submission time Handle Problem Language Result Execution time Memory
628221 2022-08-13T08:21:04 Z MarkBcc168 Catfish Farm (IOI22_fish) C++17
100 / 100
146 ms 20836 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y, std::vector<int> w){
    vector<ll> col[n];
    for(int i=0; i<m; ++i){
        col[x[i]].push_back(i);
    }
    for(int i=0; i<n; ++i){
        sort(col[i].begin(), col[i].end(),
            [&](int p, int q){return y[p] < y[q];});
    }
    vector<ll> up(m,0), down(m,0);
    vector<ll> up_max(n,0), down_max(n,0);
    for(int i=0; i<n; ++i){
        if(col[i].size() == 0){
            if(i==0){
                up_max[i] = 0;
                down_max[i] = 0;
            }
            else{
                up_max[i] = up_max[i-1];
                down_max[i] = down_max[i-1];
            }
            continue;
        }
            
        //case 1: delete (i-1)-st column and start over
        ll mx = (i<2) ? 0LL : max(up_max[i-2], down_max[i-1]);
        for(int j=0; j<col[i].size(); ++j){
            if(j==0) up[col[i][0]] = mx + w[col[i][0]];
            else{
                up[col[i][j]] = up[col[i][j-1]] + w[col[i][j]];
            }
        }
        if(i > 0){
            mx = (i<2) ? 0LL : max(up_max[i-2], down_max[i-2]);
            for(int j=col[i].size()-1; j>=0; --j){
                if(j==col[i].size()-1) down[col[i][j]] = mx + w[col[i][j]];
                else{
                    down[col[i][j]] = down[col[i][j+1]] + w[col[i][j]];
                }
            }
        }
        
        //case 2: actually using the previous column
        if(i>0 && col[i-1].size() > 0){
            int p, x = col[i-1].size();
            p = -1; 
            for(int j=0; j<col[i].size(); j++){
                while(p < x-1 && y[col[i-1][p+1]] < y[col[i][j]]) p++;
                if(p==-1) continue;
                if(j==0) up[col[i][j]] = max(up[col[i][j]], up[col[i-1][p]] + w[col[i][j]]);
                else{
                    up[col[i][j]] = max({up[col[i][j]],
                        up[col[i-1][p]] + w[col[i][j]],
                        up[col[i][j-1]] + w[col[i][j]]});
                }
            }
            p = x;
            if(i>1){
                for(int j=col[i].size()-1; j>=0; j--){
                    while(p > 0 && y[col[i-1][p-1]] > y[col[i][j]]) p--;
                    if(p==x) continue;
                    if(j==col[i].size()-1) down[col[i][j]] = max(down[col[i][j]], down[col[i-1][p]] + w[col[i][j]]);
                    else{
                        down[col[i][j]] = max(down[col[i][j]],
                            max(down[col[i-1][p]],down[col[i][j+1]]) + w[col[i][j]]);
                    }
                }
            }
        }
        if(i==0){
            up_max[i] = up[col[i][col[i].size()-1]];
            down_max[i] = down[col[i][0]];
        }
        else{
            up_max[i] = max(up_max[i-1], up[col[i][col[i].size()-1]]);
            down_max[i] = max(down_max[i-1], down[col[i][0]]);
        }
    }
    return max(up_max[n-2], down_max[n-1]);
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for(int j=0; j<col[i].size(); ++j){
      |                      ~^~~~~~~~~~~~~~
fish.cpp:40:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |                 if(j==col[i].size()-1) down[col[i][j]] = mx + w[col[i][j]];
      |                    ~^~~~~~~~~~~~~~~~~
fish.cpp:51:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |             for(int j=0; j<col[i].size(); j++){
      |                          ~^~~~~~~~~~~~~~
fish.cpp:66:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |                     if(j==col[i].size()-1) down[col[i][j]] = max(down[col[i][j]], down[col[i-1][p]] + w[col[i][j]]);
      |                        ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 7632 KB Output is correct
2 Correct 44 ms 9004 KB Output is correct
3 Correct 3 ms 4180 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 115 ms 18508 KB Output is correct
6 Correct 127 ms 20300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 64 ms 11576 KB Output is correct
3 Correct 78 ms 13768 KB Output is correct
4 Correct 32 ms 7744 KB Output is correct
5 Correct 40 ms 8932 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 3 ms 4180 KB Output is correct
11 Correct 3 ms 4180 KB Output is correct
12 Correct 34 ms 7652 KB Output is correct
13 Correct 39 ms 9016 KB Output is correct
14 Correct 33 ms 7680 KB Output is correct
15 Correct 37 ms 8648 KB Output is correct
16 Correct 33 ms 7780 KB Output is correct
17 Correct 40 ms 8640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4180 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 29 ms 7508 KB Output is correct
4 Correct 18 ms 6752 KB Output is correct
5 Correct 50 ms 11212 KB Output is correct
6 Correct 41 ms 11260 KB Output is correct
7 Correct 45 ms 11196 KB Output is correct
8 Correct 46 ms 11204 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 0 ms 212 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 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 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 0 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 19 ms 2512 KB Output is correct
18 Correct 15 ms 2752 KB Output is correct
19 Correct 15 ms 2772 KB Output is correct
20 Correct 14 ms 2772 KB Output is correct
21 Correct 14 ms 2772 KB Output is correct
22 Correct 30 ms 5128 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 10 ms 1812 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 724 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 0 ms 212 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 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 19 ms 2512 KB Output is correct
18 Correct 15 ms 2752 KB Output is correct
19 Correct 15 ms 2772 KB Output is correct
20 Correct 14 ms 2772 KB Output is correct
21 Correct 14 ms 2772 KB Output is correct
22 Correct 30 ms 5128 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 10 ms 1812 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 98 ms 11916 KB Output is correct
29 Correct 110 ms 15948 KB Output is correct
30 Correct 108 ms 15456 KB Output is correct
31 Correct 108 ms 15460 KB Output is correct
32 Correct 106 ms 16864 KB Output is correct
33 Correct 109 ms 15476 KB Output is correct
34 Correct 113 ms 15456 KB Output is correct
35 Correct 39 ms 6540 KB Output is correct
36 Correct 110 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4180 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 29 ms 7508 KB Output is correct
4 Correct 18 ms 6752 KB Output is correct
5 Correct 50 ms 11212 KB Output is correct
6 Correct 41 ms 11260 KB Output is correct
7 Correct 45 ms 11196 KB Output is correct
8 Correct 46 ms 11204 KB Output is correct
9 Correct 56 ms 11212 KB Output is correct
10 Correct 52 ms 7616 KB Output is correct
11 Correct 96 ms 15072 KB Output is correct
12 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 3 ms 4180 KB Output is correct
19 Correct 3 ms 4180 KB Output is correct
20 Correct 2 ms 4180 KB Output is correct
21 Correct 3 ms 4180 KB Output is correct
22 Correct 45 ms 10188 KB Output is correct
23 Correct 86 ms 15068 KB Output is correct
24 Correct 87 ms 15168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 7632 KB Output is correct
2 Correct 44 ms 9004 KB Output is correct
3 Correct 3 ms 4180 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 115 ms 18508 KB Output is correct
6 Correct 127 ms 20300 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 64 ms 11576 KB Output is correct
9 Correct 78 ms 13768 KB Output is correct
10 Correct 32 ms 7744 KB Output is correct
11 Correct 40 ms 8932 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 1 ms 212 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 3 ms 4180 KB Output is correct
18 Correct 34 ms 7652 KB Output is correct
19 Correct 39 ms 9016 KB Output is correct
20 Correct 33 ms 7680 KB Output is correct
21 Correct 37 ms 8648 KB Output is correct
22 Correct 33 ms 7780 KB Output is correct
23 Correct 40 ms 8640 KB Output is correct
24 Correct 3 ms 4180 KB Output is correct
25 Correct 3 ms 4180 KB Output is correct
26 Correct 29 ms 7508 KB Output is correct
27 Correct 18 ms 6752 KB Output is correct
28 Correct 50 ms 11212 KB Output is correct
29 Correct 41 ms 11260 KB Output is correct
30 Correct 45 ms 11196 KB Output is correct
31 Correct 46 ms 11204 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 0 ms 212 KB Output is correct
37 Correct 0 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 212 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 19 ms 2512 KB Output is correct
49 Correct 15 ms 2752 KB Output is correct
50 Correct 15 ms 2772 KB Output is correct
51 Correct 14 ms 2772 KB Output is correct
52 Correct 14 ms 2772 KB Output is correct
53 Correct 30 ms 5128 KB Output is correct
54 Correct 3 ms 724 KB Output is correct
55 Correct 10 ms 1812 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 3 ms 724 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 98 ms 11916 KB Output is correct
60 Correct 110 ms 15948 KB Output is correct
61 Correct 108 ms 15456 KB Output is correct
62 Correct 108 ms 15460 KB Output is correct
63 Correct 106 ms 16864 KB Output is correct
64 Correct 109 ms 15476 KB Output is correct
65 Correct 113 ms 15456 KB Output is correct
66 Correct 39 ms 6540 KB Output is correct
67 Correct 110 ms 16364 KB Output is correct
68 Correct 56 ms 11212 KB Output is correct
69 Correct 52 ms 7616 KB Output is correct
70 Correct 96 ms 15072 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 3 ms 4180 KB Output is correct
78 Correct 3 ms 4180 KB Output is correct
79 Correct 2 ms 4180 KB Output is correct
80 Correct 3 ms 4180 KB Output is correct
81 Correct 45 ms 10188 KB Output is correct
82 Correct 86 ms 15068 KB Output is correct
83 Correct 87 ms 15168 KB Output is correct
84 Correct 124 ms 18596 KB Output is correct
85 Correct 122 ms 18828 KB Output is correct
86 Correct 133 ms 20280 KB Output is correct
87 Correct 146 ms 20816 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 137 ms 20836 KB Output is correct
90 Correct 122 ms 20312 KB Output is correct
91 Correct 113 ms 20064 KB Output is correct