제출 #848814

#제출 시각아이디문제언어결과실행 시간메모리
848814vjudge1Knapsack (NOI18_knapsack)C++11
100 / 100
71 ms35112 KiB
/*Objective: HSG-VNUHCM TST 2023 -> VOI 2024*/
/*
    me zeena
    school: HSG-VNUHCM (PTNK)
    class: i2225
    training VOI 2024 & 2025
    goal: APCS (HCMUS)
    i suck at dp, adhoc and graphs 
*/
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>

/* unnecessary defines */ 
#define F first
#define S second
#define elif else if
#define FOR(i,a,b) for (int i=(a); i<=(int)(b); i++)
#define FOD(i,b,a) for (int i=(b); i>=(int)(a); i--)
#define rep(i,n) for (int i=0; i<(int)(n); i++)
#define rev(i,n) for (int i=(int)(n)-1; i>=0; i--)
#define iall(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define all(a,n,i) (a)+(i), (a)+(n)+(i)
#define psb push_back
#define ppb pop_back()
#define psf push_front
#define ppf pop_front()
#define nl '\n'
#define mask(i) (1LL<<(i))

typedef long long ll;
typedef long double ld;
/*end of this shit*/

using namespace std;

#define task ""

void frepfrep() {
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
}

const bool CF = 0;

const int maxN = 2e5+5;
const int mod = 1e9+7;
const int maxS = 2000;
map<int, vector<pair<int,int>>> a;
int n, s;

void solve() {
    cin >> s >> n;
    rep(i,n) {
        int v,w,k; cin >> v >> w >> k;
        if (w<=s && k>0) {
            a[w].psb({v,k});
        }
    }
    vector<vector<ll>> dp(a.size()+1, vector<ll>(s+1, -1));
    int x = 1;
    dp[0][0] = 0;
    for (auto &[w, b]: a) {
        sort(iall(b), greater<pair<int,int>>());
        FOR(i,0,s) {
            dp[x][i] = dp[x-1][i];
            int k = 0;
            int x2 = 0;
            int curused = 0;
            ll val = 0;
            while ((k+1)*w <= i && x2 < b.size()) {
                k++;
                val+=b[x2].F;
                if (dp[x-1][i-k*w] != -1) {
                    dp[x][i] = max(dp[x][i], dp[x-1][i-k*w] + val);
                }

                curused++;
                if (curused == b[x2].S) curused=0, x2++; 
            }
        }
        x++;
    }
    cout << *max_element(iall(dp.back()));
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    frepfrep();
    if (CF) {
        int t; cin >> t;
        while (t--) solve();
        return 0;
    }
    solve();
}

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

knapsack.cpp: In function 'void solve()':
knapsack.cpp:66:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   66 |     for (auto &[w, b]: a) {
      |                ^
knapsack.cpp:74:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |             while ((k+1)*w <= i && x2 < b.size()) {
      |                                    ~~~^~~~~~~~~~
knapsack.cpp: In function 'void frepfrep()':
knapsack.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:43:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         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...