Submission #785487

# Submission time Handle Problem Language Result Execution time Memory
785487 2023-07-17T09:34:32 Z thimote75 Catfish Farm (IOI22_fish) C++17
84 / 100
905 ms 54324 KB
#include "fish.h"

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

using num   = long long;
using idata = vector<int>;
using igrid = vector<idata>;
using ndata = vector<num>;
using ngrid = vector<ndata>;
using nmap  = map<int, num>;

const num  INF = 1e18;
const num NINF = -INF;

template <typename U, typename V>
struct MapCumul {
  map<U, V> s;
  V d;
  MapCumul (map<U, V> &m, V start) {
    V h = start;
    d = start;

    for (const auto &X : m) {
      h += X.second;

      s.insert({ X.first, h });
    }
  }
  V get (U x) {
    auto it = s.upper_bound(x);
    if (it == s.begin()) return d;

    it --;
    return (*it).second;
  }
};

struct LocalResult {
  int N;
  map<int, num> ascend, descend;
  // ascend[0] = descend[0]
  // ascend[i] => ascend[j] ifof i < j
  // descend[i] => descend[j] ifof i > j

  LocalResult (int _N) {
    N = _N;

    ascend.insert({ 0, 0 });
  }

  LocalResult next (nmap &weights) {
    LocalResult next(N);

    MapCumul<int, num> cumul(weights, 0);

    vector<int> positions = { 0 };
    for (const auto &data : weights) {
      if (positions[positions.size()] + 1 != data.first)
        positions.push_back(data.first - 1);
      positions.push_back(data.first);
    }

    if (positions[positions.size() - 1] != N)
      positions.push_back(N);

    auto it_ascend = ascend.begin();
    auto it_descend = descend.begin();
    num opt_ascend = NINF;
    num opt_desc_c = NINF;
    num opt_desc   = NINF;
    for (const auto &descend_data : descend) {
      opt_desc_c = max(
        opt_desc_c,
        descend_data.second + cumul.get(descend_data.first)
      );
    }

    for (int i = 1; i <= positions.size(); i ++) {
      num mx  = NINF;
      int pos = positions[i];
      num cml = cumul.get(pos);

      while (it_ascend != ascend.end() && (*it_ascend).first <= pos) {
        int pas = (*it_ascend).first;
        num vas = (*it_ascend).second;
        opt_ascend = max(opt_ascend, vas - cumul.get(pas));
        it_ascend ++;
      }
      
      mx = max(mx, opt_ascend + cml);

      while (it_descend != descend.end() && (*it_descend).first <= pos) {
        int pds = (*it_descend).first;
        num vds = (*it_descend).second;
        opt_desc = max(opt_desc, vds);
        it_descend ++;
      }

      mx = max(mx, opt_desc + cumul.get(pos));
      mx = max(mx, opt_desc_c);

      next.ascend.insert({ pos, mx });
    }

    it_descend = descend.end(); 
    if (it_descend != descend.begin()) it_descend --;
    bool cdescend = it_descend != descend.end();
    num opt_descpc = NINF;
    for (int i = positions.size() - 1; i >= 0; i --) {
      int pos = positions[i];
      num cml = cumul.get(pos);
      num mx  = NINF;

      while (cdescend && (*it_descend).first >= pos) {
        int pas = (*it_descend).first;
        num pbs = (*it_descend).second;

        opt_descpc = max(
          opt_descpc,
          pbs + cumul.get(pas)
        );

        if (it_descend == descend.begin()) cdescend = false;
        else it_descend --;
      }

      mx = max(mx, opt_descpc - cml);

      next.descend.insert({ pos, mx });
    }

    for (const auto &ascend_data : ascend) {
      int pos = ascend_data.first;
      auto it = next.descend.find(pos);
      num res = ascend_data.second;
      if (it != next.descend.end()) {
        const auto f = *it;
        res = max(res, f.second);
        next.descend.erase(pos);
      }
      next.descend.insert({ pos, res });
    }

    return next;
  }
};

ndata cumul (ndata &arr) {
  ndata res (arr.size() + 1, 0);

  for (int i = 0; i < arr.size(); i ++)
    res[i + 1] = res[i] + arr[i];
  
  return res;
}

