답안 #704207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
704207 2023-03-01T23:06:53 Z finn__ Building Bridges (CEOI17_building) C++17
30 / 100
3000 ms 2612 KB
#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct LinearFn
{
    T m, t;

    LinearFn() {}

    LinearFn(T m_, T t_) { m = m_, t = t_; }

    T operator()(T x) const { return m * x + t; }
};

template <typename T>
struct LiChaoNode
{
    LiChaoNode<T> *left, *right;
    LinearFn<T> f;

    LiChaoNode() { left = 0, right = 0, f = LinearFn<T>(0, numeric_limits<T>::max() / 4); }

    void insert(LinearFn<T> g, T l, T r)
    {
        T mid = (l + r) / 2;
        if (g(mid) < f(mid))
            swap(g, f);
        if (r - l == 1)
            return;
        if ((g(l) < f(l)) ^ (g(mid) < f(mid)))
        {
            if (!left)
                left = new LiChaoNode();
            left->insert(g, l, (l + r) / 2);
        }
        else
        {
            if (!right)
                right = new LiChaoNode();
            right->insert(g, (l + r) / 2, r);
        }
    }

    T get_min(T x, T l, T r)
    {
        T y = f(x);
        if (left)
            y = min(y, left->get_min(x, l, (l + r) / 2));
        if (right)
            y = min(y, right->get_min(x, (l + r) / 2, r));
        return y;
    }

    void destroy()
    {
        if (left)
            left->destroy();
        if (right)
            right->destroy();
        delete left;
        delete right;
    }
};

#define N 100000
#define H (1 << 21)

int64_t h[N], w[N];

int main()
{
    size_t n;
    cin >> n;

    int64_t wsum = 0;
    for (size_t i = 0; i < n; i++)
        cin >> h[i];
    for (size_t i = 0; i < n; i++)
        cin >> w[i], wsum += w[i];

    LiChaoNode<int64_t> root;
    root.insert(LinearFn<int64_t>(-2 * h[0], h[0] * h[0] - w[0]), 0, H);

    for (size_t i = 1; i < n; i++)
    {
        int64_t y = root.get_min(h[i], 0, H);
        LinearFn<int64_t> g(-2 * h[i], y + 2 * h[i] * h[i] - w[i]);
        root.insert(g, 0, H);

        if (i == n - 1)
            cout << wsum + g.t - h[i] * h[i] << '\n';
    }

    root.destroy();
}
# 결과 실행 시간 메모리 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 3 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3066 ms 2612 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 3 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Execution timed out 3066 ms 2612 KB Time limit exceeded
7 Halted 0 ms 0 KB -