Submission #659612

# Submission time Handle Problem Language Result Execution time Memory
659612 2022-11-18T18:13:09 Z peijar Catfish Farm (IOI22_fish) C++17
100 / 100
329 ms 55420 KB
#include "fish.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e18;

int max_weights(signed N, signed M, vector<signed> X, vector<signed> Y,
                vector<signed> W) {
  vector<vector<pair<int, int>>> onCol(N);
  vector<vector<int>> prefSum(N);
  for (int i = 0; i < M; ++i)
    onCol[X[i]].emplace_back(Y[i] + 1, W[i]);
  for (int i = 0; i < N; ++i) {
    sort(onCol[i].begin(), onCol[i].end());
    prefSum[i].resize(onCol[i].size() + 1);
    for (int j = 0; j < (int)onCol[i].size(); ++j)
      prefSum[i][j + 1] = prefSum[i][j] + onCol[i][j].second;
  }

  vector<vector<int>> interesting(N);
  for (int i = 0; i < N; ++i) {
    interesting[i].push_back(0);
    for (auto [y, w] : onCol[i])
      interesting[i].push_back(y);
    if (i)
      for (auto [y, w] : onCol[i - 1])
        interesting[i].push_back(y);
    if (i + 1 < N)
      for (auto [y, w] : onCol[i + 1])
        interesting[i].push_back(y);
    interesting[i].push_back(N);
    sort(interesting[i].begin(), interesting[i].end());
    interesting[i].resize(unique(interesting[i].begin(), interesting[i].end()) -
                          interesting[i].begin());
  }
  array<vector<pair<int, int>>, 2> oldDp, suffixMax;
  for (int i = 0; i < 2; ++i) {
    for (int x : interesting[0]) {
      oldDp[i].emplace_back(x, 0);
      suffixMax[i].emplace_back(x, 0);
    }
  }

  auto eval = [&](int side, int h) {
    auto it = upper_bound(oldDp[side].begin(), oldDp[side].end(), pair(h, INF));
    assert(it != oldDp[side].begin());
    --it;
    assert(it->first == h);
    return it->second;
  };

  auto getSum = [&](int i, int h1, int h2) -> int {
    if (h1 > h2)
      return 0;
    int lo = upper_bound(onCol[i].begin(), onCol[i].end(), pair(h1, INF)) -
             onCol[i].begin();
    int up = upper_bound(onCol[i].begin(), onCol[i].end(), pair(h2, INF)) -
             onCol[i].begin();
    return prefSum[i][up] - prefSum[i][lo];
  };

  int sol = 0;
  for (int i = 1; i < N; ++i) {
    int nbInteressants = interesting[i].size();
    vector<pair<int, int>> dp0, dp1;
    int curOld = 0;
    int cntBefore = interesting[i - 1].size();
    vector<int> prevI(cntBefore);
    vector<int> prevJ(cntBefore);
    int curPos = 0;
    int curSum = 0;
    for (int jHauteur = 0; jHauteur < cntBefore; ++jHauteur) {
      int oldH = interesting[i - 1][jHauteur];
      prevI[jHauteur] = oldDp[1][jHauteur].second;
      while (curPos < (int)onCol[i].size() and onCol[i][curPos].first <= oldH)
        curSum += onCol[i][curPos++].second;
      assert(curSum == getSum(i, 0, oldH));
      prevI[jHauteur] += curSum;
    }
    for (int j = cntBefore - 2; j >= 0; --j)
      prevI[j] = max(prevI[j], prevI[j + 1]);

    curPos = 0, curSum = 0;
    for (int jHauteur = 0; jHauteur < cntBefore; ++jHauteur) {
      int oldH = interesting[i - 1][jHauteur];
      while (curPos < (int)onCol[i - 1].size() and
             onCol[i - 1][curPos].first <= oldH)
        curSum += onCol[i - 1][curPos++].second;
      prevJ[jHauteur] = oldDp[0][jHauteur].second - curSum;
    }
    for (int jHauteur = 1; jHauteur < cntBefore; ++jHauteur)
      prevJ[jHauteur] = max(prevJ[jHauteur - 1], prevJ[jHauteur]);

    int curOld2 = 0;
    for (int iHauteur = 0; iHauteur < nbInteressants; ++iHauteur) {
      int h = interesting[i][iHauteur];
      // 1
      int v1 = 0;
      int v0 = 0;
      while (curOld < cntBefore and interesting[i - 1][curOld] <= h)
        ++curOld;
      assert(curOld);
      v0 = suffixMax[1][curOld - 1].second;
      if (curOld < cntBefore)
        v1 = max(v1, prevI[curOld] - getSum(i, 0, h));
      while (curOld2 < cntBefore and interesting[i - 1][curOld2] < h)
        ++curOld2;
      if (curOld2)
        v0 = max(v0, getSum(i - 1, 0, h) + prevJ[curOld2]);
      /*for (int jHauteur = 0; jHauteur < cntBefore; ++jHauteur) {
        int oldH = interesting[i - 1][jHauteur];
        if (oldH < h)
          v0 = max(v0, getSum(i - 1, oldH, h) + eval(0, oldH));
      }*/
      v1 = max(v1, v0);
      sol = max(sol, v1);
      dp0.emplace_back(h, v0);
      dp1.emplace_back(h, v1);
      // cout << i << ' ' << h << ' ' << v0 << ' ' << v1 << endl;
    }
    oldDp[0] = move(dp0);
    oldDp[1] = move(dp1);
    suffixMax[0] = oldDp[0];
    suffixMax[1] = oldDp[1];
    for (int h = nbInteressants - 2; h >= 0; --h)
      for (int side = 0; side < 2; ++side)
        suffixMax[side][h].second =
            max(suffixMax[side][h].second, suffixMax[side][h + 1].second);
  }
  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:45:8: warning: variable 'eval' set but not used [-Wunused-but-set-variable]
   45 |   auto eval = [&](int side, int h) {
      |        ^~~~
# Verdict Execution time Memory Grader output
1 Correct 84 ms 29888 KB Output is correct
2 Correct 98 ms 35368 KB Output is correct
3 Correct 41 ms 13624 KB Output is correct
4 Correct 42 ms 13524 KB Output is correct
5 Correct 298 ms 55420 KB Output is correct
6 Correct 298 ms 44884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 162 ms 36688 KB Output is correct
3 Correct 196 ms 44596 KB Output is correct
4 Correct 87 ms 31276 KB Output is correct
5 Correct 107 ms 35328 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 296 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 41 ms 13660 KB Output is correct
11 Correct 39 ms 13604 KB Output is correct
12 Correct 99 ms 32744 KB Output is correct
13 Correct 118 ms 36640 KB Output is correct
14 Correct 99 ms 30852 KB Output is correct
15 Correct 126 ms 27812 KB Output is correct
16 Correct 100 ms 31052 KB Output is correct
17 Correct 111 ms 33744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 13560 KB Output is correct
2 Correct 43 ms 13524 KB Output is correct
3 Correct 70 ms 17480 KB Output is correct
4 Correct 66 ms 17132 KB Output is correct
5 Correct 107 ms 23780 KB Output is correct
6 Correct 109 ms 23728 KB Output is correct
7 Correct 101 ms 23752 KB Output is correct
8 Correct 104 ms 23704 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 1 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 1 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 340 KB Output is correct
10 Correct 1 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 1 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 1 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 1 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 340 KB Output is correct
10 Correct 1 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 1 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 2 ms 468 KB Output is correct
17 Correct 28 ms 4048 KB Output is correct
18 Correct 29 ms 4564 KB Output is correct
19 Correct 23 ms 4304 KB Output is correct
20 Correct 23 ms 4288 KB Output is correct
21 Correct 22 ms 4180 KB Output is correct
22 Correct 46 ms 8140 KB Output is correct
23 Correct 8 ms 1236 KB Output is correct
24 Correct 29 ms 3532 KB Output is correct
25 Correct 2 ms 436 KB Output is correct
26 Correct 7 ms 1236 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 1 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 1 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 340 KB Output is correct
10 Correct 1 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 1 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 2 ms 468 KB Output is correct
17 Correct 28 ms 4048 KB Output is correct
18 Correct 29 ms 4564 KB Output is correct
19 Correct 23 ms 4304 KB Output is correct
20 Correct 23 ms 4288 KB Output is correct
21 Correct 22 ms 4180 KB Output is correct
22 Correct 46 ms 8140 KB Output is correct
23 Correct 8 ms 1236 KB Output is correct
24 Correct 29 ms 3532 KB Output is correct
25 Correct 2 ms 436 KB Output is correct
26 Correct 7 ms 1236 KB Output is correct
27 Correct 3 ms 980 KB Output is correct
28 Correct 139 ms 22440 KB Output is correct
29 Correct 220 ms 30840 KB Output is correct
30 Correct 246 ms 33868 KB Output is correct
31 Correct 276 ms 33880 KB Output is correct
32 Correct 163 ms 31840 KB Output is correct
33 Correct 249 ms 33932 KB Output is correct
34 Correct 261 ms 33880 KB Output is correct
35 Correct 89 ms 13040 KB Output is correct
36 Correct 241 ms 32332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 13560 KB Output is correct
2 Correct 43 ms 13524 KB Output is correct
3 Correct 70 ms 17480 KB Output is correct
4 Correct 66 ms 17132 KB Output is correct
5 Correct 107 ms 23780 KB Output is correct
6 Correct 109 ms 23728 KB Output is correct
7 Correct 101 ms 23752 KB Output is correct
8 Correct 104 ms 23704 KB Output is correct
9 Correct 122 ms 23760 KB Output is correct
10 Correct 75 ms 14152 KB Output is correct
11 Correct 155 ms 27872 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 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 42 ms 13620 KB Output is correct
19 Correct 40 ms 13592 KB Output is correct
20 Correct 39 ms 13600 KB Output is correct
21 Correct 40 ms 13576 KB Output is correct
22 Correct 131 ms 21964 KB Output is correct
23 Correct 178 ms 27880 KB Output is correct
24 Correct 178 ms 27936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 29888 KB Output is correct
2 Correct 98 ms 35368 KB Output is correct
3 Correct 41 ms 13624 KB Output is correct
4 Correct 42 ms 13524 KB Output is correct
5 Correct 298 ms 55420 KB Output is correct
6 Correct 298 ms 44884 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 162 ms 36688 KB Output is correct
9 Correct 196 ms 44596 KB Output is correct
10 Correct 87 ms 31276 KB Output is correct
11 Correct 107 ms 35328 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 296 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 41 ms 13660 KB Output is correct
17 Correct 39 ms 13604 KB Output is correct
18 Correct 99 ms 32744 KB Output is correct
19 Correct 118 ms 36640 KB Output is correct
20 Correct 99 ms 30852 KB Output is correct
21 Correct 126 ms 27812 KB Output is correct
22 Correct 100 ms 31052 KB Output is correct
23 Correct 111 ms 33744 KB Output is correct
24 Correct 42 ms 13560 KB Output is correct
25 Correct 43 ms 13524 KB Output is correct
26 Correct 70 ms 17480 KB Output is correct
27 Correct 66 ms 17132 KB Output is correct
28 Correct 107 ms 23780 KB Output is correct
29 Correct 109 ms 23728 KB Output is correct
30 Correct 101 ms 23752 KB Output is correct
31 Correct 104 ms 23704 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 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 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 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 1 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 2 ms 468 KB Output is correct
48 Correct 28 ms 4048 KB Output is correct
49 Correct 29 ms 4564 KB Output is correct
50 Correct 23 ms 4304 KB Output is correct
51 Correct 23 ms 4288 KB Output is correct
52 Correct 22 ms 4180 KB Output is correct
53 Correct 46 ms 8140 KB Output is correct
54 Correct 8 ms 1236 KB Output is correct
55 Correct 29 ms 3532 KB Output is correct
56 Correct 2 ms 436 KB Output is correct
57 Correct 7 ms 1236 KB Output is correct
58 Correct 3 ms 980 KB Output is correct
59 Correct 139 ms 22440 KB Output is correct
60 Correct 220 ms 30840 KB Output is correct
61 Correct 246 ms 33868 KB Output is correct
62 Correct 276 ms 33880 KB Output is correct
63 Correct 163 ms 31840 KB Output is correct
64 Correct 249 ms 33932 KB Output is correct
65 Correct 261 ms 33880 KB Output is correct
66 Correct 89 ms 13040 KB Output is correct
67 Correct 241 ms 32332 KB Output is correct
68 Correct 122 ms 23760 KB Output is correct
69 Correct 75 ms 14152 KB Output is correct
70 Correct 155 ms 27872 KB Output is correct
71 Correct 0 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 0 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 42 ms 13620 KB Output is correct
78 Correct 40 ms 13592 KB Output is correct
79 Correct 39 ms 13600 KB Output is correct
80 Correct 40 ms 13576 KB Output is correct
81 Correct 131 ms 21964 KB Output is correct
82 Correct 178 ms 27880 KB Output is correct
83 Correct 178 ms 27936 KB Output is correct
84 Correct 329 ms 47780 KB Output is correct
85 Correct 327 ms 49552 KB Output is correct
86 Correct 309 ms 43848 KB Output is correct
87 Correct 316 ms 45740 KB Output is correct
88 Correct 0 ms 300 KB Output is correct
89 Correct 322 ms 45664 KB Output is correct
90 Correct 277 ms 45156 KB Output is correct
91 Correct 224 ms 44812 KB Output is correct