This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include "molecules.h"
#include <algorithm>
using namespace std;
vector<vector<pair<int, 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].first >= dp[i][weight - weights[i]].first+ weights[i])
{
dp[i+1][weight] = dp[i][weight];
}
else
{
dp[i+1][weight] = {dp[i][weight-weights[i]].first + weights[i], i};
}
}
}
}
}
std::vector<int> find_subset(int l, int u, std::vector<int> w) {
dp = vector<vector<pair<int,int>>>(w.size()+1, vector<pair<int,int>>(u+1, {0, -1}));
knapsack(w, u);
//cout<<"begin pair: ";
pair<int,int> currP = dp[w.size()][u];
//cout<<currP.first<<" "<<currP.second<<endl;
if(currP.first < l)
return vector<int>(0);
vector<int> retVal;
int currWeight = currP.first;
while(currP.second != -1)
{
retVal.push_back(currP.second+1);
currP = dp[currP.second][currWeight-w[currP.second]];
currWeight-=w[currP.second];
// cout<<"new pair: " <<currP.first<<" "<<currP.second<<endl;
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |