제출 #1321486

#제출 시각아이디문제언어결과실행 시간메모리
1321486NgTrung2217Knapsack (NOI18_knapsack)C++20
100 / 100
40 ms5340 KiB
#include <bits/stdc++.h>

#define task ""
#define ff first
#define ss second

using namespace std;

using ll = long long;
using pii = pair <int, int>;
using pll = pair <ll, ll>;

const char el = '\n';
const char sp = ' ';
const int maxS = 2005;
const int maxN = 1e5 + 5;

ll s, n;
ll v[maxN], w[maxN], k[maxN];
ll dp[maxS];

void solve ()
{
    if (!(cin >> s >> n)) return;
    for (int i = 1;i <= n;++i)
        cin >> v[i] >> w[i] >> k[i];

    ll max_k = 0;
    for (int i = 1;i <= n;++i)
        max_k = max(max_k, k[i]);

    if (n == 1)
    {
        cout << v[1] * min(s / w[1], k[1]) << el;
    }
    else if (n <= 100 && max_k <= 10)
    {
        for (int i = 0;i <= s;++i) dp[i] = 0;
        for (int i = 1;i <= n;++i)
        {
            for (int j = s;j >= w[i];--j)
            {
                for (int t = 0;t <= k[i];++t)
                {
                    if (j >= w[i] * t)
                    {
                        dp[j] = max(dp[j], dp[j - w[i] * t] + v[i] * t);
                    }
                }
            }
        }
        cout << dp[s] << el;
    }
    else
    {
        vector <pll> obj[maxS];
        for (int i = 0;i <= s;++i) dp[i] = 0;
        for (int i = 1;i <= n;++i)
        {
            if (w[i] <= s) obj[w[i]].push_back({v[i], k[i]});
        }

        for (int i = 1;i <= s;++i)
        {
            if (obj[i].empty()) continue;
            sort(obj[i].begin(), obj[i].end(), greater <pll>());

            int id = 0;
            for (int count = 0; count * i < s; ++count)
            {
                if (id >= (int)obj[i].size()) break;
                for (int j = s; j >= i; --j)
                {
                    dp[j] = max(dp[j], dp[j - i] + obj[i][id].ff);
                }
                obj[i][id].ss--;
                if (obj[i][id].ss == 0) id++;
            }
        }
        cout << dp[s] << el;
    }
}

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    solve();

    return 0;
}

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         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...