Submission #600242

# Submission time Handle Problem Language Result Execution time Memory
600242 2022-07-20T14:52:53 Z MilosMilutinovic Hacker (BOI15_hac) C++14
100 / 100
157 ms 86708 KB
/**
 *    author:  wxhtzdy
 *    created: 20.07.2022 16:47:00
**/
#include <bits/stdc++.h>

using namespace std;

// usage:
//   auto fun = [&](int i, int j) { return min(i, j); };
//   SparseTable<int, decltype(fun)> st(a, fun);
// or:
//   SparseTable<int> st(a, [&](int i, int j) { return min(i, j); });
template <typename T, class F = function<T(const T&, const T&)>>
class SparseTable {
 public:
  int n;
  vector<vector<T>> mat;
  F func;

  SparseTable(const vector<T>& a, const F& f) : func(f) {
    n = static_cast<int>(a.size());
    int max_log = 32 - __builtin_clz(n);
    mat.resize(max_log);
    mat[0] = a;
    for (int j = 1; j < max_log; j++) {
      mat[j].resize(n - (1 << j) + 1);
      for (int i = 0; i <= n - (1 << j); i++) {
        mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
      }
    }
  }

  T get(int from, int to) const {
    assert(0 <= from && from <= to && to <= n - 1);
    int lg = 32 - __builtin_clz(to - from + 1) - 1;
    return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]);
  }
};

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];
  }
  vector<long long> pref(2 * n + 1);
  for (int i = 0; i < 2 * n; i++) {
    pref[i + 1] = pref[i] + a[i % n];
  }
  int moves = (n + 1) / 2;   
  vector<long long> val(n + 1);
  for (int i = 1; i <= n; i++) {
    val[i] = pref[i + moves - 1] - pref[i - 1];
  }
  SparseTable<long long> st(val, [&](long long a, long long b) { return min(a, b); });
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long mn;
    if (i - moves >= 0) {
      mn = st.get(i - moves + 1, i);      
    } else {
      mn = min(st.get(1, i), st.get(n - (moves - i) + 1, n));
    }
    ans = max(ans, mn);     
  }
  cout << ans << '\n';                                                     
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 2 ms 900 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 852 KB Output is correct
31 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 21 ms 12184 KB Output is correct
5 Correct 46 ms 32664 KB Output is correct
6 Correct 63 ms 41432 KB Output is correct
7 Correct 69 ms 50348 KB Output is correct
8 Correct 116 ms 86568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 2 ms 900 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 852 KB Output is correct
31 Correct 1 ms 852 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 852 KB Output is correct
35 Correct 21 ms 12184 KB Output is correct
36 Correct 46 ms 32664 KB Output is correct
37 Correct 63 ms 41432 KB Output is correct
38 Correct 69 ms 50348 KB Output is correct
39 Correct 116 ms 86568 KB Output is correct
40 Correct 2 ms 1492 KB Output is correct
41 Correct 5 ms 3028 KB Output is correct
42 Correct 9 ms 4556 KB Output is correct
43 Correct 57 ms 32828 KB Output is correct
44 Correct 157 ms 86476 KB Output is correct
45 Correct 22 ms 15692 KB Output is correct
46 Correct 71 ms 50352 KB Output is correct
47 Correct 125 ms 86476 KB Output is correct
48 Correct 120 ms 86708 KB Output is correct
49 Correct 135 ms 85844 KB Output is correct
50 Correct 117 ms 85836 KB Output is correct