Submission #628304

# Submission time Handle Problem Language Result Execution time Memory
628304 2022-08-13T09:45:05 Z 600Mihnea Catfish Farm (IOI22_fish) C++17
58 / 100
1000 ms 68536 KB
#include "fish.h"

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

void maxup(ll &a, ll b) {
  a = max(a, b);
}

long long max_weights(int n, int cnt_fish, std::vector<int> XX, std::vector<int> YY, std::vector<int> WW) {
  const ll inf = (ll) 1e18 + 7;
  n += 7;

  vector<set<int>> interestingSet(n);
  vector<vector<int>> interesting(n);
  vector<vector<vector<ll>>> dp(2, vector<vector<ll>> (n));
  vector<vector<ll>> both(n);
  vector<ll> dp_zero(n, 0LL);
  vector<vector<pair<int, ll>>> pe(n);

  assert((int) XX.size() == cnt_fish);
  assert((int) YY.size() == cnt_fish);
  assert((int) WW.size() == cnt_fish);


  for (int i = 0; i < cnt_fish; i++) {
    int x = XX[i] + 5, y = YY[i] + 1, w = WW[i];
    pe[x].push_back({y, w});
    vector<int> xs = {x - 1, x + 1};
    for (auto &i : xs) {
      interestingSet[i].insert(y);
    }
  }

  for (int x = 0; x < n; x++) {
    interestingSet[x].insert(0);
    for (auto &y : interestingSet[x]) {
      interesting[x].push_back(y);
    }
    dp[0][x].resize((int) interesting[x].size(), 0LL);
    dp[1][x].resize((int) interesting[x].size(), 0LL);
    both[x].resize((int) interesting[x].size(), 0LL);

    sort(pe[x].begin(), pe[x].end());
    ll sum = 0;
    for (auto &it : pe[x]) {
      sum += it.second;
      it.second = sum;
    }
  }

  function<ll(int, int)> get = [&] (int x, int y) {
    if (x == n) {
      return 0LL;
    }
    assert(0 <= x && x < n);
    assert(0 <= y && y < n);
    ll sol = 0;
    int low = 0, high = (int) pe[x].size() - 1;

    while (low <= high) {
      int mid = (low + high) / 2;
      if (pe[x][mid].first <= y) {
        sol = pe[x][mid].second;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }

    return sol;
  };


  for (int i = 5; i < n - 2; i++) {
    {
      maxup(dp_zero[i], dp_zero[i - 1]);
      for (int ant_j = 0; ant_j < (int) interesting[i - 1].size(); ant_j++) {
        int h2 = interesting[i - 1][ant_j];
        maxup(dp_zero[i], both[i - 1][ant_j]);
      }
    }
    {
      vector<ll> dp_ant0((int) interesting[i].size());
      {
        int q = 0, w = 0;
        ll Fpref = -inf, Spref = -inf;
        for (int j = 0; j < (int) interesting[i].size(); j++) {
          int h = interesting[i][j];
          while (q < (int) interesting[i - 2].size() && interesting[i - 2][q] < h) {
            maxup(Fpref, both[i - 2][q]);
            q++;
          }
          while (w < (int) interesting[i - 1].size() && interesting[i - 1][w] < h) {
            int h2 = interesting[i - 1][w];
            maxup(Spref, dp[1][i - 1][w] - get(i, h2) - get(i - 1, h2));
            w++;
          }
          maxup(dp_ant0[j], Fpref + get(i + 1, h));
          maxup(dp_ant0[j], dp_zero[i - 2] + get(i - 1, h) + get(i + 1, h));
          maxup(dp[1][i][j], Spref + get(i - 1, h) + get(i + 1, h));
        }
      }
      {
        int q = (int) interesting[i - 2].size(), w = (int) interesting[i - 1].size();
        ll Fsuf = -inf, Ssuf = -inf;
        for (int j = (int) interesting[i].size() - 1; j >= 0; j--) {
          int h = interesting[i][j];
          while (q - 1 >= 0 && interesting[i - 2][q - 1] >= h) {
            q--;
            maxup(Fsuf, both[i - 2][q] + - get(i - 1, interesting[i - 2][q]));
          }
          while (w - 1 >= 0 && interesting[i - 1][w - 1] >= h) {
            w--;
            maxup(Ssuf, both[i - 1][w]);
          }
          ll yy = -inf;
          for (int ant_j = q; ant_j < (int) interesting[i - 2].size(); ant_j++) {
            int h2 = interesting[i - 2][ant_j];
            maxup(yy, both[i - 2][ant_j] + - get(i - 1, h2));
          }
          assert(yy == Fsuf);
          maxup(dp_ant0[j], yy + get(i - 1, h) + get(i + 1, h));
          yy = -inf;
          for (int ant_j = w; ant_j < (int) interesting[i - 1].size(); ant_j++) {
            maxup(yy, both[i - 1][ant_j]);
          }
          assert(yy == Ssuf);
          maxup(dp[0][i][j], yy - get(i, h) + get(i + 1, h));
        }
      }
      for (int j = 0; j < (int) interesting[i].size(); j++) {
        maxup(dp[0][i][j], dp_ant0[j]);
        maxup(dp[1][i][j], dp_ant0[j]);
        both[i][j] = max(dp[0][i][j], dp[1][i][j]);
      }
    }
  }
  ll sol = 0;

  for (int i = 0; i < n; i++) {
    maxup(sol, dp_zero[i]);
    for (int j = 0; j < (int) interesting[i].size(); j++) {
      maxup(sol, both[i][j]);
    }
  }

  return sol;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:82:13: warning: unused variable 'h2' [-Wunused-variable]
   82 |         int h2 = interesting[i - 1][ant_j];
      |             ^~
# Verdict Execution time Memory Grader output
1 Execution timed out 1098 ms 46988 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1084 ms 62384 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 34676 KB Output is correct
2 Correct 60 ms 34752 KB Output is correct
3 Correct 110 ms 37004 KB Output is correct
4 Correct 95 ms 38564 KB Output is correct
5 Correct 164 ms 44920 KB Output is correct
6 Correct 163 ms 44880 KB Output is correct
7 Correct 167 ms 44948 KB Output is correct
8 Correct 168 ms 44888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 211 ms 6748 KB Output is correct
18 Correct 202 ms 6748 KB Output is correct
19 Correct 152 ms 6836 KB Output is correct
20 Correct 115 ms 6268 KB Output is correct
21 Correct 108 ms 6200 KB Output is correct
22 Correct 371 ms 11996 KB Output is correct
23 Correct 22 ms 2004 KB Output is correct
24 Correct 124 ms 5532 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 20 ms 1916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 211 ms 6748 KB Output is correct
18 Correct 202 ms 6748 KB Output is correct
19 Correct 152 ms 6836 KB Output is correct
20 Correct 115 ms 6268 KB Output is correct
21 Correct 108 ms 6200 KB Output is correct
22 Correct 371 ms 11996 KB Output is correct
23 Correct 22 ms 2004 KB Output is correct
24 Correct 124 ms 5532 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 20 ms 1916 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Execution timed out 1054 ms 30572 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 34676 KB Output is correct
2 Correct 60 ms 34752 KB Output is correct
3 Correct 110 ms 37004 KB Output is correct
4 Correct 95 ms 38564 KB Output is correct
5 Correct 164 ms 44920 KB Output is correct
6 Correct 163 ms 44880 KB Output is correct
7 Correct 167 ms 44948 KB Output is correct
8 Correct 168 ms 44888 KB Output is correct
9 Correct 232 ms 49684 KB Output is correct
10 Correct 130 ms 26920 KB Output is correct
11 Correct 351 ms 53544 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 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 60 ms 34736 KB Output is correct
19 Correct 63 ms 34720 KB Output is correct
20 Correct 57 ms 34724 KB Output is correct
21 Correct 60 ms 34680 KB Output is correct
22 Correct 263 ms 50852 KB Output is correct
23 Correct 419 ms 65328 KB Output is correct
24 Correct 420 ms 68536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1098 ms 46988 KB Time limit exceeded
2 Halted 0 ms 0 KB -