제출 #585814

#제출 시각아이디문제언어결과실행 시간메모리
585814imtiyazrasool92Knapsack (NOI18_knapsack)C++17
컴파일 에러
0 ms0 KiB
#pragma GCC optimize("O1")

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun> class y_combinator_result {
    Fun fun_;
public:
    template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
    template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }

using namespace std;

#ifdef imtiyazrasool92
#include "algos/debug.h"
#else
#define dbg(...) 92
#endif

int S, N;
// int dp[100000][2001];
map<pair<int, int>, int> dp;
int A[100000][3];

int dfs(int index, int weight) {
    if (weight == 0)
        return 0;

    if (index >= N)
        return 0;

    if (dp.find({index, weight}) == dp.end()) {
        int answer = 0;
        for (int i = 0; i <= min(weight / A[index][1], A[index][2]); i++) {
            if (weight - (i * A[index][1]) >= 0)
                answer = max(answer, dfs(index + 1, weight - (i * A[index][1])) + A[index][0] * i);
            else
                break;
        }
        dp[ {index, weight}] = answer;
    }

    return dp[ {index, weight}];
}

void run_case() {
    cin >> S >> N;

    for (int i = 0; i < N; i++) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];
        if (A[i][1] > S) {
            N--;
            i--;
        } else if (A[i][1] == 0) {
            N--;
            i--;
        }
    }

    for (int i = 0; i < N; i++)
        for (int s = 0; s <= S; s++)
            dp[i][s] = -1;

    cout << dfs(0, S);
}

int32_t main() {
    ios::sync_with_stdio(false);
#ifndef imtiyazrasool92
    cin.tie(nullptr);
#endif

    int tests = 1;
    // cin >> tests;

    while (tests--) {
        run_case();
        cout << '\n';
    }

    return 0;
}

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

knapsack.cpp: In function 'void run_case()':
knapsack.cpp:80:15: error: no match for 'operator[]' (operand types are 'std::map<std::pair<int, int>, int>' and 'int')
   80 |             dp[i][s] = -1;
      |               ^
In file included from /usr/include/c++/10/map:61,
                 from knapsack.cpp:14:
/usr/include/c++/10/bits/stl_map.h:492:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]'
  492 |       operator[](const key_type& __k)
      |       ^~~~~~~~
/usr/include/c++/10/bits/stl_map.h:492:34: note:   no known conversion for argument 1 from 'int' to 'const key_type&' {aka 'const std::pair<int, int>&'}
  492 |       operator[](const key_type& __k)
      |                  ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_map.h:512:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]'
  512 |       operator[](key_type&& __k)
      |       ^~~~~~~~
/usr/include/c++/10/bits/stl_map.h:512:29: note:   no known conversion for argument 1 from 'int' to 'std::map<std::pair<int, int>, int>::key_type&&' {aka 'std::pair<int, int>&&'}
  512 |       operator[](key_type&& __k)
      |                  ~~~~~~~~~~~^~~