Submission #35633

# Submission time Handle Problem Language Result Execution time Memory
35633 2017-11-26T13:32:29 Z szawinis Building Bridges (CEOI17_building) C++14
100 / 100
189 ms 13900 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (1e9)+7, INF = 1e18;
const int MAX = (1e5)+1;

struct line {
  ll m, c;
  mutable function<const line*()> succ;
  ll getVal(ll x) { return m*x + c; }
  bool operator<(const line& rhs) const {
    if(rhs.c != -INF) return m > rhs.m;
    const line* s = succ();
    if(!s) return false;
    return ((double) c - s->c > (double) (s->m - m)*rhs.m);
  }
};

class MinHull : public multiset<line> {
  bool bad(iterator it) {
    auto nxt = next(it);
    if(it == begin()) {
      if(nxt != end()) return nxt->m == it->m && nxt->c <= it->c;
      else return false;
    }
    auto pre = prev(it);
    if(nxt == end()) return pre->m == it->m && pre->c <= it->c;
    line l1 = *pre, l2 = *it, l3 = *nxt;
    return (double) (l3.c-l1.c)*(l1.m-l2.m) <= (double) (l2.c-l1.c)*(l1.m-l3.m);
  }
public:
  void update(ll m, ll c) {
    auto it = insert({m, c});
    it->succ = [=] { return next(it) != end() ? &*next(it) : 0; };
    if(bad(it)) {
      erase(it);
      return;
    }
    while(it != begin() && bad(prev(it))) erase(prev(it));
    while(next(it) != end() && bad(next(it))) erase(next(it));
  }
  ll query(ll x) {
    line l = *lower_bound({x, -INF});
    return l.getVal(x);
  }
} hull;

int n;
ll h[MAX], w[MAX], dp[MAX];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> h[i];
  for(int i = 1; i <= n; i++) {
    cin >> w[i];
    w[i] += w[i-1];
  }
  dp[1] = 0;
  hull.update(-2*h[1], dp[1]+h[1]*h[1] - w[1]);
  for(int i = 2; i <= n; i++) {
    dp[i] = hull.query(h[i]) + h[i]*h[i] + w[i-1];
    hull.update(-2*h[i], dp[i] + h[i]*h[i] - w[i]);
  }
  cout << dp[n];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4528 KB Output is correct
2 Correct 0 ms 4528 KB Output is correct
3 Correct 0 ms 4528 KB Output is correct
4 Correct 0 ms 4528 KB Output is correct
5 Correct 0 ms 4528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 4660 KB Output is correct
2 Correct 103 ms 4660 KB Output is correct
3 Correct 103 ms 4660 KB Output is correct
4 Correct 96 ms 4528 KB Output is correct
5 Correct 113 ms 6376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4528 KB Output is correct
2 Correct 0 ms 4528 KB Output is correct
3 Correct 0 ms 4528 KB Output is correct
4 Correct 0 ms 4528 KB Output is correct
5 Correct 0 ms 4528 KB Output is correct
6 Correct 99 ms 4660 KB Output is correct
7 Correct 103 ms 4660 KB Output is correct
8 Correct 103 ms 4660 KB Output is correct
9 Correct 96 ms 4528 KB Output is correct
10 Correct 113 ms 6376 KB Output is correct
11 Correct 89 ms 4528 KB Output is correct
12 Correct 93 ms 4660 KB Output is correct
13 Correct 63 ms 4528 KB Output is correct
14 Correct 96 ms 4660 KB Output is correct
15 Correct 189 ms 13900 KB Output is correct
16 Correct 103 ms 6376 KB Output is correct
17 Correct 26 ms 4528 KB Output is correct
18 Correct 36 ms 4528 KB Output is correct