제출 #794188

#제출 시각아이디문제언어결과실행 시간메모리
794188jaredKnapsack (NOI18_knapsack)C++14
37 / 100
134 ms262144 KiB
#include <bits/stdc++.h>

#ifdef LOCAL_TEST

#include "debugging.h"

#endif

#ifndef LOCAL_TEST

#define endl '\n'

#endif

using namespace std;

void setio(string filename) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    if (getenv("stdio")) filename = "";
    else if (getenv("local_test")) filename = "test";

    if (!filename.empty()) {
        freopen((filename + ".in").c_str(), "r", stdin);
        freopen((filename + ".out").c_str(), "w", stdout);
    }
}

const int MAX_N = 1e5, MAX_S = 2000;
int s, n, v[MAX_N], w[MAX_N], prek[MAX_N], dp[MAX_N + 1][MAX_S + 1];

int ptr = 0, cnt = 0;
pair<int, int> next_item() {
    cnt++;
    if (cnt > prek[ptr]) ptr++;
    return {v[ptr], w[ptr]};
}

int main() {
    setio("");

    cin >> s >> n;
    for (int i = 0; i < n; ++i) {
        cin >> v[i] >> w[i] >> prek[i];

        if (i > 0) prek[i] += prek[i - 1];
    }

    for (int i = 1; i <= prek[n - 1]; ++i) { // item index
        auto [cur_v, cur_w] = next_item();

        for (int j = 0; j <= s; ++j) { // weight
            dp[i][j] = max(dp[i - 1][j],
                           j - cur_w >= 0 ? dp[i - 1][j - cur_w] + cur_v : -1);
        }
    }
    cout << dp[prek[n - 1]][s];
}

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:51:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   51 |         auto [cur_v, cur_w] = next_item();
      |              ^
knapsack.cpp: In function 'void setio(std::string)':
knapsack.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         freopen((filename + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         freopen((filename + ".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...