제출 #1131888

#제출 시각아이디문제언어결과실행 시간메모리
1131888ngtlongKnapsack (NOI18_knapsack)C++20
0 / 100
1 ms324 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task ""
#define mas(a, n) a + 1, a + n + 1
#define cts(a) a.begin(), a.end()
using namespace std;
const int narr = 1e2 + 5;

void testcase()
{
    #ifndef ONLINE_JUDGE
    freopen("tmp.inp", "r", stdin);
    freopen("tmp.out", "w", stdout);
    #endif
}

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

ll ans(ll n, ll w, ll W[], ll c[], ll a[])
{
    vector<ll> dp(w + 1, 0);
    ll wi, ci, ui, v;
    for (ll i = 1; i <= n; i++)
    {
        wi = W[i], ci = c[i], ui = a[i];
        if (wi > w)
            continue;
        for (ll j = 0; j < wi; j++)
        {
            deque<pair<ll, ll>> dq;
            for (ll curw = j; curw <= w; curw += wi)
            {
                v = dp[curw] - (curw / wi) * ci;
                while (!dq.empty() && dq.back().first <= v)
                    dq.pop_back();
                dq.emplace_back(v, curw);
                ll maxi = ui * wi;
                if (!dq.empty() && dq.front().second < curw - maxi)
                    dq.pop_front();
                dp[curw] = dq.front().first + (curw / wi) * ci;
            }
        }
    }
    return dp[w];
}

ll W[narr], c[narr], a[narr], n, w;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    testcase();
    // readfo();
    cin >> w >> n;
    for (int i = 1; i <= n; i++)
        cin >> c[i] >> W[i] >> a[i];
    cout << ans(n, w, W, c, a);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    return 0;
}

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

knapsack.cpp: In function 'void testcase()':
knapsack.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen("tmp.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     freopen("tmp.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void readfo()':
knapsack.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen(task ".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     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...