結果

問題 No.1037 exhausted
ユーザー tomatoma
提出日時 2020-04-24 23:11:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 3,183 bytes
コンパイル時間 1,580 ms
コンパイル使用メモリ 174,008 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 04:11:18
合計ジャッジ時間 5,210 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tp3 = tuple<int, int, int>;
using Mat = vector<vector<ll>>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
template<typename T>
T minch(T& l, T r) {
    return l = min(l, r);
}
template<typename T>
T maxch(T& l, T r) {
    return l = max(l, r);
}
template<typename T>
void output(const T& val) {
    cout << val << endl;
}
template<typename T>
void output(const vector<T>& vec, const bool newline = false) {
    for (const T& val : vec)cout << val << (newline ? '\n' : ' '); cout << endl;
}
template<typename T>
void output(const vector<vector<T>>& mat) {
    for (const auto& row : mat)output(row);
}
//ttp://beet-aizu.hatenablog.com/entry/2019/03/12/171221
template<typename T>
class SegmentTree {
private:
    using F = function<T(T, T)>; // モノイド型
    int n; // 横幅
    F f;   // モノイド
    T e;   // モノイド単位元
    vector<T> data;

public:
    // init忘れに注意
    SegmentTree() {}
    SegmentTree(F f, T e) :f(f), e(e) {}
    void init(int n_) {
        n = 1;
        while (n < n_)n <<= 1;
        data.assign(n << 1, e);
    }
    void build(const vector<T>& v) {
        int n_ = v.size();
        init(n_);
        rep(i, n_)data[n + i] = v[i];
        for (int i = n - 1; i >= 0; i--) {
            data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
        }
    }
    void set_val(int idx, T val) {
        idx += n;
        data[idx] = val;
        while (idx >>= 1) {
            data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
        }
    }
    T query(int a, int b) {
        // [a,b)
        T vl = e, vr = e;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1)vl = f(vl, data[l++]); // unknown
            if (r & 1)vr = f(data[--r], vr); // unknown
        }
        return f(vl, vr);
    }
};
// ============ template finished ============

int main()
{
    ll N, V, L;
    cin >> N >> V >> L;
    vector<ll> dp(V + 1, 0);
    vector<vector<ll>> xvw(N + 1, vector<ll>(3, 0));
    REP(i, 1, N + 1) {
        rep(j, 3)cin >> xvw[i][j];
    }
    REP(i, 1, N + 1) {
        ll d = xvw[i][0] - xvw[i - 1][0];
        {
            // slide
            rep(j, V + 1) {
                dp[j] = j + d <= V ? dp[j + d] : INFL;
            }
        }
        auto f = [](ll a, ll b) {return min(a, b); };
        SegmentTree<ll> seg(f, INFL);
        seg.build(dp);

        vector<ll> dp2(V + 1);
        ll v = xvw[i][1], w = xvw[i][2];
        rep(j, V + 1) {
            ll L = max(0ll, j - v);
            dp2[j] = min(dp[j], seg.query(L, j) + w);
        }
        dp = dp2;
    }
    ll D = L - xvw.back()[0];
    if (D > V)output(-1);
    else {
        auto res = *min_element(dp.begin() + D, dp.end());
        output(res == INFL ? -1 : res);
    }
    return 0;
}
0