답안 #669547

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
669547 2022-12-06T17:28:54 Z four_specks Building Bridges (CEOI17_building) C++17
100 / 100
52 ms 9040 KB
#include <bits/stdc++.h>

using namespace std;

inline namespace
{
    template <typename T>
    T fl_div(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }

    template <typename T>
    struct Line
    {
        T m, c;
        T operator()(T x) const { return m * x + c; }
    };

    template <typename T>
    struct LineContainerObject
    {
        T m, c;
        mutable T p;

        bool operator<(const LineContainerObject &i_line) const { return m < i_line.m; }
        bool operator<(T x) const { return p < x; }
    };

    template <typename T>
    struct LineContainer : multiset<LineContainerObject<T>, less<>>
    {
        void add(const Line<T> &line)
        {
            auto it1 = Base::insert({line.m, line.c, 0}), it2 = next(it1), it = it1;
            while (isect(it, next(it)))
                Base::erase(next(it));
            if (it != Base::begin())
            {
                it--;
                if (isect(it, next(it)))
                    isect(it, Base::erase(next(it)));
            }
            while (it != Base::begin() && prev(it)->p >= it->p)
            {
                it--;
                isect(it, Base::erase(next(it)));
            }
        }

        T query(T x)
        {
            auto it = Base::lower_bound(x);
            return it->m * x + it->c;
        }

    private:
        using Base = multiset<LineContainerObject<T>, less<>>;
        using Iterator = typename Base::iterator;

        inline static const T INF = numeric_limits<T>::max();

        bool isect(Iterator it, Iterator it1)
        {
            if (it1 == Base::end())
            {
                it->p = INF;
                return 0;
            }

            if (it->m == it1->m)
                it->p = it->c > it1->c ? INF : -INF;
            else
                it->p = fl_div(it1->c - it->c, it->m - it1->m);

            return it->p >= it1->p;
        }
    };

} // namespace

void solve()
{
    int n;
    cin >> n;

    vector<long> h(n), w(n);
    for (long &x : h)
        cin >> x;
    for (long &x : w)
        cin >> x;

    long add = 0;
    for (long x : w)
        add += x;

    vector<long> dp(n);
    LineContainer<long> cht;
    dp[0] = w[0];
    cht.add(Line<long>{2 * h[0], dp[0] - h[0] * h[0]});
    for (int i = 1; i < n; i++)
    {
        dp[i] = cht.query(h[i]) - h[i] * h[i] + w[i];
        cht.add(Line<long>{2 * h[i], dp[i] - h[i] * h[i]});
    }

    cout << -dp[n - 1] + add << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

    solve();

    return 0;
}

Compilation message

building.cpp: In instantiation of 'void {anonymous}::LineContainer<T>::add(const {anonymous}::Line<T>&) [with T = long int]':
building.cpp:97:54:   required from here
building.cpp:32:59: warning: variable 'it2' set but not used [-Wunused-but-set-variable]
   32 |             auto it1 = Base::insert({line.m, line.c, 0}), it2 = next(it1), it = it1;
      |                                                           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 2728 KB Output is correct
2 Correct 37 ms 2644 KB Output is correct
3 Correct 37 ms 2644 KB Output is correct
4 Correct 35 ms 2700 KB Output is correct
5 Correct 40 ms 3864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 36 ms 2728 KB Output is correct
7 Correct 37 ms 2644 KB Output is correct
8 Correct 37 ms 2644 KB Output is correct
9 Correct 35 ms 2700 KB Output is correct
10 Correct 40 ms 3864 KB Output is correct
11 Correct 36 ms 2696 KB Output is correct
12 Correct 36 ms 2752 KB Output is correct
13 Correct 28 ms 2668 KB Output is correct
14 Correct 36 ms 2744 KB Output is correct
15 Correct 52 ms 9040 KB Output is correct
16 Correct 32 ms 3796 KB Output is correct
17 Correct 19 ms 2672 KB Output is correct
18 Correct 18 ms 2644 KB Output is correct