結果

問題 No.703 ゴミ拾い Easy
ユーザー parukiparuki
提出日時 2018-08-21 12:25:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 1,500 ms
コード長 2,028 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 205,932 KB
実行使用メモリ 9,340 KB
最終ジャッジ日時 2023-08-30 18:59:57
合計ジャッジ時間 6,644 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,576 KB
testcase_01 AC 2 ms
5,384 KB
testcase_02 AC 2 ms
5,384 KB
testcase_03 AC 2 ms
5,388 KB
testcase_04 AC 2 ms
5,420 KB
testcase_05 AC 2 ms
5,416 KB
testcase_06 AC 2 ms
5,380 KB
testcase_07 AC 2 ms
5,448 KB
testcase_08 AC 2 ms
5,420 KB
testcase_09 AC 2 ms
5,524 KB
testcase_10 AC 2 ms
5,368 KB
testcase_11 AC 2 ms
5,456 KB
testcase_12 AC 2 ms
5,412 KB
testcase_13 AC 2 ms
5,388 KB
testcase_14 AC 2 ms
5,504 KB
testcase_15 AC 2 ms
5,440 KB
testcase_16 AC 2 ms
5,432 KB
testcase_17 AC 2 ms
5,396 KB
testcase_18 AC 2 ms
5,492 KB
testcase_19 AC 2 ms
5,452 KB
testcase_20 AC 2 ms
5,544 KB
testcase_21 AC 2 ms
5,432 KB
testcase_22 AC 2 ms
5,424 KB
testcase_23 AC 2 ms
5,616 KB
testcase_24 AC 85 ms
9,244 KB
testcase_25 AC 85 ms
9,264 KB
testcase_26 AC 84 ms
9,288 KB
testcase_27 AC 85 ms
9,232 KB
testcase_28 AC 85 ms
9,288 KB
testcase_29 AC 85 ms
9,264 KB
testcase_30 AC 85 ms
9,232 KB
testcase_31 AC 84 ms
9,224 KB
testcase_32 AC 85 ms
9,260 KB
testcase_33 AC 85 ms
9,340 KB
testcase_34 AC 74 ms
9,288 KB
testcase_35 AC 74 ms
9,192 KB
testcase_36 AC 75 ms
9,232 KB
testcase_37 AC 74 ms
9,236 KB
testcase_38 AC 76 ms
9,180 KB
testcase_39 AC 76 ms
9,216 KB
testcase_40 AC 75 ms
9,240 KB
testcase_41 AC 75 ms
9,224 KB
testcase_42 AC 75 ms
9,236 KB
testcase_43 AC 74 ms
9,228 KB
testcase_44 AC 2 ms
5,456 KB
testcase_45 AC 2 ms
5,572 KB
testcase_46 AC 88 ms
9,180 KB
testcase_47 AC 88 ms
9,292 KB
testcase_48 AC 2 ms
5,404 KB
testcase_49 AC 2 ms
5,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class Val, class Compare>
struct ConvexHullTrick {
    deque<pair<Val, Val> > lines;
    Compare comp;
    ConvexHullTrick() :comp(Compare()) {
    }
    void addLine(Val slope, Val constant) {
        pair<Val, Val> line(slope, constant);
        int n;
        while ((n = (int)lines.size()) >= 2) {
            Val a1, a2, a3, b1, b2, b3;
            tie(a1, b1) = lines[n - 2];
            tie(a2, b2) = lines[n - 1];
            tie(a3, b3) = line;
            if ((a1 - a2)*(b3 - b1) >(a1 - a3)*(b2 - b1))break;
            else lines.pop_back();
        }
        lines.push_back(line);
    }
    Val query(Val x) {
        while ((int)lines.size() >= 2 && comp(f(x, lines[1]), f(x, lines[0]))) {
            lines.pop_front();
        }
        return f(x, lines[0]);
    }
    Val f(Val x, pair<Val, Val> line) {
        return x * line.first + line.second;
    }
};

const int MAX_N = 300005;
int N, A[MAX_N], X[MAX_N], Y[MAX_N];
ll f[MAX_N];

void solve() {
    cin >> N;
    for (int *x : { A,X,Y }) {
        rep(i, N)cin >> x[i];
    }

    ConvexHullTrick<ll, less<ll>> cht;
    rep(i, N) {
        ll slope = -2ll * X[i];
        ll constant = f[i] + (ll)X[i] * X[i] + (ll)Y[i] * Y[i];
        cht.addLine(slope, constant);
        f[i + 1] = (ll)A[i] * A[i] + cht.query(A[i]);
    }
    cout << f[N] << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0