1 条题解

  • 0
    @ 2025-11-3 0:09:35

    C++ :

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int N = 5e4 + 1;
    // 强度 >= P 的最小花费
    int t, f[101][N], p[101], c[101]; // fij: 从前 i 个里选择,费用为 j 的最大强度
    
    int main() {
        cin >> t;
        while (t--) {
            int n, P, Q;
            cin >> n >> P >> Q;
            for (int i = 1; i <= n; i++) {
                cin >> p[i] >> c[i];
            }
    
            int x = 1e9;
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= Q; j++) {
                    f[i][j] = f[i - 1][j];
                    if (j >= c[i])
                        f[i][j] = max(f[i][j], f[i - 1][j - c[i]] + p[i]);
                    if (f[i][j] >= P && j < x)
                        x = j;
                }
            }
            if (x > Q) cout << "-1\n";
            else cout << x << endl;
    
        }
    }
    
    
    • 1

    信息

    ID
    5624
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者