Submission #908653

# Submission time Handle Problem Language Result Execution time Memory
908653 2024-01-16T15:57:30 Z vjudge1 Catfish Farm (IOI22_fish) C++17
100 / 100
176 ms 36420 KB
#include <bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)

#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)

enum {
    UP = 0,
    DOWN = 1
};

ll max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W) {
  vector<vector<pair<int, int>>> fishes(N);

  F(i, 0, M) fishes[X[i]].push_back(make_pair(Y[i], i));

  ll largest = 0;
  
  vector<pl> up, down; // stores [ypos, dpvalue] of prev state

  vector<vl> dp(M, vl(2, -1e18)); // dp[fish index][type]

  F(x, 0, N) {
    sort(fishes[x].begin(), fishes[x].end());

    if (x > 0) { // w/o this check, we allow placing piers to index -1 which is no bueno
      ll max_down = 0;
      FD(j, ll(fishes[x].size()) - 1, -1) {
        auto [y, i] = fishes[x][j];
        while (down.size() > 0 && down.back().K > y) {
            max_down = max(max_down, down.back().V);
            down.pop_back();
        }

        ll &ret = dp[i][DOWN];
        ret = largest + W[i];
        ret = max(ret, max_down + W[i]);

        // taking cummulative max 
        if (j + 1 < fishes[x].size()) {
          ret = max(ret, dp[fishes[x][j + 1].V][DOWN] + W[i]);
        }
      }
    }

    if (x >= 1) for (auto [y, i] : fishes[x - 1]) {
        largest = max(largest, dp[i][DOWN]);
    }

    ll max_up = 0;
    for (int j = 0; j < fishes[x].size(); ++j) {
        auto [y, i] = fishes[x][j];
      while (up.size() > 0 && up.back().K < y) {
        max_up = max(max_up, up.back().V);
        up.pop_back();
      }

      ll &ret = dp[i][UP];
      ret = largest + W[i];
      ret = max(ret, max_up + W[i]);
      if (j > 0) {
        ret = max(ret, dp[fishes[x][j - 1].V][UP] + W[i]);
      }
    }

    if (x >= 1) for (auto [y, i] : fishes[x - 1]) {
        largest = max(largest, dp[i][UP]);
    }

    down.clear();
    for (auto [y, i]: fishes[x]) down.emplace_back(y, dp[i][DOWN]);
    up.clear();
    for (auto [y, i]: fishes[x]) up.emplace_back(y, dp[i][UP]);
    reverse(A(up));
  }

  ll answer = 0;
  F(i, 0, M) {
    answer = max(answer, dp[i][DOWN]);
    if (X[i] + 1 < N) answer = max(answer, dp[i][UP]);
  }

  return answer;
}

Compilation message

fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:59:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         if (j + 1 < fishes[x].size()) {
      |             ~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for (int j = 0; j < fishes[x].size(); ++j) {
      |                     ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 37 ms 13904 KB Output is correct
2 Correct 47 ms 16932 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 3 ms 2740 KB Output is correct
5 Correct 175 ms 36420 KB Output is correct
6 Correct 144 ms 33624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 83 ms 20840 KB Output is correct
3 Correct 99 ms 26684 KB Output is correct
4 Correct 37 ms 14660 KB Output is correct
5 Correct 44 ms 16912 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 3 ms 2652 KB Output is correct
12 Correct 36 ms 14696 KB Output is correct
13 Correct 45 ms 17132 KB Output is correct
14 Correct 39 ms 12504 KB Output is correct
15 Correct 40 ms 13632 KB Output is correct
16 Correct 35 ms 12616 KB Output is correct
17 Correct 38 ms 13692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 24 ms 8280 KB Output is correct
4 Correct 18 ms 7516 KB Output is correct
5 Correct 50 ms 14680 KB Output is correct
6 Correct 40 ms 14420 KB Output is correct
7 Correct 45 ms 14676 KB Output is correct
8 Correct 46 ms 14680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 17 ms 4956 KB Output is correct
18 Correct 17 ms 4956 KB Output is correct
19 Correct 17 ms 4956 KB Output is correct
20 Correct 19 ms 4952 KB Output is correct
21 Correct 15 ms 4952 KB Output is correct
22 Correct 31 ms 9552 KB Output is correct
23 Correct 4 ms 1368 KB Output is correct
24 Correct 11 ms 3420 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 4 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 17 ms 4956 KB Output is correct
18 Correct 17 ms 4956 KB Output is correct
19 Correct 17 ms 4956 KB Output is correct
20 Correct 19 ms 4952 KB Output is correct
21 Correct 15 ms 4952 KB Output is correct
22 Correct 31 ms 9552 KB Output is correct
23 Correct 4 ms 1368 KB Output is correct
24 Correct 11 ms 3420 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 4 ms 1116 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 87 ms 22348 KB Output is correct
29 Correct 176 ms 30456 KB Output is correct
30 Correct 126 ms 30244 KB Output is correct
31 Correct 126 ms 30032 KB Output is correct
32 Correct 123 ms 31312 KB Output is correct
33 Correct 123 ms 30032 KB Output is correct
34 Correct 127 ms 30032 KB Output is correct
35 Correct 43 ms 12272 KB Output is correct
36 Correct 124 ms 30696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 24 ms 8280 KB Output is correct
4 Correct 18 ms 7516 KB Output is correct
5 Correct 50 ms 14680 KB Output is correct
6 Correct 40 ms 14420 KB Output is correct
7 Correct 45 ms 14676 KB Output is correct
8 Correct 46 ms 14680 KB Output is correct
9 Correct 44 ms 14656 KB Output is correct
10 Correct 40 ms 11856 KB Output is correct
11 Correct 100 ms 23616 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 3 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 48 ms 13772 KB Output is correct
23 Correct 102 ms 23604 KB Output is correct
24 Correct 100 ms 23876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 13904 KB Output is correct
2 Correct 47 ms 16932 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 3 ms 2740 KB Output is correct
5 Correct 175 ms 36420 KB Output is correct
6 Correct 144 ms 33624 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 83 ms 20840 KB Output is correct
9 Correct 99 ms 26684 KB Output is correct
10 Correct 37 ms 14660 KB Output is correct
11 Correct 44 ms 16912 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 3 ms 2652 KB Output is correct
18 Correct 36 ms 14696 KB Output is correct
19 Correct 45 ms 17132 KB Output is correct
20 Correct 39 ms 12504 KB Output is correct
21 Correct 40 ms 13632 KB Output is correct
22 Correct 35 ms 12616 KB Output is correct
23 Correct 38 ms 13692 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 24 ms 8280 KB Output is correct
27 Correct 18 ms 7516 KB Output is correct
28 Correct 50 ms 14680 KB Output is correct
29 Correct 40 ms 14420 KB Output is correct
30 Correct 45 ms 14676 KB Output is correct
31 Correct 46 ms 14680 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 600 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 17 ms 4956 KB Output is correct
49 Correct 17 ms 4956 KB Output is correct
50 Correct 17 ms 4956 KB Output is correct
51 Correct 19 ms 4952 KB Output is correct
52 Correct 15 ms 4952 KB Output is correct
53 Correct 31 ms 9552 KB Output is correct
54 Correct 4 ms 1368 KB Output is correct
55 Correct 11 ms 3420 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 4 ms 1116 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
59 Correct 87 ms 22348 KB Output is correct
60 Correct 176 ms 30456 KB Output is correct
61 Correct 126 ms 30244 KB Output is correct
62 Correct 126 ms 30032 KB Output is correct
63 Correct 123 ms 31312 KB Output is correct
64 Correct 123 ms 30032 KB Output is correct
65 Correct 127 ms 30032 KB Output is correct
66 Correct 43 ms 12272 KB Output is correct
67 Correct 124 ms 30696 KB Output is correct
68 Correct 44 ms 14656 KB Output is correct
69 Correct 40 ms 11856 KB Output is correct
70 Correct 100 ms 23616 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 344 KB Output is correct
74 Correct 1 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 1 ms 348 KB Output is correct
77 Correct 3 ms 2652 KB Output is correct
78 Correct 2 ms 2652 KB Output is correct
79 Correct 2 ms 2652 KB Output is correct
80 Correct 2 ms 2652 KB Output is correct
81 Correct 48 ms 13772 KB Output is correct
82 Correct 102 ms 23604 KB Output is correct
83 Correct 100 ms 23876 KB Output is correct
84 Correct 143 ms 32816 KB Output is correct
85 Correct 144 ms 32876 KB Output is correct
86 Correct 160 ms 33548 KB Output is correct
87 Correct 174 ms 33892 KB Output is correct
88 Correct 0 ms 344 KB Output is correct
89 Correct 174 ms 34128 KB Output is correct
90 Correct 143 ms 32868 KB Output is correct
91 Correct 140 ms 32596 KB Output is correct