답안 #642048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
642048 2022-09-18T10:54:12 Z valerikk Candies (JOI18_candies) C++17
100 / 100
485 ms 16208 KB
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 2e18;

void upd(vector<ll> &a, const vector<ll> &b) {
  if (a.size() < b.size()) {
    a.resize(b.size(), -INF);
  }
  for (int i = 0; i < (int) b.size(); ++i) {
    a[i] = max(a[i], b[i]);
  }
}

void upd(vector<ll> &t, const vector<ll> &a, const vector<ll> &b) {
  int sz_a = a.size(), sz_b = b.size();

  int mn_a = 0;
  while (mn_a < sz_a && a[mn_a] == -INF) {
    ++mn_a;
  }
  
  if (mn_a == sz_a) return;

  int mx_a = mn_a;
  while (mx_a + 1 < sz_a && a[mx_a + 1] != -INF) {
    ++mx_a;
  } 

  int mn_b = 0;
  while (mn_b < sz_b && b[mn_b] == -INF) {
    ++mn_b;
  }
  
  if (mn_b == sz_b) return;

  int mx_b = mn_b;
  while (mx_b + 1 < sz_b && b[mx_b + 1] != -INF) {
    ++mx_b;
  }

  vector<ll> da, db;
  for (int i = mn_a + 1; i <= mx_a; ++i) {
    da.push_back(a[i] - a[i - 1]);
  }
  for (int i = mn_b + 1; i <= mx_b; ++i) {
    db.push_back(b[i] - b[i - 1]);
  }

  vector<ll> d(da.size() + db.size());
  merge(da.begin(), da.end(), db.begin(), db.end(), d.begin(), greater<ll>());

  vector<ll> c(mx_a + mx_b + 1, -INF);
  c[mn_a + mn_b] = a[mn_a] + b[mn_b];
  for (int i = 0; i < mx_a - mn_a + mx_b - mn_b; ++i) {
    c[mn_a + mn_b + i + 1] = c[mn_a + mn_b + i] + d[i];
  }

  if (t.size() < c.size()) {
    t.resize(c.size(), -INF);
  }
  for (int i = 0; i < (int) c.size(); ++i) {
    t[i] = max(t[i], c[i]);
  }
} 

const int N = 2e5 + 7;

typedef array<array<vector<ll>, 2>, 2> Res;

Res merge(const Res &a, const Res &b) {
  Res c;
  for (int al = 0; al < 2; ++al) {
    for (int ar = 0; ar < 2; ++ar) {
      for (int bl = 0; bl < 2 - ar; ++bl) {
        for (int br = 0; br < 2; ++br) {
          upd(c[al][br], a[al][ar], b[bl][br]);
        }
      }
    }
  }
  return c;
}

int n;
ll a[N];

Res solve(int l, int r) {
  if (r - l == 1) {
    Res res;
    res[0][0].resize(1, -INF);
    res[0][0][0] = 0;
    res[1][1].resize(2, -INF);
    res[1][1][1] = a[l];
    return res;
  }
  int mid = (l + r) / 2;
  return merge(solve(l, mid), solve(mid, r));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  auto res = solve(0, n);

  vector<ll> ans((n + 1) / 2 + 1, -INF);
  for (int l = 0; l < 2; ++l) {
    for (int r = 0; r < 2; ++r) {
      upd(ans, res[l][r]);
    }
  }

  for (int i = 1; i <= (n + 1) / 2; ++i) {
    cout << ans[i] << "\n";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 5 ms 468 KB Output is correct
3 Correct 5 ms 424 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 6 ms 468 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 4 ms 468 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 4 ms 536 KB Output is correct
19 Correct 4 ms 468 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 5 ms 468 KB Output is correct
3 Correct 5 ms 424 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 6 ms 468 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 4 ms 468 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 4 ms 536 KB Output is correct
19 Correct 4 ms 468 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 447 ms 13412 KB Output is correct
22 Correct 461 ms 15296 KB Output is correct
23 Correct 478 ms 15356 KB Output is correct
24 Correct 416 ms 15124 KB Output is correct
25 Correct 400 ms 15196 KB Output is correct
26 Correct 485 ms 15128 KB Output is correct
27 Correct 391 ms 15328 KB Output is correct
28 Correct 416 ms 15340 KB Output is correct
29 Correct 420 ms 15328 KB Output is correct
30 Correct 396 ms 15320 KB Output is correct
31 Correct 463 ms 15380 KB Output is correct
32 Correct 407 ms 15340 KB Output is correct
33 Correct 407 ms 15208 KB Output is correct
34 Correct 473 ms 15176 KB Output is correct
35 Correct 385 ms 15156 KB Output is correct
36 Correct 431 ms 16080 KB Output is correct
37 Correct 407 ms 16100 KB Output is correct
38 Correct 428 ms 16148 KB Output is correct
39 Correct 377 ms 16016 KB Output is correct
40 Correct 375 ms 15916 KB Output is correct
41 Correct 371 ms 15972 KB Output is correct
42 Correct 405 ms 16208 KB Output is correct
43 Correct 369 ms 16132 KB Output is correct
44 Correct 369 ms 16088 KB Output is correct
45 Correct 375 ms 16084 KB Output is correct
46 Correct 382 ms 16084 KB Output is correct
47 Correct 397 ms 16084 KB Output is correct
48 Correct 410 ms 15844 KB Output is correct
49 Correct 393 ms 16008 KB Output is correct
50 Correct 397 ms 15872 KB Output is correct