結果

問題 No.703 ゴミ拾い Easy
ユーザー face4face4
提出日時 2019-12-21 18:17:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 1,500 ms
コード長 2,015 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 75,728 KB
実行使用メモリ 35,684 KB
最終ジャッジ日時 2023-08-30 19:13:26
合計ジャッジ時間 8,533 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 261 ms
35,384 KB
testcase_25 AC 260 ms
35,232 KB
testcase_26 AC 262 ms
35,204 KB
testcase_27 AC 262 ms
35,520 KB
testcase_28 AC 262 ms
35,248 KB
testcase_29 AC 262 ms
35,124 KB
testcase_30 AC 263 ms
35,152 KB
testcase_31 AC 261 ms
35,256 KB
testcase_32 AC 262 ms
35,292 KB
testcase_33 AC 262 ms
35,072 KB
testcase_34 AC 223 ms
35,148 KB
testcase_35 AC 221 ms
35,556 KB
testcase_36 AC 223 ms
35,184 KB
testcase_37 AC 223 ms
35,684 KB
testcase_38 AC 222 ms
35,224 KB
testcase_39 AC 224 ms
35,280 KB
testcase_40 AC 225 ms
35,460 KB
testcase_41 AC 222 ms
35,116 KB
testcase_42 AC 223 ms
35,272 KB
testcase_43 AC 222 ms
35,112 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 289 ms
35,148 KB
testcase_47 AC 276 ms
35,356 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

struct line{
    ll a, b;
    bool operator==(const line other)const{
        return a==other.a && b==other.b;
    }
};

ll f(line l, ll x){
    return l.a*x + l.b;
}

const ll INF = 1ll<<55;

struct LiChaoTree{
    int n;
    vector<ll> xs;
    vector<line> v;
    line tmp{0, INF};
    LiChaoTree(vector<ll> x) : xs(x){
        n = 1;
        while(n < xs.size())  n *= 2;
        while(xs.size() < n)    xs.push_back(x.back());
        v.resize(2*n+1, tmp);
    }
    void add_line(line p, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        int m = (l+r)/2;
        if(v[k] == tmp){
            v[k] = p;
            return;
        }
        ll lx = xs[l], mx = xs[m], rx = xs[r-1];
        bool left = f(p,lx) < f(v[k], lx);
        bool mid = f(p,mx) < f(v[k], mx);
        bool right = f(p,rx) < f(v[k], rx);
        if(left == right){
            if(left)    v[k] = p;
            return;
        }
        if(mid){
            swap(v[k], p);
        }
        // v[k]とpが交わっている部分について更新していくと考えるとわかりやすい
        if(left != mid){
            add_line(p, 2*k+1, l, m);
        }else{
            add_line(p, 2*k+2, m, r);
        }
    }
    ll query(int k){
        ll x = xs[k];
        k += n-1;
        ll ret = INF;
        while(true){
            if(!(v[k]==tmp)) ret = min(ret, f(v[k], x));
            if(k==0)    break;
            k = (k-1)/2;
        }
        return ret;
    }
};

int main(){
    int n;
    cin >> n;
    vector<ll> a(n), x(n), y(n);
    for(int i = 0; i < n; i++)  cin >> a[i];
    for(int i = 0; i < n; i++)  cin >> x[i];
    for(int i = 0; i < n; i++)  cin >> y[i];
    vector<ll> dp(n+1, INF);
    dp[0] = 0;
    LiChaoTree lct(a);
    for(int i = 0; i < n; i++){
        lct.add_line(line({-2*x[i], x[i]*x[i]+y[i]*y[i]+dp[i]}));
        dp[i+1] = lct.query(i) + a[i]*a[i];
    }
    cout << dp[n] << endl;
    return 0;
}
0