제출 #709310

#제출 시각아이디문제언어결과실행 시간메모리
709310nima_aryanKnapsack (NOI18_knapsack)C++14
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx2") #ifdef LOCAL #include "algo/debug.h" #endif using i64 = long long; 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)); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int S, N; cin >> S >> N; vector<int> V(N + 1), W(N + 1), K(N + 1); for (int i = 1; i <= N; ++i) { cin >> V[i] >> W[i] >> K[i]; } const int maxb = 18; vector<pair<int, int>> items; for (int i = 1; i <= N; ++i) { for (int bit = 0; bit <= maxb; ++bit) { if (K[i] >> bit & 1) { items.emplace_back(i, 1 << bit); } } } const i64 inf = 1e18; const int num = (int) items.size(); vector dp(num + 1, vector<i64>(S + 1, -inf)); dp[0][0] = 0; for (int i = 1; i <= num; ++i) { auto [idx, cnt] = items.at(i - 1); for (int sum = 0; sum <= S; ++sum) { dp[i][sum] = dp[i - 1][sum]; i64 opt = (i64) sum - (i64) cnt * W[idx]; if (opt >= 0) { dp[i][sum] = max(dp[i][sum], dp[i - 1][opt] + (i64) cnt * V[idx]); } } } i64 ans = 0; for (int sum = 0; sum <= S; ++sum) { ans = max(ans, dp.back().at(sum)); } cout << ans << '\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:52:11: error: missing template arguments before 'dp'
   52 |    vector dp(num + 1, vector<i64>(S + 1, -inf));
      |           ^~
knapsack.cpp:53:4: error: 'dp' was not declared in this scope
   53 |    dp[0][0] = 0;
      |    ^~
knapsack.cpp:55:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   55 |       auto [idx, cnt] = items.at(i - 1);
      |            ^
knapsack.cpp:50:14: warning: unused variable 'inf' [-Wunused-variable]
   50 |    const i64 inf = 1e18;
      |              ^~~