Submission #701342

#TimeUsernameProblemLanguageResultExecution timeMemory
701342KidOfCubesKnapsack (NOI18_knapsack)C++17
100 / 100
113 ms34456 KiB
#include <bits/stdc++.h>
#include <climits>
#include <cstdint>
#include <functional>
using namespace std;

// struct item {
//     int value;
//     int weight;
//     int amount;
// };

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

    int S,N;
    cin >> S >> N;
    // vector<item> itemTypes(N,item());
    map<int,vector<pair<int,int>>> items; //indexed by weight, value first is value, value second is amount
    int weight;
    int value;
    int amount;
    for(int i=0;i<N;i++){
        cin >> value >> weight >> amount;
        if(weight>S||amount<=0){
            continue;
        }
        items[weight].push_back({value,amount});
    }

    // for(auto pair : items){
    //     cout << "weight "<<pair.first<<" has: [";
    //     for(int i=0;i<pair.second.size();i++){
    //         cout << "{" << pair.second[i].first <<","<< pair.second[i].second<<"}, ";
    //     }
    //     cout << "]"<<endl;
    // }

    vector<vector<long long>> dp(items.size()+1,vector<long long>(S+1,INT64_MIN));
    int weightIndex=1;
    dp[0][0]=0;
    for(auto& [weight, weightedItems] : items){
        sort(weightedItems.begin(),weightedItems.end(),greater<::pair<int,int>>());

        //looping thru itemTypes with weight weight
        for(int i=0;i<=S;i++){
            dp[weightIndex][i]=dp[weightIndex-1][i]; //pull
            int totalAmount = 0;
			int typeIndex = 0;
			int typeAmountCount = 0;
			long long totalValue = 0;
			// go through as many items until we run out of items or usable weight
			while ((totalAmount + 1) * weight <= i && typeIndex < weightedItems.size()) {
                totalAmount++;
                totalValue+=weightedItems[typeIndex].first;
                if(dp[weightIndex-1][i-(totalAmount*weight)]!=INT64_MIN){
                    dp[weightIndex][i]=max(dp[weightIndex][i],dp[weightIndex-1][i-(totalAmount*weight)]+totalValue);
                }

                typeAmountCount++;
                if(typeAmountCount==weightedItems[typeIndex].second){
                    typeAmountCount=0;
                    typeIndex++;
                }

            }
        }
        weightIndex++;
    }
    cout << *std::max_element(dp.back().begin(), dp.back().end()) << endl;

}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:54:56: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |    while ((totalAmount + 1) * weight <= i && typeIndex < weightedItems.size()) {
      |                                              ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...