Submission #975083

#TimeUsernameProblemLanguageResultExecution timeMemory
975083allwKnapsack (NOI18_knapsack)C++17
Compilation error
0 ms0 KiB
#include <iostream> #include <vector> int main() { int s, n; std::cin >> s >> n; std::vector<std::vector<int>> items(n, std::vector<int>(3)); // s in [1, 2000] for (int i = 0; i < n; ++i) { std::cin >> items[i][0] >> items[i][1] >> items[i][2]; // cost, weight, count } std::sort(items.begin(), items.end(), std::greater<std::vector<int>>()); std::vector<std::vector<int>> a(2001, std::vector<int>()); for (int i = 0; i < n; ++i) { int cnt = items[i][2]; int w = items[i][1]; int max_items = 2000 / w; while (cnt > 0 && a[w].size() < max_items) { a[w].push_back(items[i][0]); cnt--; } } std::vector<std::vector<int>> items2; for (int i = 0; i < a.size(); ++i) { for (int j = 0; j < a[i].size(); ++j) { items2.push_back({i, a[i][j]}); // w, cost } } std::vector<int> dp(s + 1, INT_MIN); dp[0] = 0; for (int i = 0; i < items2.size(); ++i) { int w = items2[i][0]; int c = items2[i][1]; std::vector<int> new_dp = dp; for (int j = 0; j < dp.size(); ++j) { if (dp[j] >= 0 && j + w < dp.size()) { new_dp[j + w] = std::max(new_dp[j + w], dp[j] + c); } } dp = new_dp; } int mmax = 0; for (int i = 0; i < dp.size(); ++i) { mmax = std::max(dp[i], mmax); } std::cout << mmax << '\n'; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:14:10: error: 'sort' is not a member of 'std'
   14 |     std::sort(items.begin(), items.end(), std::greater<std::vector<int>>());
      |          ^~~~
knapsack.cpp:22:39: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   22 |         while (cnt > 0 && a[w].size() < max_items) {
      |                           ~~~~~~~~~~~~^~~~~~~~~~~
knapsack.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < a.size(); ++i) {
      |                     ~~^~~~~~~~~~
knapsack.cpp:30:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         for (int j = 0; j < a[i].size(); ++j) {
      |                         ~~^~~~~~~~~~~~~
knapsack.cpp:36:32: error: 'INT_MIN' was not declared in this scope
   36 |     std::vector<int> dp(s + 1, INT_MIN);
      |                                ^~~~~~~
knapsack.cpp:3:1: note: 'INT_MIN' is defined in header '<climits>'; did you forget to '#include <climits>'?
    2 | #include <vector>
  +++ |+#include <climits>
    3 | 
knapsack.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < items2.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~~
knapsack.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for (int j = 0; j < dp.size(); ++j) {
      |                         ~~^~~~~~~~~~~
knapsack.cpp:45:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |             if (dp[j] >= 0 && j + w < dp.size()) {
      |                               ~~~~~~^~~~~~~~~~~
knapsack.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     for (int i = 0; i < dp.size(); ++i) {
      |                     ~~^~~~~~~~~~~