Submission #1029263

#TimeUsernameProblemLanguageResultExecution timeMemory
1029263avighnaDischarging (NOI20_discharging)C++17
100 / 100
194 ms196440 KiB
#include <bits/stdc++.h>

typedef long long ll;

class SparseTable {
public:
  ll maxPow;
  std::vector<ll> orig;
  std::vector<std::vector<ll>> table_max;

  SparseTable(const std::vector<ll> &x) : orig(x) {
    const ll n = x.size();
    maxPow = std::floor(std::log2(n));
    table_max = std::vector<std::vector<ll>>(maxPow + 1, std::vector<ll>(n));
    for (ll i = 0; i < n; ++i) {
      table_max[0][i] = x[i];
    }
    for (ll power = 1; power <= maxPow; ++power) {
      for (ll i = 0; i < n - (1 << (power - 1)); ++i) {
        table_max[power][i] =
            std::max(table_max[power - 1][i],
                     table_max[power - 1][i + (1 << (power - 1))]);
      }
    }
  }

  ll query_max(ll a, ll b) {
    if (a == b) {
      return orig[a];
    }
    ll pow_to_use = std::ceil(std::log2(b - a + 1)) - 1;
    return std::max(table_max[pow_to_use][a],
                    table_max[pow_to_use][b - (1 << pow_to_use) + 1]);
  }
};

struct Line {
  ll m, b;

  Line() {}
  Line(ll m, ll b) : m(m), b(b) {}

  ll operator()(ll x) { return m * x + b; }
};

class CHT {
private:
  long double x_intersect(const Line &l1, const Line &l2) {
    return -(l1.b - l2.b) / ((long double)(l1.m - l2.m));
  }

public:
  std::deque<Line> dq;

  void insert(Line l) {
    while (dq.size() > 1 &&
           x_intersect(dq[0], dq[1]) <= x_intersect(dq[0], l)) {
      dq.pop_front();
    }
    dq.push_front(l);
  }

  ll query(ll x) {
    while (dq.size() > 1 && dq[dq.size() - 1](x) <= dq[dq.size() - 2](x)) {
      dq.pop_back();
    }
    return dq.back()(x);
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n;
  std::cin >> n;
  std::vector<ll> a(n), val(n);
  std::vector<bool> active(n);
  for (ll i = 0, mx = 0, prev = -1; i < n; ++i) {
    std::cin >> a[i];
    if (a[i] > mx) {
      mx = a[i];
      val[i] = prev;
      active[i] = true;
      prev = mx;
    }
  }

  std::vector<ll> dp(n);
  SparseTable table(a);
  CHT convex_hull;
  for (ll i = n - 1, j = -1, suff_max = 0; i >= 0; --i) {
    suff_max = std::max(suff_max, a[i]);
    dp[i] = suff_max * (n - i);
    if (!convex_hull.dq.empty()) {
      dp[i] = std::min(dp[i], -convex_hull.query(i));
    }
    if (j != -1) {
      dp[i] = std::min(dp[i], table.query_max(i, j - 1) * (n - i) + dp[j]);
    }
    if (active[i]) {
      if (j != -1) {
        convex_hull.insert({val[j], -dp[j] - val[j] * n});
      }
      j = i;
    }
  }
  std::cout << dp[0] << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...