Submission #823517

#TimeUsernameProblemLanguageResultExecution timeMemory
823517upedKnapsack (NOI18_knapsack)C++14
100 / 100
58 ms3876 KiB
// // Created by MiłoszK on 11.08.2023. // // Two Sets II #include<bits/stdc++.h> using namespace std; using ll = long long; vector<ll> sieve(ll n) { vector<bool> is_prime(n + 1, true); is_prime[0] = is_prime[1] = false; for (ll i = 2; i <= n; i++) { if (is_prime[i] && (long long) i * i <= n) { for (ll j = i * i; j <= n; j += i) is_prime[j] = false; } } vector<ll> primes; for (ll i = 2; i <= n; ++i) { if (is_prime[i]) { primes.push_back(i); } } return primes; } void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } void fileio(const string &name) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } int main() { fastio(); ll s, n; cin >> s >> n; map<int, vector<pair<int, int>>> items; vector<pair<int, int>> vp; for (ll i = 0; i < n; ++i) { int v, w, k; cin >> v >> w >> k; items[w].emplace_back(v, k); } // for (int i = 0; i <= 2000; ++i) { // sort(items[i].begin(), items[i].end(), greater<>()); // } vector<int> dp2(s + 1); for (auto& [w, v] : items) { sort(v.begin(), v.end(), greater<>()); for (int j = s; j >= w; --j) { int cnt_w = 0; int cnt_v = 0; int used = 0; int k = 0; while (k < v.size()) { cnt_w += w; cnt_v += v[k].first; if (j - cnt_w < 0) break; ++used; if (used == v[k].second) { ++k; used = 0; } dp2[j] = max(dp2[j], dp2[j - cnt_w] + cnt_v); } } // for (int i = 0; i <= s; ++i) { // cout << dp2[i] << ' '; // } // cout << '\n'; } cout << dp2[s]; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:58:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |     for (auto& [w, v] : items) {
      |                ^
knapsack.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             while (k < v.size()) {
      |                    ~~^~~~~~~~~~
knapsack.cpp: In function 'void fileio(const string&)':
knapsack.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen((name + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen((name + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...