Submission #632025

# Submission time Handle Problem Language Result Execution time Memory
632025 2022-08-19T10:22:01 Z timreizin Catfish Farm (IOI22_fish) C++17
100 / 100
174 ms 31800 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include "fish.h"

#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;

using ll = long long;

ll solve(vector<vector<pair<int, ll>>> &fish, int n)
{
    //4 8 5 1 9
    ll res = 0;
    vector<vector<ll>> dp0(fish.size()), dp1(fish.size());
    ll sum = 0;
    for (int i = 0; i < fish.size(); ++i) for (int j = 0; j < fish[i].size(); ++j) sum += fish[i][j].second;
    for (int i = 0; i < fish.size(); ++i)
    {
        fish[i].emplace_back(n, 0);
        dp0[i].resize(fish[i].size());
        ll mx = 0;
        if (i != 0)
        {
            for (int j = 0; j < fish[i - 1].size(); ++j)
            {
                mx = max(mx, dp1[i - 1][j]);
            }
            fill(dp0[i].begin(), dp0[i].end(), mx);
            mx = 0;
        }
        for (int j = 0, k = 0; j < fish[i].size(); ++j)
        {
            if (i != 0)
            {
                while (k < fish[i - 1].size() && fish[i - 1][k].first < fish[i][j].first)
                {
                    mx = max(mx, dp0[i - 1][k]);
                    mx += fish[i - 1][k].second;
                    ++k;
                }
            }
            dp0[i][j] = max(mx, dp0[i][j]);
        }
        dp1[i] = dp0[i];
        if (i != 0)
        {
            ll mx = 0;
            for (int j = (int)fish[i].size() - 1, k = (int)fish[i - 1].size() - 1; j >= 0; --j)
            {
                while (k >= 0 && fish[i - 1][k].first > fish[i][j].first)
                {
                    mx = max(mx, dp1[i - 1][k]);
                    --k;
                }
                mx += fish[i][j].second;
                dp1[i][j] = max(dp1[i][j], mx);
                res = max(dp1[i][j], res);
            }
        }
    }
    return res;
}

ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W)
{
    vector<tuple<int, int, ll>> xyw(m);
    for (int i = 0; i < m; ++i) xyw[i] = {X[i], Y[i], W[i]};
    sort(xyw.begin(), xyw.end());
    ll res = 0;
    vector<vector<pair<int, ll>>> tos;
    int prevX = -1;
    ll sum = 0;
    for (auto [x, y, w] : xyw)
    {
        sum += w;
        if (x > prevX + 1)
        {
            tos.push_back(vector<pair<int, ll>>());
            res += solve(tos, n);
            tos.clear();
            tos.push_back(vector<pair<int, ll>>());
            prevX = x - 1;
        }
        if (x == prevX + 1)
        {
            tos.push_back(vector<pair<int, ll>>());
        }
        tos.back().emplace_back(y, w);
        prevX = x;
    }
    if (prevX != n - 1) tos.push_back(vector<pair<int, ll>>());
    res += solve(tos, n);
    //assert(res <= sum);
    return res;
}
/*
5 6
4 2 1
2 0 2
2 2 4
2 4 8
4 3 16
2 1 32
*/

/*
5 5
0 0 4
1 0 8
2 0 5
3 0 1
4 0 9
*/

Compilation message

fish.cpp: In function 'll solve(std::vector<std::vector<std::pair<int, long long int> > >&, int)':
fish.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < fish.size(); ++i) for (int j = 0; j < fish[i].size(); ++j) sum += fish[i][j].second;
      |                     ~~^~~~~~~~~~~~~
fish.cpp:19:61: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < fish.size(); ++i) for (int j = 0; j < fish[i].size(); ++j) sum += fish[i][j].second;
      |                                                           ~~^~~~~~~~~~~~~~~~
fish.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < fish.size(); ++i)
      |                     ~~^~~~~~~~~~~~~
fish.cpp:27:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |             for (int j = 0; j < fish[i - 1].size(); ++j)
      |                             ~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:34:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for (int j = 0, k = 0; j < fish[i].size(); ++j)
      |                                ~~^~~~~~~~~~~~~~~~
fish.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |                 while (k < fish[i - 1].size() && fish[i - 1][k].first < fish[i][j].first)
      |                        ~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 34 ms 5960 KB Output is correct
