QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#783226#7739. KnapsackrahmanmehrajWA 0ms3768kbC++141.5kb2024-11-26 02:06:262024-11-26 02:06:26

Judging History

你现在查看的是最新测评结果

  • [2024-11-26 02:06:26]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3768kb
  • [2024-11-26 02:06:26]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Knapsack function to maximize beauty within a budget
int knapsack(int W, const vector<pair<int, int>>& items) {
    vector<int> dp(W + 1, 0); // dp[w] represents max beauty for budget w

    for (const auto& [price, beauty] : items) { 
        for (int w = W; w >= price; --w) {
            dp[w] = max(dp[w], dp[w - price] + beauty);
        }
    }

    return dp[W];
}

int main() {
    int n, W, k;
    cin >> n >> W >> k;

    vector<pair<int, int>> gemstones(n); // {price, beauty}
    for (int i = 0; i < n; ++i) {
        cin >> gemstones[i].first >> gemstones[i].second;
    }

    int maxBeauty = 0;
    for (int mask = 0; mask < (1 << n); ++mask) {
        if (__builtin_popcount(mask) == k) { // Check if subset size is exactly k
            int freeBeauty = 0, freeCost = 0;
            vector<pair<int, int>> remainingGemstones;

            for (int i = 0; i < n; ++i) {
                if (mask & (1 << i)) {
                    freeBeauty += gemstones[i].second; // Take this gemstone for free
                } else {
                    remainingGemstones.push_back(gemstones[i]); // Add to items to buy
                }
            }

            // Use knapsack to find max beauty for remaining gemstones
            int currentBeauty = freeBeauty + knapsack(W, remainingGemstones);
            maxBeauty = max(maxBeauty, currentBeauty);
        }
    }

    cout << maxBeauty << endl;

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3596kb

input:

4 10 1
9 10
10 1
3 5
5 20

output:

35

result:

ok 1 number(s): "35"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

5 13 2
5 16
5 28
7 44
8 15
8 41

output:

129

result:

ok 1 number(s): "129"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3764kb

input:

10 50 1
44 182173741
38 163268500
36 114173760
30 521894533
25 89514235
12 516184197
42 971377551
35 28242326
31 480227821
31 388523197

output:

2009456281

result:

ok 1 number(s): "2009456281"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3768kb

input:

10 100 3
23 51015869
9 981426050
76 243762017
64 128189636
4 718411601
48 250140255
17 340478117
68 262055220
40 370503079
4 547232664

output:

0

result:

wrong answer 1st numbers differ - expected: '3765024872', found: '0'