Submission #803214

#TimeUsernameProblemLanguageResultExecution timeMemory
803214rocketsriKnapsack (NOI18_knapsack)C++17
12 / 100
1 ms340 KiB
#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; int main() { int m, n; cin >> m >> n; vector<long long> a(n); vector<long long> w(n); vector<long long> freq(n); unordered_map<long long, vector<pair<long long, long long>>> pairs; // (weight, {{value, frequency}}) for (int i = 0; i < n; i++) { cin >> a[i] >> w[i] >> freq[i]; } for (int i = 0; i < n; i++) { if (pairs.find(w[i]) != pairs.end()) { pairs[w[i]].push_back({a[i], freq[i]}); } else { pairs[w[i]] = {{a[i], freq[i]}}; } } vector<long long> dp(m + 1, -1); dp[0] = 0; for (auto& entry : pairs) { long long weight = entry.first; auto& curr = entry.second; sort(curr.begin(), curr.end(), [](const pair<long long, long long>& x, const pair<long long, long long>& y) { if (x.first > y.first) { return 1; } else if (x.first < y.first) { return -1; } else { return 0; } }); reverse(curr.begin(), curr.end()); } for (auto& entry : pairs) { long long weight = entry.first; auto& curr = entry.second; long long sum = 0; for (const auto& j : curr) { sum += j.second; } for (long long i = m; i >= weight; i--) { long long f = i / weight; if (dp[i - min(f, sum) * weight] != -1) { long long c = dp[i - min(f, sum) * weight]; for (const auto& j : curr) { long long fr = j.second, val = j.first; if (fr >= f) { c += f * val; f = 0; break; } else { c += fr * val; f -= fr; } } dp[i] = max(dp[i], c); } } } long long max_val = 0; for (int i = 1; i <= m; i++) { max_val = max(max_val, dp[i]); } cout << max_val << endl; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:32:19: warning: unused variable 'weight' [-Wunused-variable]
   32 |         long long weight = entry.first;
      |                   ^~~~~~
#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...