제출 #1221428

#제출 시각아이디문제언어결과실행 시간메모리
1221428mifzal5331Knapsack (NOI18_knapsack)C++20
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
    int kapasitas, barang;
    cin >> kapasitas >> barang;

    vector<int> harga(barang), berat(barang), stok(barang);
    for (int i = 0; i < barang; i++) {
        cin >> harga[i] >> berat[i] >> stok[i];
    }

    // Group items by weight
    map<int, vector<pair<int, int>>> byBerat;
    for (int i = 0; i < barang; i++) {
        byBerat[berat[i]].push_back(harga[i], stok[i]);
    }

    vector<pair<int, int>> items; // (total_weight, total_value)
    for (auto &[w, group] : byBerat) {
        sort(group.rbegin(), group.rend()); // sort by value descending

        int maxAllowed = kapasitas / w;

        for (auto &[v, s] : group) {
            int take = min(s, maxAllowed);
            maxAllowed -= take;

            for (int k = 1; take > 0; k <<= 1) {
                int batch = min(k, take);
                take -= batch;

                items.push_back(w * batch, v * batch);
            }

            if (maxAllowed == 0) break;
        }
    }

    vector<ll> dp(kapasitas + 1, 0);
    for (auto &[beratItem, nilaiItem] : items) {
        for (int j = kapasitas; j >= beratItem; j--) {
            dp[j] = max(dp[j], dp[j - beratItem] + nilaiItem);
        }
    }

    cout << dp[kapasitas] << endl;
}

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'int main()':
knapsack.cpp:18:36: error: no matching function for call to 'std::vector<std::pair<int, int> >::push_back(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)'
   18 |         byBerat[berat[i]].push_back(harga[i], stok[i]);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/vector:67,
                 from /usr/include/c++/11/functional:62,
                 from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/11/algorithm:74,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:65,
                 from knapsack.cpp:1:
/usr/include/c++/11/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::pair<int, int>]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/11/bits/stl_vector.h:1187:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/11/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::pair<int, int>]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/11/bits/stl_vector.h:1203:7: note:   candidate expects 1 argument, 2 provided
knapsack.cpp:35:32: error: no matching function for call to 'std::vector<std::pair<int, int> >::push_back(int, std::tuple_element<0, std::pair<int, int> >::type)'
   35 |                 items.push_back(w * batch, v * batch);
      |                 ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/vector:67,
                 from /usr/include/c++/11/functional:62,
                 from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/11/algorithm:74,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:65,
                 from knapsack.cpp:1:
/usr/include/c++/11/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::pair<int, int>]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/11/bits/stl_vector.h:1187:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/11/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::pair<int, int>]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/11/bits/stl_vector.h:1203:7: note:   candidate expects 1 argument, 2 provided