QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#732483#7739. Knapsackenar#WA 1ms8956kbC++202.1kb2024-11-10 14:44:212024-11-10 14:44:22

Judging History

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

  • [2024-11-10 14:44:22]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:8956kb
  • [2024-11-10 14:44:21]
  • 提交

answer

#include <bits/stdc++.h>
using i64 = long long;

void solve() {
    int n, W, k;
    std::cin >> n >> W >> k;
    std::vector dp(2, std::vector(n + 1, std::vector<i64>(W + 1)));
    std::vector lst(2, std::vector(n + 1, std::vector<std::array<int, 3>>(W + 1)));
    std::vector<std::pair<i64, i64>> v(n + 1);
    for(int i = 1; i <= n; ++i) {
        auto &[cost, val] = v[i];
        std::cin >> cost >> val;
        for(int j = 0; j <= W; ++j) {
            if(dp[0][i - 1][j] > dp[0][i][j]) {
                dp[0][i][j] = dp[0][i - 1][j];
                lst[0][i][j] = {0, i - 1, j};
            }
            if(dp[1][i - 1][j] > dp[0][i][j]) {
                dp[0][i][j] = dp[1][i - 1][j];
                lst[0][i][j] = {1, i - 1, j};
            }
        }
        for(int j = cost; j <= W; ++j) {
            if(dp[0][i - 1][j - cost] + val > dp[1][i][j]) {
                dp[1][i][j] = dp[0][i - 1][j - cost] + val;
                lst[1][i][j] = {0, i - 1, j - cost};
            }
            if(dp[1][i - 1][j - cost] + val > dp[1][i][j]) {
                dp[1][i][j] = dp[1][i - 1][j - cost] + val;
                lst[1][i][j] = {1, i - 1, j - cost};
            }
        }
    }
    std::array<int, 3> now = {(dp[1][n][W] > dp[0][n][W]), n, W};
    std::vector<int> vis(n + 1);
    while(now != std::array<int, 3>{0, 0, 0}) {
        auto [i, j, k] = now;
        if(i == 1) {
            vis[j] = 1;
        }
        now = lst[i][j][k];
    }
    std::vector<std::pair<i64, i64>> vec;
    for(int i = 1; i <= n; ++i) {
        if(vis[i]) continue;
        vec.push_back(v[i]);
    }
    std::sort(vec.begin(), vec.end(), 
    [&](auto a, auto b) ->bool {
        return a.second > b.second;
    });
    i64 ans = std::max(dp[0][n][W], dp[1][n][W]);
    for(int i = 0; i < vec.size() && i < k; ++i) {
        ans += vec[i].second;
    }
    std::cout << ans << '\n';
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T = 1;
    //std::cin >> T;
    while (T--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3504kb

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: 3648kb

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: 0
Accepted
time: 0ms
memory: 3680kb

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:

3765024872

result:

ok 1 number(s): "3765024872"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

10 500 10
430 981427684
100 458631577
32 453298334
393 716958962
82 120486064
393 561149128
182 518807793
293 950335710
332 159193263
331 280711850

output:

5201000365

result:

ok 1 number(s): "5201000365"

Test #6:

score: 0
Accepted
time: 1ms
memory: 4908kb

input:

10 3000 10
1325 563890842
2007 190665722
1393 874490922
548 279594682
1380 155046921
2666 894516819
770 740325614
2735 643777488
2451 754155860
1068 138544189

output:

5235009059

result:

ok 1 number(s): "5235009059"

Test #7:

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

input:

10 10000 5
108 735534045
6250 87364128
3071 66920092
9343 555321302
9070 759896065
9843 146885261
3083 364637443
7088 370871572
7802 754417134
3125 697204945

output:

4451687859

result:

ok 1 number(s): "4451687859"

Test #8:

score: 0
Accepted
time: 1ms
memory: 4040kb

input:

100 50 61
24 517916473
33 497071404
40 343150837
13 559776223
2 941245278
27 987936903
7 403293890
26 68412861
28 683505315
6 173482637
31 220799032
29 815472376
42 426462445
25 470177395
43 818534622
26 137556071
15 308105056
27 745044655
28 309413241
11 61130780
36 963194467
19 701095156
5 9347020...

output:

44747553879

result:

ok 1 number(s): "44747553879"

Test #9:

score: -100
Wrong Answer
time: 1ms
memory: 3996kb

input:

100 100 42
6 774586597
36 576227741
25 257737545
64 559313526
24 371408966
47 803303316
73 342479452
100 619687258
73 682542576
71 182579788
61 354270154
74 30869398
23 995859349
88 255983493
48 334287458
17 13677067
8 441554725
24 934072639
85 610681655
67 327543259
60 697383634
99 320640445
50 974...

output:

37030241557

result:

wrong answer 1st numbers differ - expected: '37584168931', found: '37030241557'