제출 #507538

#제출 시각아이디문제언어결과실행 시간메모리
507538Saeed_247Knapsack (NOI18_knapsack)C++14
100 / 100
94 ms33152 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int s, n;
    cin >> s >> n;
    map<int, vector<pair<int, int>>> mp;
    for (int i = 1; i <= n; i++) {
        int v, w, a;
        cin >> v >> w >> a;
        if (w <= s && a > 0) {
            mp[w].push_back({v, a});
        }
    }
    vector<vector<long long>> dp(mp.size() + 1, vector<long long>(s + 1, INT_MIN));
    dp[0][0] = 0;
    int i = 1;
    for (auto& [w, items] : mp) {
        sort(items.begin(), items.end(), greater<pair<int, int>>());
        for (int j = 0; j <= s; j++) {
            dp[i][j] = dp[i - 1][j];
            int amt = 0, curr = 0, used = 0;
            long long x = 0;
            while ((amt + 1) * w <= j && curr < items.size()) {
                amt++;
                x += items[curr].first;
                if (dp[i - 1][j - amt * w] != INT_MIN) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - amt * w] + x);
                }
                used++;
                if (used == items[curr].second) {
                    used = 0;
                    curr++;
                }
            }
        }
        i++;
    }
    cout << *max_element(dp.back().begin(), dp.back().end()) << '\n';
    return 0;
}

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:19:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   19 |     for (auto& [w, items] : mp) {
      |                ^
knapsack.cpp:25:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |             while ((amt + 1) * w <= j && curr < items.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...