結果

問題 No.1583 Building Blocks
ユーザー merom686
提出日時 2021-07-02 22:48:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,018 bytes
コンパイル時間 1,840 ms
コンパイル使用メモリ 200,040 KB
最終ジャッジ日時 2025-01-22 16:36:24
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T>
void chmin(T &a, T b) {
    if (b < a) a = b;
}

struct P {
    bool operator<(const P &p) const {
        return w + s < p.w + p.s;
    }
    int w, s;
};

int dp[1001][1001];

int main() {
    int n;
    cin >> n;

    vector<P> p(n);
    for (int i = 0; i < n; i++) {
        int w, s;
        cin >> w >> s;

        p[i] = { w, s };
    }
    sort(p.begin(), p.end());

    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            dp[i][j] = (int)2.01e9;
        }
    }
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            dp[i + 1][j] = dp[i][j];
        }
        for (int j = 0; j <= i; j++) {
            if (dp[i][j] <= p[i].s) chmin(dp[i + 1][j + 1], dp[i][j] + p[i].w);
        }
    }

    for (int j = n; j >= 0; j--) {
        if (dp[n][j] < (int)2.01e9) {
            cout << j << endl;
            break;
        }
    }

    return 0;
}
0