Submission #1047923

#TimeUsernameProblemLanguageResultExecution timeMemory
1047923vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
31 ms35012 KiB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using i64 = long long;
const int MAXN = 1e5 + 5;
const int MAXV = 2e3 + 5;
#define ALL(a) (a).begin(), (a).end()
 
struct PRODUCT {
    int c, w, x;
    PRODUCT(int c = 0, int w = 0, int x = 0) : c(c), w(w), x(x) {}
};
 
int N, S;
PRODUCT a[MAXN];
vector<pii> g[MAXV];
vector<int> b[MAXV];
i64 dp[MAXV][MAXV];
 
namespace subtask {
    const int MAXN = 105;
    const int MAXV = 2e3 + 5;
    i64 dp[MAXN][MAXV];
 
    void solution() {
        for(int i = 1; i <= N; i++) {
            auto [c, w, x] = a[i];
            for(int j = 0; j <= S; j++) dp[i][j] = dp[i - 1][j];
            for(int z = 0; z <= x and z * w <= S; z++) {
                for(int j = S; j >= w * z; j--)
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - w * z] + 1LL * c * z);
            }
        }
        i64 ans = 0;
        for(int i = 1; i <= S; i++)
            ans = max(ans, dp[N][i]);
        cout << ans;
    }
}
 
signed main() {
#define TASK "code"
 
    if (fopen(TASK ".inp", "r")) {
        freopen(TASK ".inp", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }
 
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    cin >> S >> N;
    for(int i = 1; i <= N; i++) cin >> a[i].c >> a[i].w >> a[i].x;
    for(int i = 1; i <= N; i++) {
        auto [c, w, x] = a[i];
        g[w].emplace_back(c, x);
    }
    for(int i = 1; i <= S; i++) sort(ALL(g[i]), [&](const pii x, const pii y) {
        if(x.first != y.first) return x.first > y.first;
        return x.second > y.second;
    });
    for(int i = 1; i <= S; i++) {
        if(g[i].empty()) continue;
        int j = 0;
        for(int z = 1; z <= S; z++) {
            if(g[i][j].second == 0)
                j++;
            if(j == g[i].size()) break;
            b[i].emplace_back(g[i][j].first);
            g[i][j].second--;
            if(b[i].size() * i > S) break;
        }
    }
//    for(int i = 1; i <= S; i++) {
//        if(b[i].empty()) continue;
//        cout << i << ": ";
//        for(int x : b[i]) cout << x << " ";
//        cout << "\n";
//    }
    for(int i = 1; i <= S; i++) {
        i64 cur = 0;
        for(int &x : b[i]) {
            cur += x;
            x = cur;
        }
    }
    for(int w = 1; w <= S; w++) {
        for(int j = 0; j <= S; j++) dp[w][j] = dp[w - 1][j];
        for(int i = 0; i < b[w].size(); i++) {
            for(int j = S; j >= (i + 1) * w; j--)
                dp[w][j] = max(dp[w][j], dp[w - 1][j - (i + 1) * w] + b[w][i]);
        }
    }
    i64 ans = 0;
    for(int j = 0; j <= S; j++) ans = max(ans, dp[S][j]);
    cout << ans;
    return (0 ^ 0);
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |             if(j == g[i].size()) break;
      |                ~~^~~~~~~~~~~~~~
knapsack.cpp:72:32: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |             if(b[i].size() * i > S) break;
      |                ~~~~~~~~~~~~~~~~^~~
knapsack.cpp:90:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for(int i = 0; i < b[w].size(); i++) {
      |                        ~~^~~~~~~~~~~~~
knapsack.cpp:45:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         freopen(TASK ".out", "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...