Submission #673804

#TimeUsernameProblemLanguageResultExecution timeMemory
673804ThinGarfieldKnapsack (NOI18_knapsack)C++11
100 / 100
153 ms35148 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define mp make_pair #define F first #define S second int main() { int limit; int type_num; cin >> limit >> type_num; // grouping elements by weight map<int, vector<pair<int, int>>> by_weight; for (int t = 0; t < type_num; t++) { int value; int weight; int amt; cin >> value >> weight >> amt; amt = min(amt, limit / weight); if (weight <= limit && amt > 0) { by_weight[weight].push_back({value, amt}); } } /* * best[i][j] contains the most value we can * get using j weight and the first i weight types */ vector<vector<long long>> best(by_weight.size() + 1, vector<long long>(limit + 1, INT32_MIN)); best[0][0] = 0; int at = 1; for (auto& [w, items] : by_weight) { // sort items of given weight in reverse order by value sort(items.rbegin(), items.rend()); for (int i = 0; i <= limit; i++) { best[at][i] = best[at - 1][i]; int copies = 0; int type_at = 0; int curr_used = 0; long long profit = 0; // go through as many items until we run out of items or usable weight while ((copies + 1) * w <= i && type_at < items.size()) { copies++; profit += items[type_at].first; if (best[at - 1][i - copies * w] != INT32_MIN) { best[at][i] = max(best[at][i], best[at - 1][i - copies * w] + profit); } curr_used++; if (curr_used == items[type_at].second) { curr_used = 0; type_at++; } } } at++; } cout << *max_element(best.back().begin(), best.back().end()) << endl; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |   for (auto& [w, items] : by_weight) {
      |              ^
knapsack.cpp:44:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |       while ((copies + 1) * w <= i && type_at < items.size()) {
      |                                       ~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...