Submission #977726

# Submission time Handle Problem Language Result Execution time Memory
977726 2024-05-08T10:06:59 Z mannshah1211 Candies (JOI18_candies) C++17
100 / 100
4624 ms 22660 KB
/**
 *  author: hashman
 *  created:
**/

// Thanks Dominater069 for the idea of solution!

#pragma GCC target("avx2")
#pragma GCC optimize("fast-math")

#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() {
  cerr << endl;
}

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

using Int = long long;

const Int inf = 1e18;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  auto solve = [&](auto&& self, int l, int r) -> vector<vector<Int>> {
    int sz = r - l + 1, till = (sz + 1) / 2;
    vector<vector<Int>> dp(till + 1, vector<Int>(4, -inf));
    if (l == r) {
      dp[0][0] = 0;
      dp[1][3] = a[l];
      return dp;
    }
    int m = (l + r) / 2;
    vector<vector<Int>> dpl = self(self, l, m), dpr = self(self, m + 1, r);
    for (int i = 0; i <= till; i++) {
      for (int ll = 0; ll < 2; ll++) {
        for (int lr = 0; lr < 2; lr++) {
          for (int rl = 0; rl < 2; rl++) {
            for (int rr = 0; rr < 2; rr++) {
              if (rl && lr) {
                continue;
              }
              int curo = ll + 2 * rr, lfo = ll + 2 * lr, rgo = rl + 2 * rr;
              int low = 0, high = i;
              while (high - low > 2) {
                int mid_1 = (2 * low + high) / 3, mid_2 = (low + 2 * high) / 3;
                if (mid_2 >= int(dpl.size())) {
                  high = mid_2;
                  continue;
                }
                if (i - mid_1 >= int(dpr.size())) {
                  low = mid_1;
                  continue;
                }
                Int candl = dpl[mid_1][lfo] + dpr[i - mid_1][rgo], candh = dpl[mid_2][lfo] + dpr[i - mid_2][rgo];
                if (candl > candh) {
                  high = mid_2;
                } else {
                  low = mid_1;
                }
              }
              for (int op = low; op <= high; op++) {
                int a = op, b = i - op;
                if (a >= int(dpl.size()) || b >= int(dpr.size())) {
                  continue;
                }
                dp[i][curo] = max(dp[i][curo], dpl[a][lfo] + dpr[b][rgo]);
              }
            }
          }
        }
      }
    }
    return dp;
  };
  vector<vector<Int>> dp = solve(solve, 0, n - 1);
  for (int i = 1; i <= (n + 1) / 2; i++) {
    cout << max({dp[i][0], dp[i][1], dp[i][2], dp[i][3]}) << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 604 KB Output is correct
2 Correct 17 ms 604 KB Output is correct
3 Correct 18 ms 600 KB Output is correct
4 Correct 19 ms 772 KB Output is correct
5 Correct 20 ms 604 KB Output is correct
6 Correct 17 ms 604 KB Output is correct
7 Correct 18 ms 600 KB Output is correct
8 Correct 17 ms 660 KB Output is correct
9 Correct 17 ms 604 KB Output is correct
10 Correct 19 ms 604 KB Output is correct
11 Correct 18 ms 604 KB Output is correct
12 Correct 21 ms 600 KB Output is correct
13 Correct 17 ms 672 KB Output is correct
14 Correct 17 ms 620 KB Output is correct
15 Correct 17 ms 600 KB Output is correct
16 Correct 20 ms 608 KB Output is correct
17 Correct 17 ms 604 KB Output is correct
18 Correct 17 ms 680 KB Output is correct
19 Correct 17 ms 604 KB Output is correct
20 Correct 18 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 604 KB Output is correct
2 Correct 17 ms 604 KB Output is correct
3 Correct 18 ms 600 KB Output is correct
4 Correct 19 ms 772 KB Output is correct
5 Correct 20 ms 604 KB Output is correct
6 Correct 17 ms 604 KB Output is correct
7 Correct 18 ms 600 KB Output is correct
8 Correct 17 ms 660 KB Output is correct
9 Correct 17 ms 604 KB Output is correct
10 Correct 19 ms 604 KB Output is correct
11 Correct 18 ms 604 KB Output is correct
12 Correct 21 ms 600 KB Output is correct
13 Correct 17 ms 672 KB Output is correct
14 Correct 17 ms 620 KB Output is correct
15 Correct 17 ms 600 KB Output is correct
16 Correct 20 ms 608 KB Output is correct
17 Correct 17 ms 604 KB Output is correct
18 Correct 17 ms 680 KB Output is correct
19 Correct 17 ms 604 KB Output is correct
20 Correct 18 ms 600 KB Output is correct
21 Correct 4341 ms 22476 KB Output is correct
22 Correct 4309 ms 22632 KB Output is correct
23 Correct 4413 ms 22528 KB Output is correct
24 Correct 4330 ms 22520 KB Output is correct
25 Correct 4411 ms 22456 KB Output is correct
26 Correct 4415 ms 22492 KB Output is correct
27 Correct 4469 ms 22468 KB Output is correct
28 Correct 4437 ms 22476 KB Output is correct
29 Correct 4608 ms 22484 KB Output is correct
30 Correct 4624 ms 22472 KB Output is correct
31 Correct 4387 ms 22648 KB Output is correct
32 Correct 4424 ms 22528 KB Output is correct
33 Correct 4467 ms 22484 KB Output is correct
34 Correct 4539 ms 22472 KB Output is correct
35 Correct 4463 ms 22460 KB Output is correct
36 Correct 4362 ms 22464 KB Output is correct
37 Correct 4304 ms 22472 KB Output is correct
38 Correct 4317 ms 22476 KB Output is correct
39 Correct 4470 ms 22480 KB Output is correct
40 Correct 4458 ms 22484 KB Output is correct
41 Correct 4529 ms 22660 KB Output is correct
42 Correct 4477 ms 22480 KB Output is correct
43 Correct 4473 ms 22588 KB Output is correct
44 Correct 4365 ms 22656 KB Output is correct
45 Correct 4340 ms 22468 KB Output is correct
46 Correct 4496 ms 22576 KB Output is correct
47 Correct 4366 ms 22448 KB Output is correct
48 Correct 4405 ms 22488 KB Output is correct
49 Correct 4314 ms 22480 KB Output is correct
50 Correct 4521 ms 22600 KB Output is correct