제출 #493262

#제출 시각아이디문제언어결과실행 시간메모리
493262HaYoungJoonKnapsack (NOI18_knapsack)C++14
100 / 100
83 ms3036 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; ll dp[2001]; typedef pair<ll,ll> pii; vector<pii > pre[2001],items; bool cmp(pii A, pii B) { return A.first > B.first; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,S; cin >> S >> n; ll v,k,w; for (int i = 1; i <= n; i++) { cin >> v >> w >> k; pre[w].push_back({v,k}); } items.push_back({0,0}); for (int w = 1; w <= S; w++) { if (pre[w].empty()) continue; sort(pre[w].begin(),pre[w].end(),cmp); ll weight = S; for (int i = 0; i < pre[w].size() && weight > 0; i++) { for (int j = 1; j <= pre[w][i].second && weight > 0; j++) { weight -= w; if (weight < 0) break; items.push_back({w,pre[w][i].first}); } } } for (int i = 1; i < items.size(); i++) for (int j = S; j >= 0; j--) if (j - items[i].first >= 0) dp[j] = max(dp[j],dp[j - items[i].first ] + items[i].second); ll res = 0; for (int i = 1; i <= S; i++) res = max(res,dp[i]); cout << res; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'int main()':
knapsack.cpp:30:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         for (int i = 0; i < pre[w].size() && weight > 0; i++) {
      |                         ~~^~~~~~~~~~~~~~~
knapsack.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i = 1; 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...