num max_weights(int N, int M, idata X, idata Y, idata W) {
  LocalResult result (N);

  vector<map<int, num>> grid(N + 1);
  for (int i = 0; i < M; i ++)
    grid[X[i] + 1].insert({ Y[i] + 1, W[i] });

  num res = NINF;
  for (int i = 0; i <= N; i ++) {
    result = result.next( grid[i] );

    if (i == N - 1) {
      for (const auto &u : result.ascend) res = max(res, u.second);
    }
  }
  for (const auto &u : result.descend) res = max(res, u.second);

  return res;
}

Compilation message

fish.cpp: In member function 'LocalResult LocalResult::next(nmap&)':
fish.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i = 1; i <= positions.size(); i ++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:94:13: warning: unused variable 'pds' [-Wunused-variable]
   94 |         int pds = (*it_descend).first;
      |             ^~~
fish.cpp: In function 'ndata cumul(ndata&)':
fish.cpp:152:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |   for (int i = 0; i < arr.size(); i ++)
      |                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 205 ms 28220 KB Output is correct
2 Correct 251 ms 33136 KB Output is correct
3 Correct 31 ms 4948 KB Output is correct
4 Correct 32 ms 4948 KB Output is correct
5 Correct 790 ms 49872 KB Output is correct
6 Correct 438 ms 30828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 427 ms 46472 KB Output is correct
3 Correct 550 ms 54324 KB Output is correct
4 Correct 215 ms 28500 KB Output is correct
5 Correct 260 ms 33252 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 32 ms 4948 KB Output is correct
11 Correct 32 ms 4948 KB Output is correct
12 Correct 214 ms 28484 KB Output is correct
13 Correct 253 ms 33224 KB Output is correct
14 Correct 230 ms 28448 KB Output is correct
15 Correct 265 ms 28408 KB Output is correct
16 Correct 230 ms 28432 KB Output is correct
17 Correct 275 ms 31544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 4948 KB Output is correct
2 Correct 34 ms 4948 KB Output is correct
3 Correct 67 ms 9140 KB Output is correct
4 Correct 55 ms 8036 KB Output is correct
5 Correct 106 ms 13604 KB Output is correct
6 Correct 107 ms 13604 KB Output is correct
7 Correct 117 ms 13612 KB Output is correct
8 Correct 120 ms 13608 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 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 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 2 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 340 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 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 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 2 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 70 ms 4344 KB Output is correct
18 Correct 64 ms 4416 KB Output is correct
19 Correct 55 ms 4324 KB Output is correct
20 Correct 59 ms 4312 KB Output is correct
21 Correct 64 ms 4368 KB Output is correct
22 Correct 130 ms 8276 KB Output is correct
23 Correct 15 ms 1260 KB Output is correct
24 Correct 50 ms 3076 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 13 ms 1108 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 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 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 2 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 70 ms 4344 KB Output is correct
18 Correct 64 ms 4416 KB Output is correct
19 Correct 55 ms 4324 KB Output is correct
20 Correct 59 ms 4312 KB Output is correct
21 Correct 64 ms 4368 KB Output is correct
22 Correct 130 ms 8276 KB Output is correct
23 Correct 15 ms 1260 KB Output is correct
24 Correct 50 ms 3076 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 13 ms 1108 KB Output is correct
27 Correct 3 ms 624 KB Output is correct
28 Correct 379 ms 19300 KB Output is correct
29 Correct 594 ms 27468 KB Output is correct
30 Correct 606 ms 26504 KB Output is correct
31 Correct 623 ms 26516 KB Output is correct
32 Correct 486 ms 26424 KB Output is correct
33 Correct 611 ms 26520 KB Output is correct
34 Correct 596 ms 26600 KB Output is correct
35 Correct 171 ms 10572 KB Output is correct
36 Correct 648 ms 26732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 4948 KB Output is correct
2 Correct 34 ms 4948 KB Output is correct
3 Correct 67 ms 9140 KB Output is correct
4 Correct 55 ms 8036 KB Output is correct
5 Correct 106 ms 13604 KB Output is correct
6 Correct 107 ms 13604 KB Output is correct
7 Correct 117 ms 13612 KB Output is correct
8 Correct 120 ms 13608 KB Output is correct
9 Correct 105 ms 13540 KB Output is correct
10 Correct 82 ms 11256 KB Output is correct
11 Correct 183 ms 22220 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 31 ms 4948 KB Output is correct
19 Correct 31 ms 4948 KB Output is correct
20 Correct 34 ms 4948 KB Output is correct
21 Correct 33 ms 4948 KB Output is correct
22 Correct 158 ms 13504 KB Output is correct
23 Correct 203 ms 22112 KB Output is correct
24 Correct 242 ms 22224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 205 ms 28220 KB Output is correct
2 Correct 251 ms 33136 KB Output is correct
3 Correct 31 ms 4948 KB Output is correct
4 Correct 32 ms 4948 KB Output is correct
5 Correct 790 ms 49872 KB Output is correct
6 Correct 438 ms 30828 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 427 ms 46472 KB Output is correct
9 Correct 550 ms 54324 KB Output is correct
10 Correct 215 ms 28500 KB Output is correct
11 Correct 260 ms 33252 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 32 ms 4948 KB Output is correct
17 Correct 32 ms 4948 KB Output is correct
18 Correct 214 ms 28484 KB Output is correct
19 Correct 253 ms 33224 KB Output is correct
20 Correct 230 ms 28448 KB Output is correct
21 Correct 265 ms 28408 KB Output is correct
22 Correct 230 ms 28432 KB Output is correct
23 Correct 275 ms 31544 KB Output is correct
24 Correct 32 ms 4948 KB Output is correct
25 Correct 34 ms 4948 KB Output is correct
26 Correct 67 ms 9140 KB Output is correct
27 Correct 55 ms 8036 KB Output is correct
28 Correct 106 ms 13604 KB Output is correct
29 Correct 107 ms 13604 KB Output is correct
30 Correct 117 ms 13612 KB Output is correct
31 Correct 120 ms 13608 KB Output is correct
32 Correct 1 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 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 2 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 2 ms 340 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 70 ms 4344 KB Output is correct
49 Correct 64 ms 4416 KB Output is correct
50 Correct 55 ms 4324 KB Output is correct
51 Correct 59 ms 4312 KB Output is correct
52 Correct 64 ms 4368 KB Output is correct
53 Correct 130 ms 8276 KB Output is correct
54 Correct 15 ms 1260 KB Output is correct
55 Correct 50 ms 3076 KB Output is correct
56 Correct 2 ms 336 KB Output is correct
57 Correct 13 ms 1108 KB Output is correct
58 Correct 3 ms 624 KB Output is correct
59 Correct 379 ms 19300 KB Output is correct
60 Correct 594 ms 27468 KB Output is correct
61 Correct 606 ms 26504 KB Output is correct
62 Correct 623 ms 26516 KB Output is correct
63 Correct 486 ms 26424 KB Output is correct
64 Correct 611 ms 26520 KB Output is correct
65 Correct 596 ms 26600 KB Output is correct
66 Correct 171 ms 10572 KB Output is correct
67 Correct 648 ms 26732 KB Output is correct
68 Correct 105 ms 13540 KB Output is correct
69 Correct 82 ms 11256 KB Output is correct
70 Correct 183 ms 22220 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 31 ms 4948 KB Output is correct
78 Correct 31 ms 4948 KB Output is correct
79 Correct 34 ms 4948 KB Output is correct
80 Correct 33 ms 4948 KB Output is correct
81 Correct 158 ms 13504 KB Output is correct
82 Correct 203 ms 22112 KB Output is correct
83 Correct 242 ms 22224 KB Output is correct
84 Correct 905 ms 42644 KB Output is correct
85 Correct 851 ms 44608 KB Output is correct
86 Correct 428 ms 30280 KB Output is correct
87 Correct 480 ms 31072 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 462 ms 30980 KB Output is correct
90 Incorrect 468 ms 31136 KB 1st lines differ - on the 1st token, expected: '109258562863783', found: '109255391578752'
91 Halted 0 ms 0 KB -