Submission #768653

#TimeUsernameProblemLanguageResultExecution timeMemory
768653mohamedsiuKnapsack (NOI18_knapsack)C++14
100 / 100
59 ms3728 KiB
#include<bits/stdc++.h> //#define int long long #define pii pair<int,int> #define fi first #define se second using namespace std; using ll = long long; const int maxn = 2e3 + 5; const int maxN = 1e5 + 5; const int mod = 1e9 + 7; const int inf = 2e9 + 7; const int tt = 250; int dx[] = {0,-1,0,1,-1,1,-1,1}; int dy[] = {-1,0,1,0,-1,1,1,-1}; struct Item { int val; int wei; int cnt; }; Item a[maxN]; bool functor(Item p,Item q) { return p.val > q.val; } int S,n; vector<int> vc[maxn]; int dp[2][maxn]; int32_t main() { // freopen("test.inp","r",stdin); // freopen("test.out","w",stdout); ios_base::sync_with_stdio(0); cin.tie(0); cin >> S >> n; for(int i = 1;i <= n;i++) { cin >> a[i].val >> a[i].wei >> a[i].cnt; } sort(a + 1,a + n + 1,functor); for(int i = 1;i <= n;i++) { while(vc[a[i].wei].size() < (S/a[i].wei) && a[i].cnt > 0) { vc[a[i].wei].push_back(a[i].val); a[i].cnt--; } } dp[0][0] = 0; for(int i = 1;i <= S;i++) { int now = i%2; int pre = 1 - now; for(int j = 0;j <= S;j++) { dp[now][j] = dp[pre][j]; int cnt = 0; int sum = 0; for(int k: vc[i]) { cnt++; sum += k; if(cnt*i > j) break; dp[now][j] = max(dp[now][j],dp[pre][j - cnt*i] + sum); } } } cout << dp[S%2][S]; }

Compilation message (stderr)

knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:52:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |         while(vc[a[i].wei].size() < (S/a[i].wei) && a[i].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...