제출 #659039

#제출 시각아이디문제언어결과실행 시간메모리
659039NONTACKnapsack (NOI18_knapsack)C++11
29 / 100
8 ms15664 KiB
#include <bits/stdc++.h> using namespace std; struct item { int v, w; }; int n, s; item a[100005]; int k[100005]; int dem[20005]; vector<item> b; long long dp[20005][2005]; bool cmp(item &a, item &b) { if(a.w == b.w) return a.v > b.v; return a.w < b.w; } int main() { //freopen("test.in","r",stdin); //freopen("test.out","w",stdout); cin>>s>>n; for(int i = 0; i < n; i++){ cin>>a[i].v>>a[i].w>>k[i]; if(k[i] > s / a[i].w) k[i] = s / a[i].w; } sort(a, a + n, cmp); //for(int i = 0; i < n; i++) cout<<a[i].v<<" "<<a[i].w<<" "<<a[i].k<<endl; for(int i = 1; i <= 2000; i++) dem[i] = (2000 / i); int cnt = 0; b.push_back({0, 0}); for(int i = 0; i < n; i++){ int d = k[i], w = a[i].w; if(dem[w] <= 0) continue; if(d >= dem[w]){ while(dem[w]--) b.push_back({a[i].v, a[i].w}); } else{ dem[w] -= d; while(d--) b.push_back({a[i].v, a[i].w}); } } //print(); //for(int i = 0; i < b.size(); i++) cout<<b[i].w<<" "<<b[i].v<<endl; for(int i = 1; i < b.size(); i++){ long long v = b[i].v, w = b[i].w; for(int j = 0; j <= s; j++){ dp[i][j] = dp[i - 1][j]; if(j - w >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v); } } cout<<dp[b.size() - 1][s]; return 0; }

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:56:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for(int i = 1; i < b.size(); i++){
      |                    ~~^~~~~~~~~~
knapsack.cpp:39:9: warning: unused variable 'cnt' [-Wunused-variable]
   39 |     int cnt = 0;
      |         ^~~
#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...