結果

問題 No.952 危険な火薬庫
ユーザー Shuz*Shuz*
提出日時 2019-08-15 12:48:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 6,113 bytes
コンパイル時間 1,719 ms
コンパイル使用メモリ 179,664 KB
実行使用メモリ 73,812 KB
最終ジャッジ日時 2024-05-01 03:47:32
合計ジャッジ時間 3,977 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
6,940 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 151 ms
73,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// Define
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
const ll mod = 998244353;
const ll inf = 1 << 30;
const ll LINF = LONG_MAX;
const ll INF = 1LL << 60;
const ull MAX = ULONG_MAX;
#define mp make_pair
#define pb push_back
#define elif else if
#define endl '\n'
#define space ' '
#define def inline auto
#define func inline constexpr ll
#define run(a) __attribute__((constructor)) def _##a()
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define input(a) scanf("%lld", &(a))
#define print(a) printf("%lld\n", (a))
#define fi first
#define se second
#define ok(a, b) (0 <= (a) && (a) < (b))
template <class T> using vvector = vector<vector<T>>;
template <class T> using pvector = vector<pair<T, T>>;
template <class T>
using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

// Debug
#define debug(...)                                                             \
    {                                                                          \
        cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ";                     \
        for (auto &&X : {__VA_ARGS__}) cerr << "[" << X << "] ";               \
        cerr << endl;                                                          \
    }

#define dump(a, h, w)                                                          \
    {                                                                          \
        cerr << __LINE__ << ": " << #a << " = [" << endl;                      \
        rep(__i, h) {                                                          \
            rep(__j, w) { cerr << a[__i][__j] << space; }                      \
            cerr << endl;                                                      \
        }                                                                      \
        cerr << "]" << endl;                                                   \
    }

#define vdump(a, n)                                                            \
    {                                                                          \
        cerr << __LINE__ << ": " << #a << " = [";                              \
        rep(__i, n) if (__i) cerr << space << a[__i];                          \
        else cerr << a[__i];                                                   \
        cerr << "]" << endl;                                                   \
    }

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i)
#define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i)
#define each(i, a) for (auto &&i : a)
#define loop() for (;;)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)
run(0) { fasten, fout(10); }

// Speed
#pragma GCC optimize("O3")
#pragma GCC target("avx")

// Math
func gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
func lcm(ll a, ll b) { return a * b / gcd(a, b); }
func sign(ll a) { return a ? abs(a) / a : 0; }

inline constexpr ll modulo(const ll n, const ll m = MOD) {
    if (n < 0) {
        return n % m + m;
    } else if (n >= m) {
        return n % m;
    } else {
        return n;
    }
}

inline constexpr ll chmod(ll &n, const ll m = MOD) { return n = modulo(n, m); }

inline constexpr ll mpow(ll a, ll n, const ll m = MOD) {
    ll r = 1;
    while (n) {
        if (n & 1) r *= a;
        chmod(r, m);
        a *= a;
        chmod(a, m);
        n >>= 1;
    }
    return r;
}

struct Factor {
    inline vector<ll> factors(ll N) {
        vector<ll> A;
        ll i = 2;
        while (i * i <= N) {
            if (N % i == 0) {
                A.push_back(i);
                N /= i;
            } else {
                i++;
            }
        }
        if (N != 1) A.push_back(N);
        sort(all(A));
        return A;
    }
    inline vector<ll> divisor(ll N) {
        vector<ll> A = factors(N), B;
        ll a = A.size();
        rep(i, 1 << a) {
            ll d = 1;
            rep(j, a) if (i & (1 << j)) d *= A[j];
            B.push_back(d);
        }
        sort(all(B)), B.erase(unique(all(B)), B.end());
        return B;
    }
};

struct csum {
    ll N;
    vector<ll> A, S;
    csum(vector<ll> v) : A(v), S(1) { each(k, v) S.push_back(k + S.back()); }
    ll sum(ll l, ll r) { return S[r] - S[l]; }
    ll lsum(ll r) { return S[r]; }
    ll rsum(ll l) { return S.back() - S[l]; }
};

struct LinearCHT {
    using F = pair<ll, ll>;
    deque<F> deq;
#define a first
#define b second
#define lines deq.size()
    bool invalid_f1(F f0, F f1, F f2) {
        return (f1.a - f0.a) * (f2.b - f1.b) >= (f1.b - f0.b) * (f2.a - f1.a);
    }
    ll f(F f0, ll x) { return f0.a * x + f0.b; }
    void add(F f0) {
        while (lines >= 2 && invalid_f1(deq[lines - 2], deq[lines - 1], f0)) {
            deq.pop_back();
        }
        deq.push_back(f0);
    }
    ll min(ll x) {
        while (lines >= 2 && f(deq[0], x) >= f(deq[1], x)) {
            deq.pop_front();
        }
        return f(deq[0], x);
    }
};

signed main() {
    ll N;
    cin >> N;
    vector<ll> S(N + 1);
    ll DP[N + 2][N + 1];
    rep(i, N) cin >> S[i + 1], S[i + 1] += S[i];
    rep(i, N + 2) rep(j, N + 1) DP[i][j] = INF;
    rep(i, N + 2) DP[i][0] = 0;
    rep(i, N + 2) {
        LinearCHT CHT;
        rep(j, N + 1) {
            if (i + j + 1 > N + 1) break;
            ll A = -2 * S[i + j];
            ll B = S[i + j] * S[i + j] + DP[i + j][j];
            ll X = S[i + j];
            ll Y = S[i + j] * S[i + j];
            CHT.add({A, B});
            chmin(DP[i + j + 1][j], CHT.min(X) + Y);
        }
    }
    rep(i, N) cout << min(DP[N][i + 1], DP[N + 1][i + 1]) << endl;
}
0