結果

問題 No.703 ゴミ拾い Easy
ユーザー KowerKoint2010KowerKoint2010
提出日時 2023-06-18 20:25:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 309 ms / 1,500 ms
コード長 2,715 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 78,944 KB
実行使用メモリ 30,884 KB
最終ジャッジ日時 2023-09-08 15:53:50
合計ジャッジ時間 9,154 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 287 ms
30,484 KB
testcase_25 AC 285 ms
30,652 KB
testcase_26 AC 287 ms
30,552 KB
testcase_27 AC 285 ms
30,812 KB
testcase_28 AC 287 ms
30,624 KB
testcase_29 AC 286 ms
30,552 KB
testcase_30 AC 285 ms
30,616 KB
testcase_31 AC 285 ms
30,496 KB
testcase_32 AC 285 ms
30,568 KB
testcase_33 AC 285 ms
30,436 KB
testcase_34 AC 244 ms
30,496 KB
testcase_35 AC 244 ms
30,648 KB
testcase_36 AC 244 ms
30,448 KB
testcase_37 AC 243 ms
30,436 KB
testcase_38 AC 248 ms
30,448 KB
testcase_39 AC 245 ms
30,640 KB
testcase_40 AC 245 ms
30,884 KB
testcase_41 AC 244 ms
30,436 KB
testcase_42 AC 244 ms
30,488 KB
testcase_43 AC 245 ms
30,796 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 309 ms
30,492 KB
testcase_47 AC 297 ms
30,576 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "test/yukicoder-703.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/703"

#line 2 "cpp/convex-hull-trick.hpp"

/**
 * @file convex-hull-tric.hpp
 * @brief Convex Hull Trick
 *
 * 一次関数で表される直線または線分の集合を管理し、あるxに対する最小値を求める
 */

#include <limits>
#include <type_traits>
#include <vector>

template <typename T, std::enable_if_t<std::is_scalar_v<T>, std::nullptr_t> = nullptr>
class LiChaoTree {
    int n, sz, height;
    std::vector<T> xs, as, bs;

    void update(T a, T b, int k, int h) {
        int l = (k << h) & (sz - 1);
        int r = l + (1 << h);
        
        while(1) {
            int m = (l + r) >> 1;
            T xl = xs[l], xm = xs[m];
            bool l_update = a*xl + b < as[k]*xl + bs[k];
            bool m_update = a*xm + b < as[k]*xm + bs[k];

            if(m_update) {
                std::swap(as[k], a);
                std::swap(bs[k], b);
            }
            if(h == 0) break;
            if(l_update != m_update) {
                k = k*2;
                r = m;
            } else {
                k = k*2+1;
                l = m;
            }
            h--;
        }
    }

public:
    LiChaoTree(const std::vector<T>& xs) : n(xs.size()), xs(xs) {
        sz = 1, height = 0;
        while(sz < (int)xs.size()) {
            sz <<= 1;
            height++;
        }
        this->xs.resize(sz, xs.back());
        as.assign(sz*2, 0);
        bs.assign(sz*2, std::numeric_limits<T>::max());
    }

    void add_line(T a, T b) {
        update(a, b, 1, height);
    }
    void add_segment(T a, T b, int l, int r) {
        if(l == r) return;
        l += sz, r += sz;
        int h = 0;
        while(l < r) {
            if(l & 1) update(a, b, l++, h);
            if(r & 1) update(a, b, --r, h);
            l >>= 1, r >>= 1, h++;
        }
    }

    T get_min(int i) const {
        T x = xs[i];
        int k = i + sz;
        T res = as[k]*x + bs[k];
        while(k > 1) {
            k >>= 1;
            T tmp = as[k]*x + bs[k];
            if(tmp < res) res = tmp;
        }
        return res;
    }
};
#line 4 "test/yukicoder-703.test.cpp"
#include <iostream>
#line 6 "test/yukicoder-703.test.cpp"

int main(void) {
    int n; std::cin >> n;
    std::vector<long long> a(n), x(n), y(n);
    for(int i = 0; i < n; i++) std::cin >> a[i];
    for(int i = 0; i < n; i++) std::cin >> x[i];
    for(int i = 0; i < n; i++) std::cin >> y[i];
    LiChaoTree<long long> cht(a);
    long long dp = 0;
    for(int i = 0; i < n; i++) {
        cht.add_line(-2*x[i], dp + x[i]*x[i] + y[i]*y[i]);
        dp = cht.get_min(i) + a[i]*a[i];
    }
    std::cout << dp << std::endl;
}
0