Submission #701355

# Submission time Handle Problem Language Result Execution time Memory
701355 2023-02-21T03:55:18 Z tgp072 Knapsack (NOI18_knapsack) C++14
100 / 100
55 ms 2964 KB
//#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>

using namespace std;

//change the long long to int if you need to save memory/time really badly
typedef long long ll;

//Comment this define when working on interactive problems
#define endl "\n"
#define sqrt(n) sqrt((long double) n)

const ll MAXN = 2e5 + 5;
const ll ZER = 0;
const ll ONE = 1;
const ll INF = LLONG_MAX;
const ll MOD = 1e9 + 7;

ll min(ll a, ll b) {
    if (a < b) {
        return a;
    } else {
        return b;
    }
}
 
ll max(ll a, ll b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

ll mod(ll num) {
    return (num >= MOD ? num % MOD : num);
}

ll __gcd(ll a, ll b) {
    if (b == 0) {
        return a;
    }
    
    if (a == 0) {
        return b;
    }
    
    if (a < b) {
        swap(a, b);
    }
    
    return __gcd(b, a % b);
}

ll __lcm(ll a, ll b) {
    ll prod = a*b;
    return prod/__gcd(a, b);
}

ll powmod(ll a, ll b){
    a %= MOD;
    if (a == 0) return 0;
    ll product = 1;
    while(b > 0){
        if (b&1){    // you can also use b % 2 == 1
            product *= a;
            product %= MOD;
            --b;
        }
        a *= a;
        a %= MOD;
        b /= 2;    // you can also use b >> 1
    }
    return product;
}


//calculates the inverse of a number mod MOD
ll inv(ll a) {
    return powmod(a, MOD - 2);
}

void solve(ll ca)
{
    ll s, n;
    cin >> s >> n;
    
    vector<pair<ll, ll>> items[s+1]; //list of items with the same weight, sorted by the value
    
    for (ll i = 0; i < n; i++) {
        ll v, w, k;
        cin >> v >> w >> k;
        
        items[w].push_back({v, k});
    }
    
    vector<ll> weights[s+1];
    for (ll i = 0; i <= s; i++) {
        if (items[i].size() == 0) {
            continue;
        }
        
        sort(items[i].begin(), items[i].end(), greater<>());
        ll cur = 0;
        for (ll j = 0; j < items[i].size(); j++) {
            ll k = 0;
            while (cur+i <= s && k < items[i][j].second) {
                weights[i].push_back(items[i][j].first);
                k++;
                cur += i;
            }
            
            if (cur+i > s) {
                break;
            }
        }
    }

    ll dp[s+1]; //dp[i] is the maximum value you can get with a weight of i
    for (ll i = 0; i <= s; i++) {
        dp[i] = -1;
    }
    dp[0] = 0;
    
    for (ll we = 0; we <= s; we++) {
        if (weights[we].size() == 0) {
            continue;
        }
        for (ll i = s; i >= 0; i--) {
            if (dp[i] != -1) {
                ll cur = i; ll csum = 0; ll ind = 0;
                while (cur+we <= s && ind < weights[we].size()) {
                    dp[cur+we] = max(dp[cur+we], dp[i]+csum+weights[we][ind]);
                    csum += weights[we][ind];
                    cur += we;
                    ind++;
                }
            }
        }
    }
    
    ll ans = 0;
    for (ll i = 0; i <= s; i++) {
        ans = max(ans, dp[i]);
    }
    
    cout << ans << endl;
    
    return;
}

int main()
{
    //mt19937 rng(0);
    
    //Fast IO
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    /*
    freopen("feast.in", "r", stdin);
    freopen("feast.out", "w", stdout);
    */
    
    ll t = 1;
    //cin >> t;

    ll co = 1;
    while (t--) {
        solve(co);
        ++co;
    }
}

//Always write your algorithm down on a piece of paper and question EACH and EVERY step if you aren't able to find an implementation mistake in your code.

Compilation message

knapsack.cpp: In function 'void solve(ll)':
knapsack.cpp:123:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for (ll j = 0; j < items[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~~~
knapsack.cpp:150:43: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |                 while (cur+we <= s && ind < weights[we].size()) {
      |                                       ~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 25 ms 2408 KB Output is correct
36 Correct 31 ms 2536 KB Output is correct
37 Correct 25 ms 2512 KB Output is correct
38 Correct 27 ms 2368 KB Output is correct
39 Correct 33 ms 2252 KB Output is correct
40 Correct 55 ms 2784 KB Output is correct
41 Correct 40 ms 2724 KB Output is correct
42 Correct 39 ms 2708 KB Output is correct
43 Correct 42 ms 2740 KB Output is correct
44 Correct 42 ms 2792 KB Output is correct
45 Correct 32 ms 2664 KB Output is correct
46 Correct 34 ms 2552 KB Output is correct
47 Correct 33 ms 2964 KB Output is correct
48 Correct 38 ms 2572 KB Output is correct
49 Correct 28 ms 2740 KB Output is correct
50 Correct 48 ms 2356 KB Output is correct