답안 #770788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770788 2023-07-02T00:41:46 Z vgoofficial Knapsack (NOI18_knapsack) C++14
100 / 100
58 ms 35028 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(0);
    int s,n;
    cin >> s >> n;
    vector<vector<pair<int, int>>> items(s+1);
    for(int i = 0; i < n; i++) {
        int value, weight, number;
        cin >> value >> weight >> number;
        items[weight].push_back(make_pair(value, number));
    }
    for(int i = 1; i <= s; i++) {
        sort(begin(items[i]), end(items[i]), [](const pair<int, int> a, const pair<int, int> b) -> bool{
            return a.first>b.first;
        });
    }
    ll arr[s+1][s+1];
    //arr[a][b]=x: first a weights, b total weight, x max value
    for(int i = 0; i <= s; i ++) {
        for(int j = 0; j <= s; j++) {
            arr[i][j]=0;
        }
    }

    for(int i = 1; i <= s; i++) {
        for(int j = 0; j <= s; j++) {
            arr[i][j]=max(arr[i-1][j], arr[i][j]);
            if(items[i].size()==0) continue;
            int weight = j;
            ll value = arr[i-1][j];
            int itemsUsed = 0;
            int index = 0;
            while(weight+i<=s) {
                itemsUsed++;
                weight+=i;
                value+=items[i][index].first;
                arr[i][weight]=max(arr[i][weight], value);
                if(itemsUsed==items[i][index].second) {
                    itemsUsed=0;
                    index++;
                    if(index==items[i].size()) {
                        break;
                    }
                }
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i <= s; i++) {
        ans=max(ans, arr[s][i]);
    }
    cout << ans << endl;
}

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:44:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |                     if(index==items[i].size()) {
      |                        ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 7 ms 12260 KB Output is correct
4 Correct 7 ms 12256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 18 ms 31700 KB Output is correct
3 Correct 18 ms 31620 KB Output is correct
4 Correct 17 ms 31572 KB Output is correct
5 Correct 17 ms 31456 KB Output is correct
6 Correct 20 ms 31592 KB Output is correct
7 Correct 20 ms 31560 KB Output is correct
8 Correct 21 ms 31552 KB Output is correct
9 Correct 17 ms 31612 KB Output is correct
10 Correct 18 ms 31672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 18 ms 31700 KB Output is correct
3 Correct 18 ms 31620 KB Output is correct
4 Correct 17 ms 31572 KB Output is correct
5 Correct 17 ms 31456 KB Output is correct
6 Correct 20 ms 31592 KB Output is correct
7 Correct 20 ms 31560 KB Output is correct
8 Correct 21 ms 31552 KB Output is correct
9 Correct 17 ms 31612 KB Output is correct
10 Correct 18 ms 31672 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 19 ms 31680 KB Output is correct
13 Correct 17 ms 31552 KB Output is correct
14 Correct 17 ms 31700 KB Output is correct
15 Correct 18 ms 31540 KB Output is correct
16 Correct 19 ms 31548 KB Output is correct
17 Correct 18 ms 31572 KB Output is correct
18 Correct 18 ms 31572 KB Output is correct
19 Correct 19 ms 31700 KB Output is correct
20 Correct 20 ms 31552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 7 ms 12260 KB Output is correct
4 Correct 7 ms 12256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 18 ms 31700 KB Output is correct
7 Correct 18 ms 31620 KB Output is correct
8 Correct 17 ms 31572 KB Output is correct
9 Correct 17 ms 31456 KB Output is correct
10 Correct 20 ms 31592 KB Output is correct
11 Correct 20 ms 31560 KB Output is correct
12 Correct 21 ms 31552 KB Output is correct
13 Correct 17 ms 31612 KB Output is correct
14 Correct 18 ms 31672 KB Output is correct
15 Correct 0 ms 320 KB Output is correct
16 Correct 19 ms 31680 KB Output is correct
17 Correct 17 ms 31552 KB Output is correct
18 Correct 17 ms 31700 KB Output is correct
19 Correct 18 ms 31540 KB Output is correct
20 Correct 19 ms 31548 KB Output is correct
21 Correct 18 ms 31572 KB Output is correct
22 Correct 18 ms 31572 KB Output is correct
23 Correct 19 ms 31700 KB Output is correct
24 Correct 20 ms 31552 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 19 ms 31520 KB Output is correct
27 Correct 18 ms 31620 KB Output is correct
28 Correct 18 ms 31532 KB Output is correct
29 Correct 17 ms 31640 KB Output is correct
30 Correct 21 ms 31680 KB Output is correct
31 Correct 19 ms 31620 KB Output is correct
32 Correct 21 ms 31640 KB Output is correct
33 Correct 19 ms 31572 KB Output is correct
34 Correct 20 ms 31648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 7 ms 12260 KB Output is correct
4 Correct 7 ms 12256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 18 ms 31700 KB Output is correct
7 Correct 18 ms 31620 KB Output is correct
8 Correct 17 ms 31572 KB Output is correct
9 Correct 17 ms 31456 KB Output is correct
10 Correct 20 ms 31592 KB Output is correct
11 Correct 20 ms 31560 KB Output is correct
12 Correct 21 ms 31552 KB Output is correct
13 Correct 17 ms 31612 KB Output is correct
14 Correct 18 ms 31672 KB Output is correct
15 Correct 0 ms 320 KB Output is correct
16 Correct 19 ms 31680 KB Output is correct
17 Correct 17 ms 31552 KB Output is correct
18 Correct 17 ms 31700 KB Output is correct
19 Correct 18 ms 31540 KB Output is correct
20 Correct 19 ms 31548 KB Output is correct
21 Correct 18 ms 31572 KB Output is correct
22 Correct 18 ms 31572 KB Output is correct
23 Correct 19 ms 31700 KB Output is correct
24 Correct 20 ms 31552 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 19 ms 31520 KB Output is correct
27 Correct 18 ms 31620 KB Output is correct
28 Correct 18 ms 31532 KB Output is correct
29 Correct 17 ms 31640 KB Output is correct
30 Correct 21 ms 31680 KB Output is correct
31 Correct 19 ms 31620 KB Output is correct
32 Correct 21 ms 31640 KB Output is correct
33 Correct 19 ms 31572 KB Output is correct
34 Correct 20 ms 31648 KB Output is correct
35 Correct 21 ms 2336 KB Output is correct
36 Correct 43 ms 33816 KB Output is correct
37 Correct 41 ms 33584 KB Output is correct
38 Correct 39 ms 33892 KB Output is correct
39 Correct 42 ms 34544 KB Output is correct
40 Correct 58 ms 34924 KB Output is correct
41 Correct 50 ms 34096 KB Output is correct
42 Correct 50 ms 34152 KB Output is correct
43 Correct 54 ms 34160 KB Output is correct
44 Correct 56 ms 33968 KB Output is correct
45 Correct 44 ms 35028 KB Output is correct
46 Correct 38 ms 34448 KB Output is correct
47 Correct 44 ms 34924 KB Output is correct
48 Correct 50 ms 34920 KB Output is correct
49 Correct 36 ms 34740 KB Output is correct
50 Correct 49 ms 34568 KB Output is correct