Submission #571052

#TimeUsernameProblemLanguageResultExecution timeMemory
571052webDetecting Molecules (IOI16_molecules)C++17
0 / 100
1 ms296 KiB
#include <iostream>
#include "molecules.h"
#include <algorithm>
using namespace std;

vector<vector<int>> dp; 
void knapsack(vector<int>& weights, int u)
{
    for(int i = 0; i<weights.size(); ++i)
    {
        for(int  weight = 0; weight <= u; ++weight)
        {
            if(weight < weights[i])
            {
                dp[i+1][weight] = dp[i][weight];
            }
            else
            {
                if(dp[i][weight] >= dp[i][weight - weights[i]]+ weights[i])
                {
                    dp[i+1][weight] = dp[i][weight];
                }
                else
                {
                    dp[i+1][weight] = dp[i][weight-weights[i]] + weights[i];
                }
            }
        }
    }
    
}


std::vector<int> find_subset(int l, int u, std::vector<int> w) {
    dp = vector<vector<int>>(w.size()+1, vector<int>(u+1)); 
    
    knapsack(w, u);
    int resWeight = dp[w.size()][u];
    if(resWeight < l)
        return vector<int>(0);

    vector<int> retVal;
    for(int i = w.size(); i>=0 && resWeight > 0; --i)
    {
        if(resWeight == dp[i][u])
            continue;
        else
        {
            retVal.push_back(i+1);
            resWeight-=w[i];
            u -= w[i];
        }
    }

    return retVal;
}

Compilation message (stderr)

molecules.cpp: In function 'void knapsack(std::vector<int>&, int)':
molecules.cpp:9:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |     for(int i = 0; i<weights.size(); ++i)
      |                    ~^~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...