Submission #546946

# Submission time Handle Problem Language Result Execution time Memory
546946 2022-04-09T04:11:40 Z Jomnoi Building Bridges (CEOI17_building) C++17
100 / 100
53 ms 9760 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int MAX_N = 1e5 + 10;
const long long INF = LLONG_MAX;

long long H[MAX_N], W[MAX_N];
long long dp[MAX_N];

bool QueryMode;

long long divide(const long long &a, const long long &b) {
    return a / b - ((a ^ b) < 0 and a % b != 0);
}

class Line {
public :
    mutable long long m, c, p;
    Line(const long long &m_, const long long &c_) : m(m_), c(c_), p(0) {}
    Line(const long long &m_, const long long &c_, const long long &p_) : m(m_), c(c_), p(p_) {}

    bool operator<(const Line &o) const {
        if(QueryMode == true) {
            return p < o.p;
        }
        return m < o.m;
    }
};

class LineContainer : multiset <Line> {
public :
    bool intersect(iterator now, iterator nxt) {
        if(nxt == end()) {
            now->p = INF;
            return false;
        }

        if(now->m == nxt->m) {
            now->p = INF;
            if(now->c < nxt->c) {
                now->p = -INF;
            }
        }
        else {
            now->p = divide(nxt->c - now->c, now->m - nxt->m);
        }
        return now->p >= nxt->p;
    }

    void addline(const Line &f) {
        auto nxt = insert(f), now = nxt++, prv = now;
        while(intersect(now, nxt) == true) {
            nxt = erase(nxt);
        }
        if(prv != begin() and intersect(--prv, now) == true) {
            intersect(prv, now = erase(now));
        }
        while((now = prv) != begin() and (--prv)->p >= now->p) {
            intersect(prv, erase(now));
        }
    }

    long long query(const long long &x) {
        QueryMode = true;
        auto it = *lower_bound(Line(-INF, -INF, x));
        QueryMode = false;

        return it.m * x + it.c;
    }
}cht;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int N;
    cin >> N;
    for(int i = 1; i <= N; i++) {
        cin >> H[i];
    }
    long long sum_w = 0;
    for(int i = 1; i <= N; i++) {
        cin >> W[i];
        sum_w += W[i];
    }

    dp[1] = -W[1];
    cht.addline(Line(2 * H[1], -H[1] * H[1] - dp[1]));
    for(int i = 2; i <= N; i++) {
        dp[i] = -cht.query(H[i]) + H[i] * H[i] - W[i];
        cht.addline(Line(2 * H[i], -H[i] * H[i] - dp[i]));
    }
    cout << sum_w + dp[N];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3644 KB Output is correct
2 Correct 39 ms 3664 KB Output is correct
3 Correct 39 ms 3728 KB Output is correct
4 Correct 37 ms 3532 KB Output is correct
5 Correct 35 ms 4684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 40 ms 3644 KB Output is correct
7 Correct 39 ms 3664 KB Output is correct
8 Correct 39 ms 3728 KB Output is correct
9 Correct 37 ms 3532 KB Output is correct
10 Correct 35 ms 4684 KB Output is correct
11 Correct 40 ms 3804 KB Output is correct
12 Correct 39 ms 3724 KB Output is correct
13 Correct 36 ms 3680 KB Output is correct
14 Correct 37 ms 3892 KB Output is correct
15 Correct 53 ms 9760 KB Output is correct
16 Correct 37 ms 4752 KB Output is correct
17 Correct 28 ms 3704 KB Output is correct
18 Correct 23 ms 3672 KB Output is correct