2 Correct 43 ms 7252 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 143 ms 15464 KB Output is correct
6 Correct 156 ms 11992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 89 ms 11648 KB Output is correct
3 Correct 89 ms 14400 KB Output is correct
4 Correct 34 ms 5948 KB Output is correct
5 Correct 42 ms 7208 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 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 34 ms 5968 KB Output is correct
13 Correct 45 ms 7260 KB Output is correct
14 Correct 36 ms 5892 KB Output is correct
15 Correct 41 ms 6632 KB Output is correct
16 Correct 44 ms 5972 KB Output is correct
17 Correct 40 ms 6624 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 32 ms 2456 KB Output is correct
4 Correct 23 ms 3492 KB Output is correct
5 Correct 66 ms 22132 KB Output is correct
6 Correct 67 ms 22892 KB Output is correct
7 Correct 71 ms 23420 KB Output is correct
8 Correct 70 ms 23412 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 340 KB Output is correct
10 Correct 2 ms 468 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 340 KB Output is correct
10 Correct 2 ms 468 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 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 21 ms 2868 KB Output is correct
18 Correct 22 ms 3364 KB Output is correct
19 Correct 20 ms 4588 KB Output is correct
20 Correct 20 ms 4612 KB Output is correct
21 Correct 22 ms 4580 KB Output is correct
22 Correct 40 ms 8980 KB Output is correct
23 Correct 6 ms 980 KB Output is correct
24 Correct 15 ms 2896 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 980 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 340 KB Output is correct
10 Correct 2 ms 468 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 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 21 ms 2868 KB Output is correct
18 Correct 22 ms 3364 KB Output is correct
19 Correct 20 ms 4588 KB Output is correct
20 Correct 20 ms 4612 KB Output is correct
21 Correct 22 ms 4580 KB Output is correct
22 Correct 40 ms 8980 KB Output is correct
23 Correct 6 ms 980 KB Output is correct
24 Correct 15 ms 2896 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 980 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 96 ms 15932 KB Output is correct
29 Correct 138 ms 17880 KB Output is correct
30 Correct 147 ms 28492 KB Output is correct
31 Correct 163 ms 28492 KB Output is correct
32 Correct 135 ms 28376 KB Output is correct
33 Correct 141 ms 28492 KB Output is correct
34 Correct 142 ms 27868 KB Output is correct
35 Correct 55 ms 11144 KB Output is correct
36 Correct 164 ms 27656 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 32 ms 2456 KB Output is correct
4 Correct 23 ms 3492 KB Output is correct
5 Correct 66 ms 22132 KB Output is correct
6 Correct 67 ms 22892 KB Output is correct
7 Correct 71 ms 23420 KB Output is correct
8 Correct 70 ms 23412 KB Output is correct
9 Correct 67 ms 22784 KB Output is correct
10 Correct 64 ms 15872 KB Output is correct
11 Correct 126 ms 31800 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 288 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 292 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 280 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 66 ms 5704 KB Output is correct
23 Correct 123 ms 29540 KB Output is correct
24 Correct 130 ms 31172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 5960 KB Output is correct
2 Correct 43 ms 7252 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 143 ms 15464 KB Output is correct
6 Correct 156 ms 11992 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 89 ms 11648 KB Output is correct
9 Correct 89 ms 14400 KB Output is correct
10 Correct 34 ms 5948 KB Output is correct
11 Correct 42 ms 7208 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 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 34 ms 5968 KB Output is correct
19 Correct 45 ms 7260 KB Output is correct
20 Correct 36 ms 5892 KB Output is correct
21 Correct 41 ms 6632 KB Output is correct
22 Correct 44 ms 5972 KB Output is correct
23 Correct 40 ms 6624 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 32 ms 2456 KB Output is correct
27 Correct 23 ms 3492 KB Output is correct
28 Correct 66 ms 22132 KB Output is correct
29 Correct 67 ms 22892 KB Output is correct
30 Correct 71 ms 23420 KB Output is correct
31 Correct 70 ms 23412 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 340 KB Output is correct
41 Correct 2 ms 468 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 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 21 ms 2868 KB Output is correct
49 Correct 22 ms 3364 KB Output is correct
50 Correct 20 ms 4588 KB Output is correct
51 Correct 20 ms 4612 KB Output is correct
52 Correct 22 ms 4580 KB Output is correct
53 Correct 40 ms 8980 KB Output is correct
54 Correct 6 ms 980 KB Output is correct
55 Correct 15 ms 2896 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 4 ms 980 KB Output is correct
58 Correct 2 ms 852 KB Output is correct
59 Correct 96 ms 15932 KB Output is correct
60 Correct 138 ms 17880 KB Output is correct
61 Correct 147 ms 28492 KB Output is correct
62 Correct 163 ms 28492 KB Output is correct
63 Correct 135 ms 28376 KB Output is correct
64 Correct 141 ms 28492 KB Output is correct
65 Correct 142 ms 27868 KB Output is correct
66 Correct 55 ms 11144 KB Output is correct
67 Correct 164 ms 27656 KB Output is correct
68 Correct 67 ms 22784 KB Output is correct
69 Correct 64 ms 15872 KB Output is correct
70 Correct 126 ms 31800 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 288 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 0 ms 292 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 280 KB Output is correct
80 Correct 0 ms 212 KB Output is correct
81 Correct 66 ms 5704 KB Output is correct
82 Correct 123 ms 29540 KB Output is correct
83 Correct 130 ms 31172 KB Output is correct
84 Correct 142 ms 17640 KB Output is correct
85 Correct 142 ms 17732 KB Output is correct
86 Correct 165 ms 13152 KB Output is correct
87 Correct 173 ms 12516 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 174 ms 13380 KB Output is correct
90 Correct 151 ms 12352 KB Output is correct
91 Correct 156 ms 27360 KB Output is correct