結果

問題 No.703 ゴミ拾い Easy
ユーザー KowerKoint2010KowerKoint2010
提出日時 2023-06-18 19:43:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,898 bytes
コンパイル時間 716 ms
コンパイル使用メモリ 79,932 KB
実行使用メモリ 31,112 KB
最終ジャッジ日時 2023-09-08 15:49:55
合計ジャッジ時間 10,677 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,500 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,500 KB
testcase_13 AC 1 ms
4,504 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 214 ms
30,532 KB
testcase_35 AC 216 ms
30,484 KB
testcase_36 AC 217 ms
30,668 KB
testcase_37 AC 217 ms
30,504 KB
testcase_38 AC 216 ms
30,484 KB
testcase_39 AC 216 ms
30,544 KB
testcase_40 AC 217 ms
30,436 KB
testcase_41 AC 220 ms
30,464 KB
testcase_42 AC 216 ms
30,408 KB
testcase_43 AC 216 ms
30,640 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 282 ms
30,508 KB
testcase_47 AC 269 ms
30,836 KB
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
        int m = (l + r) >> 1;
        
        while(h >= 0) {
            T xl = xs[l], xm = xs[m], xr = xs[r-1];
            bool l_update = a*xl + b < as[k]*xl + bs[k];
            bool m_update = a*xm + b < as[k]*xm + bs[k];
            bool r_update = a*xr + b < as[k]*xr + bs[k];

            if(l_update && r_update) {
                as[k] = a;
                bs[k] = b;
                return;
            }
            if(!l_update && !r_update) return;
            if(m_update) {
                std::swap(as[k], a);
                std::swap(bs[k], b);
            }
            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>

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