Submission #475251

# Submission time Handle Problem Language Result Execution time Memory
475251 2021-09-21T16:20:22 Z nohaxjustsoflo Knapsack (NOI18_knapsack) C++17
12 / 100
1 ms 360 KB
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;


const int mxN = 2005;
const int mod = 1e9+7;
const int mxlogN = 20;
const int mxK = 10;

vector<pair<int, int>> a[mxN];
ll sum[mxN];
ll dp[mxN];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int S, n; cin >> S >> n;
    for(int i = 0; i < n; i++)
    {
        int V, W, K; cin >> V >> W >> K;
        a[W].push_back({V, K});
    }
    vector<pair<int, int>> items; ///W V
    for(int i = 1; i <= S; i++)
    {
        sort(a[i].begin(), a[i].end(), greater<pair<int,int>>());
        while(sum[i] < S && a[i].size())
        {
            while(sum[i] < S && a[i].back().second)
            {
                sum[i] += i;
                a[i].back().second--;
                items.push_back({i, a[i].back().first});
            }
            a[i].pop_back();
        }
    }
    for(int i = 0; i < items.size(); i++)
    {
        for(int j = S; j >= items[i].first; j--)
        {
            if(items[i].second+dp[j-items[i].first] > dp[j])
            {
                dp[j] = items[i].second+dp[j-items[i].first];
            }
        }
    }
    cout << dp[S];
}
/**
1
8
1 0 1 1 0 1 1 1
*/

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for(int i = 0; i < items.size(); i++)
      |                    ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Halted 0 ms 0 KB -