Submission #746398

#TimeUsernameProblemLanguageResultExecution timeMemory
746398JasonTaiwanKnapsack (NOI18_knapsack)C++14
100 / 100
168 ms5068 KiB
#include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; struct item { long long weight; long long worth; long long cnt; }; bool comp(item i1, item i2) { if (i1.weight == i2.weight) { return (i1.worth > i2.worth); } return (i1.weight < i2.weight); } int main() { long long S, N; long long cnt; cin >> S >> N; vector < item > items; for (int i = 0; i < N; i++) { long long wei, wor; cin >> wor >> wei; cin >> cnt; items.push_back({wei, wor, cnt}); } sort(items.begin(), items.end(), comp); vector < item > new_item; map < int, int > myMap; for (auto i: items) { if (myMap[i.weight] * i.weight > S) { continue; } myMap[i.weight] += i.cnt; new_item.push_back(i); } swap(new_item, items); // new_item是去掉重複的 long long dp[S + 1] = {0}; for (int i = 0; i < items.size(); i++) { for (int j = S; j >= 0; j--) { int cntr = 0; while (cntr * items[i].weight <= j && cntr <= items[i].cnt) { dp[j] = max(dp[j], dp[j - cntr * items[i].weight] + cntr * items[i].worth); cntr++; } } } cout << dp[S] << endl; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (int i = 0; i < items.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
#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...