Submission #787001

#TimeUsernameProblemLanguageResultExecution timeMemory
787001PekibanKnapsack (NOI18_knapsack)C++17
100 / 100
225 ms140860 KiB
#include <bits/stdc++.h>
#include <sys/ucontext.h>
#include <vector>

using namespace std;

bool cmp(array<int, 3> a, array<int, 3> b) {
    if (a[1] != b[1])   return a[1] < b[1];
    return a[0] > b[0];
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, s;
    cin >> s >> n;
    array<int, 3> a[n];                                                                                                               
    for (int i = 0; i < n; ++i) {
        cin >> a[i][0] >> a[i][1] >> a[i][2];
    }
    sort(a, a+n, cmp);
    vector<int> bx[s+1];
    int sz = 0;
    for (int i = 0; i < n; ++i) {
        int d = (s+a[i][1]-1)/a[i][1];
        while (bx[a[i][1]].size() < d && a[i][2]) {
            bx[a[i][1]].push_back(a[i][0]);
            a[i][2]--;
            sz++;
        }
    }
    int x[sz], y[sz];
    int dp[sz][s+1];
    for (int i = 0; i < sz; ++i) {
        for (int j = 0; j <= s; ++j) {
            dp[i][j] = 0;
        }
    }
    int cnt = 0;
    for (int i = 1; i <= s; ++i) {
        for (auto u : bx[i]) {
            x[cnt] = u;
            y[cnt++] = i;
        }
    }
    // for (int i = 0; i < sz; ++i) {
    //     cout << x[i] << ' ' << y[i] << '\n';
    // }
    //
    // 1)stvorimo jednostavniji niz
    // 2) resimo laksi problem
    dp[0][0] = 0;
    for (int i = 0; i < sz; ++i) {
        for (int j = 1; j <= s; ++j) {
            dp[i][j] = max((i==0?0:dp[i-1][j]), dp[i][j-1]);
            if (j >= y[i]) {
                dp[i][j] = max(dp[i][j], (i == 0?0:dp[i-1][j-y[i]])+x[i]);
            }
        }
    }
    // for (int i = 0; i < sz; ++i) {
    //     for (int j = 0; j <= s; ++j) {
    //         cout << dp[i][j] << ' ';
    //     }
    //     cout << '\n';
    // }
    cout << dp[sz-1][s] << '\n';
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:25:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   25 |         while (bx[a[i][1]].size() < d && a[i][2]) {
      |                ~~~~~~~~~~~~~~~~~~~^~~
#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...