Submission #467860

# Submission time Handle Problem Language Result Execution time Memory
467860 2021-08-25T02:09:27 Z rainliofficial Knapsack (NOI18_knapsack) C++17
0 / 100
2 ms 388 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Item{
    int v, w, k;
    bool operator<(const Item& o) const{
        return o.v < v;
    };
};

const int MAXN = 1e5 + 5, MAXS = 2000 + 5;
int maxW, n;
map<int, vector<Item>> arr;
ll dp[MAXS][MAXS];

int main(){
    cin.tie(0)->sync_with_stdio(0);
    freopen("file.in", "r", stdin);
    cin >> maxW >> n;
    for (int i=0; i<n; i++){
        int v, w, k;
        cin >> v >> w >> k;
        if (arr.find(w) == arr.end()){
            arr.insert({w, vector<Item>()});
        }
        arr[w].push_back({v, w, k});
    }
    int weightGroup = 1;
    for (auto currW : arr){
        vector<Item> curr = currW.second;
        sort(curr.begin(), curr.end());
        int id = 0;
        for (int i=0; i<=maxW; i++){
            dp[weightGroup][i] = dp[weightGroup-1][i];
            int sum = 0;
            // Try to use as many of this current weightGroup as possible
            int copies = 0;
            int ind = 0;
            int totCopies = 1;
            while (ind < curr.size()){
                if (totCopies * currW.first > i){
                    break;
                }
                if (copies >= curr[ind].k){
                    ind++;
                    copies = 0;
                }
                if (ind >= curr.size()){
                    break;
                }
                sum += curr[ind].v;
                dp[weightGroup][i] = max(dp[weightGroup][i], dp[weightGroup-1][i - totCopies * currW.first] + sum);
                copies++;
                totCopies++;
            }
        }
        weightGroup++;
    }
    /*for (int i=1; i<weightGroup; i++){
        for (int j=0; j<=maxW; j++){
            cout << dp[i][j] << " ";
        }
        cout << "\n";
    }*/
    ll ans = 0;
    for (int i=0; i<=maxW; i++){
        ans = max(ans, dp[weightGroup-1][i]);
    }
    cout << ans << '\n';
}   

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:41:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |             while (ind < curr.size()){
      |                    ~~~~^~~~~~~~~~~~~
knapsack.cpp:49:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |                 if (ind >= curr.size()){
      |                     ~~~~^~~~~~~~~~~~~~
knapsack.cpp:33:13: warning: unused variable 'id' [-Wunused-variable]
   33 |         int id = 0;
      |             ^~
knapsack.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen("file.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 388 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 388 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 388 KB Output isn't correct
2 Halted 0 ms 0 KB -