Submission #791125

#TimeUsernameProblemLanguageResultExecution timeMemory
791125BBart888Knapsack (NOI18_knapsack)C++14
100 / 100
62 ms7328 KiB
#include <iostream> #include <vector> #include <map> #include <set> #include<algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include<iomanip> #include <array> using namespace std; const int MAXN = 1e6+ 11; const int MOD = 1e9+7; using ll = long long; using ld = long double; const int K = 26; ll n,S; ll v[MAXN]; ll weight[MAXN]; ll k[MAXN]; vector<pair<ll, int>> obj[4000]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("cowjog.in", "r", stdin); //freopen("cowjog.out", "w", stdout); cin >> S >> n; for (int i = 0; i < n; i++) cin >> v[i] >> weight[i] >> k[i]; ll mx = *max_element(k, k + n); if (n == 1) cout << v[0] * min(S/ weight[0] , k[0]) << '\n'; else if (1 <= n && n <= 100 && mx <= 10) { ll dp[4000]; memset(dp, 0, sizeof dp); for(int i =0;i<n;i++) for (int w = S; w >= 0; w--) { for (ll j = 1; j <= k[i]; j++) { if (w + j * weight[i] > S) break; if ((dp[w] > 0 || w == 0)) { dp[w + j*weight[i]] = max(dp[w + j*weight[i]], dp[w] + j*v[i]); // cout << w + j * weight[i] << " " << dp[w + j * weight[i]] << '\n'; } } } ll ans = 0; for (int i = 1; i <= S; i++) ans = max(ans, dp[i]); cout << ans << '\n'; } else { ll dp[4000]; ll ans = 0; memset(dp, 0, sizeof dp); for (int i = 0; i < n; i++) obj[weight[i]].push_back({ v[i],k[i] }); for (int i = 1; i <= S; i++) { if (obj[i].size() == 0) continue; sort(obj[i].begin(), obj[i].end(), greater<pair<int, int>>()); int id = 0; for (int j = 1; j * i <= S; j++) { if (id >= obj[i].size()) break; for (int w = S; w >= 0; w--) { if ((w == 0 || dp[w] > 0) && w + i <= S) { dp[w + i] = max(dp[w + i], dp[w] + obj[i][id].first); } } --obj[i][id].second; if (obj[i][id].second == 0) id++; } } for (int i = 1; i <= S; i++) ans = max(ans, dp[i]); cout << ans << '\n'; } return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:113:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |                 if (id >= obj[i].size())
      |                     ~~~^~~~~~~~~~~~~~~~
#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...