Submission #1218617

#TimeUsernameProblemLanguageResultExecution timeMemory
1218617pauloaphKnapsack (NOI18_knapsack)C++20
Compilation error
0 ms0 KiB
#include<iostream>
#include<algorithm>
#include <cmath>
using namespace std;

const int MAXN = 300;
const int MAXC = 1000;

int main(){

    int C;
    int n;

    cin >> C >> n;

    int V[n];
    int W[n];
    int B[n];
    long int total = n;

    for (int i = 0; i < n; i++){
        cin >> V[i] >> W[i] >> B[i];
        total += log2(B[i]);
    }

    long int NW[total];
    long int NV[total];
    long int M = 0;


    // vamos criar os novos itens (pacotes)
    for ( int i = 0; i < n; ++i ){
        int pos = 0;
        // procuramos o bit mais significativo
        while ( (1 << pos) < B[i] )
            pos++;
        // aqui sabemos que (2 ^ pos) >= B[i]
        pos--;
        // agora 2 ^ pos < B[i], logo (2 ^ pos) - 1 <= B[i] 
        while ( pos >= 0 ){
            NW[M] = (1 << pos) * W[i];
            NV[M] = (1 << pos) * V[i];
            B[i] -= (1 << pos);
            M++;
            pos--;
        }
        if ( B[i] ){ // se sobrou um resto
            NW[M] = B[i] * W[i];
            NV[M] = B[i] * V[i];
            M++;
            B[i] = 0;
        }
    }

    int dp[total + 1][C + 1];

   // caso base : quando nao temos mais itens
   for ( int c = 0; c <= C; ++c )
      dp[M][c] = 0;

   for ( int i = M - 1; i >= 0; --i )
      for ( int c = 0; c <= C; ++c ){
         dp[i][c] = dp[i + 1][c];
         if ( NW[i] <= c )
            dp[i][c] = max( dp[i][c], dp[i + 1][c - NW[i]] + NV[i] );
      }

   cout << dp[0][C] << endl;
   return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:65:27: error: no matching function for call to 'max(int&, long int)'
   65 |             dp[i][c] = max( dp[i][c], dp[i + 1][c - NW[i]] + NV[i] );
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/bits/char_traits.h:39,
                 from /usr/include/c++/11/ios:40,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from knapsack.cpp:1:
/usr/include/c++/11/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/11/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
knapsack.cpp:65:27: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long int')
   65 |             dp[i][c] = max( dp[i][c], dp[i + 1][c - NW[i]] + NV[i] );
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/bits/char_traits.h:39,
                 from /usr/include/c++/11/ios:40,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from knapsack.cpp:1:
/usr/include/c++/11/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  300 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/11/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
knapsack.cpp:65:27: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long int')
   65 |             dp[i][c] = max( dp[i][c], dp[i + 1][c - NW[i]] + NV[i] );
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/string:52,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from knapsack.cpp:1:
/usr/include/c++/11/bits/stl_algo.h:3461:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3461 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/11/bits/stl_algo.h:3461:5: note:   template argument deduction/substitution failed:
knapsack.cpp:65:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   65 |             dp[i][c] = max( dp[i][c], dp[i + 1][c - NW[i]] + NV[i] );
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/string:52,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from knapsack.cpp:1:
/usr/include/c++/11/bits/stl_algo.h:3467:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3467 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/11/bits/stl_algo.h:3467:5: note:   template argument deduction/substitution failed:
knapsack.cpp:65:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   65 |             dp[i][c] = max( dp[i][c], dp[i + 1][c - NW[i]] + NV[i] );
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~