제출 #709322

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

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   int limit, type_num;
   cin >> limit >> type_num;
   map<int, vector<pair<int, int>>> by_weight;
   for (int i = 1; i <= type_num; ++i) {
      int value, weight, amount;
      cin >> value >> weight >> amount;
      by_weight[weight].emplace_back(value, amount);
   }
   const i64 inf = 1e18;
   vector best(by_weight.size() + 1, vector<i64>(limit + 1, -inf));
   best[0][0] = 0;
   int at = 1;
   for (auto &[weight, items] : by_weight) {
      sort(items.begin(), items.end(), greater<pair<int, int>>());
      for (int sum = 0; sum <= limit; ++sum) {
         best[at][sum] = best[at - 1][sum];
         int copies = 0, type_at = 0, curr_used = 0;
         i64 profit = 0;
         while ((copies + 1) * weight <= sum && type_at < (int) items.size()) {
            copies += 1;
            profit += items[type_at].first;
            best[at][sum] = max(best[at][sum], best[at - 1][sum - copies * weight] + profit);
            curr_used += 1;
            if (curr_used == items[type_at].second) {
               curr_used = 0;
               type_at += 1;
            }
         }
      }
      at += 1;
   }
   cout << *max_element(best.back().begin(), best.back().end()) << '\n';
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 */

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:23:11: error: missing template arguments before 'best'
   23 |    vector best(by_weight.size() + 1, vector<i64>(limit + 1, -inf));
      |           ^~~~
knapsack.cpp:24:4: error: 'best' was not declared in this scope
   24 |    best[0][0] = 0;
      |    ^~~~
knapsack.cpp:26:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   26 |    for (auto &[weight, items] : by_weight) {
      |               ^
knapsack.cpp:22:14: warning: unused variable 'inf' [-Wunused-variable]
   22 |    const i64 inf = 1e18;
      |              ^~~