Submission #683702

#TimeUsernameProblemLanguageResultExecution timeMemory
683702vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
118 ms17916 KiB
#include <bits/stdc++.h>

using namespace std;

#define task "a"
#define etr "\n"
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define bg begin
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define lwb lower_bound
#define upb upper_bound
#define range(x, l, r) x+l, x+1+r
#define all(x) (x).bg(), (x).end()
#define compact(x) x.resize(unique(all(x)) - (x).bg())
#define sq(x) ((x)*(x))

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

const int N=1e5, M=2e3, mod=1e9+7;

int n, s;
ll v[N*10+5], w[N*10+5], dp[M+5];
multiset<ll> a[M+5];

void process()
{
	cin >> s >> n;
	for (int i=1; i<=n; i++)
    {
        ll k;
        cin >> v[i] >> w[i];
        cin >> k;
        k = min(k, s/w[i]);

        int bit = 0;
        while (true)
        {
            if (k >= (1 << bit))
            {
                v[i+bit] = v[i] * (1 << bit);
                w[i+bit] = w[i] * (1 << bit);
                k -= (1 << bit);
            } else
            {
                v[i+bit] = v[i] * k;
                w[i+bit] = w[i] * k;
                k -= k;
            }

            a[w[i+bit]].insert(v[i+bit]);
            if (a[w[i+bit]].size() > s/w[i+bit]) a[w[i+bit]].erase(a[w[i+bit]].bg());

            if (k == 0) break;
            bit++;
        }

        i += bit;
        n += bit;
    }

    int idx=0;
    for (int i=1; i<=s; i++)
    {
        for (ll x : a[i])
        {
            w[++idx] = i;
            v[idx] = x;
        }
    }

    ll res=0;
    for (int i=1; i<=idx; i++)
    {
        for (int j=s; j>=w[i]; j--)
        {
            dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
            res = max(res, dp[j]);
        }
    }

    cout << res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int t=1; //cin >> t;
	while (t--) process();

	return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void process()':
knapsack.cpp:64:36: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   64 |             if (a[w[i+bit]].size() > s/w[i+bit]) a[w[i+bit]].erase(a[w[i+bit]].bg());
      |                 ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
knapsack.cpp: In function 'void freop()':
knapsack.cpp:28:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |  freopen(task".inp", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:29:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |  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...