Modifed version of the knapsack - c++

I know how to code the solution for the 0-1 knapsack problem. However, I do not know how to change this code such that we can pick multiple copies of the same item. So, for each item i , we have another paramter k(i) which denotes the number of copies of i present. I will be obliged if someone could help me with this. Below is my code when k(i) = 1. Look at the knapsack function for a top down dp solution
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int n, g;
int p[1005],w[1005];
int dp[1004][35];// num of object and weight left
int knapsack(int resourcel, int item){
//if(resourcel < 0) return (1<<31);
if(item == n) return 0;
if(resourcel == 0) return 0;
if(dp[item][resourcel] != -1) return dp[item][resourcel];
if(resourcel - w[item] < 0){
dp[item][resourcel] = knapsack(resourcel,item+1);
return dp[item][resourcel];
}
else{
int take = knapsack(resourcel - w[item],item+1) + p[item];
int notTake = knapsack(resourcel,item+1);
dp[item][resourcel] = take > notTake?take : notTake;
return dp[item][resourcel];
}
}
int main(){
int tc,dummy, sum =0;
//freopen("in.txt","r",stdin);
scanf("%d",&tc);
for(int i = 0 ; i < tc; i++){
sum = 0;
memset(dp,-1,sizeof(dp));
scanf("%d",&n);
//cout<<" n is : "<<n<<endl;
for(int j = 0 ; j < n ;j++){
scanf("%d %d",&p[j],&w[j]);
//cout<<" price and val is : "<<p[j]<<" " << w[j]<<endl;
}
scanf("%d",&g);
//cout<<"g is : "<<g<<endl;
for(int p = 0 ; p< g;p++){
scanf("%d",&dummy);
sum+= knapsack(dummy,0);//wight allowed and item visited
}
printf("%d\n",sum);
}
return 0;
}

Your knapsack code is overly complicated. Here is another way to do it:
Let dp[i] = maximum profit we can get for weight i.
for i = 1 to numItems do
for j = knapsackWeight down to items[i].weight do
dp[j] = max(dp[j], dp[j - items[i].weight] + items[i].profit)
Now, you also want a field item.copies. We can simply add another loop in the middle to iterate this many times.
for i = 1 to numItems do
for k = 1 to items[i].copies do
for j = knapsackWeight down to items[i].weight do
dp[j] = max(dp[j], dp[j - items[i].weight] + items[i].profit)

Related

Denominate the amount with the minimum number of coins with a given face value. Greedy problem

I have the C++ function to do. It works fine, but there are some cases where it works bad - "greedy problem".
My C++ code:
#include <vector>
#include <algorithm>
std::vector<int> ans;
std::vector<int> get_change(const std::vector<int> &denominations, int amount) {
//pure algo
std::vector<int> money = denominations;
std::vector<int> count;
ans.clear();
count.assign(money.size(), 0);
std::sort(money.begin(), money.end());
int summ = amount;
for (int i = count.size()-1; i >= 0; i--) {
count[i] = summ / money[i];
summ = summ % money[i];
if (summ==0)
break;
}
//ans generation
for (int i = 0; i < money.size(); i++)
for (int j = 0; j < count[i]; j++)
ans.push_back(money[i]);
return ans;
}
Greedy problem sample: get_change({ 1, 6, 9 }, 30) will return { 1, 1, 1, 9, 9, 9 }, but not { 6, 6, 9, 9 }.
The task is to improve this algorithm to get the same answer.
One possible approach is backtracking.
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution. (Wikipedia)
Here, we try to determine the number of coins, for each coin.
The candidates are abandonned, as soon as the total number of coins is higher than the current optimal solution. Moreover, here, in a given situation (at step i), we directly calculate the maximum number of coins for coins[i], such that the total sum is not higher than the amount.
Here is a possible implementation:
#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> get_change(const std::vector<int>& denominations, int amount) {
std::vector<int> coins = denominations;
std::vector<int> n_coins(coins.size(), 0);
std::vector<int> n_coins_opt(coins.size(), 0);
int n = coins.size();
std::sort(coins.begin(), coins.end(), std::greater<int>());
int sum = 0; // current sum
int i = 0; // index of the coin being examined
int n_min_coins = amount / coins[n - 1] + 1;
int n_total_coins = 0;
bool up_down = true;
while (true) { // UP
if (up_down) {
n_coins[i] = (amount - sum) / coins[i]; // max number of coins[i]
sum += n_coins[i] * coins[i];
n_total_coins += n_coins[i];
if (sum == amount) {
if (n_total_coins < n_min_coins) {
n_min_coins = n_total_coins;
n_coins_opt = n_coins;
}
up_down = false;
sum -= n_coins[i] * coins[i];
n_total_coins -= n_coins[i];
n_coins[i] = 0;
i--;
}
else {
if (i == (n - 1) || (n_total_coins >= n_min_coins)) { // premature abandon
sum -= n_coins[i] * coins[i];
n_total_coins -= n_coins[i];
n_coins[i] = 0;
up_down = false;
i--;
}
else {
i++;
}
}
}
else { // DOWN
if (i < 0) break;
if (n_coins[i] == 0) {
if (i == 0) break;
i--;
}
else {
sum -= coins[i];
n_coins[i] --;
n_total_coins--;
i++;
up_down = true;
}
}
}
std::vector<int> ans;
for (int i = 0; i < coins.size(); i++)
for (int j = 0; j < n_coins_opt[i]; j++)
ans.push_back(coins[i]);
return ans;
}
int main() {
std::vector<int> coins = { 1, 6, 9 };
int amount = 30;
auto n_coins = get_change(coins, amount);
for (int i = 0; i < n_coins.size(); i++)
std::cout << n_coins[i] << " ";
std::cout << "\n";
return 1;
}
This is a dynamic programming problem.
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution{
public static void main (String[] args) throws java.lang.Exception{
System.out.println(getChange(new int[]{1,6,9},30));
System.out.println(getChange(new int[]{1},3));
System.out.println(getChange(new int[]{4,20,500},450));
}
private static List<Integer> getChange(int[] denominations,int amount){
Arrays.sort(denominations);
List<Integer> ans = new ArrayList<>();
if(amount <= 0 || denominations[0] > amount) return ans;
int[][] dp = new int[amount + 1][2];
for(int i=0;i<denominations.length;++i){
if(denominations[i] > amount) break;
dp[denominations[i]][0] = 1;
dp[denominations[i]][1] = 0;
for(int j=denominations[i] + 1;j<=amount;++j){
if(dp[j-denominations[i]][0] > 0 && (dp[j][0] == 0 || dp[j-denominations[i]][0] + 1 < dp[j][0])){
dp[j][0] = dp[j-denominations[i]][0] + 1;
dp[j][1] = j-denominations[i];
}
}
}
if(dp[amount][0] > 0){
while(dp[amount][0] != 0){
ans.add(amount - dp[amount][1]);
amount = dp[amount][1];
}
}
return ans;
}
}
Demo: https://ideone.com/2fYg4F
Algorithm:
This is similar to coin change problem.
We first sort the numbers in the array for uniform computation.
Now, we iterate on all array elements and make each array element loop over all possible amounts from that element till final amount.
Now, we can make amount j(which is a sum) only if we have made sum j - denominations[i].
While doing so, we also check for minimum number of coins needed. If amount j with current denominations[i] needs lesser coins than the current value stored in dp[j][0], then we update the answer in dp[j] accordingly.
In the end, we just loop over the exact coins needed and return the answer.
What is dp[][]:
It's just a simple 2D array where the 0th column keeps track of minimum number of coins
needed in it's first index and 2nd index has the previous coin index that give it the
minimal value.
The 2nd index in dp[][] would ease out the calculation to find the exact coin value as we would have to just do amount - dp[amount][1] to get the coin value.
In the end, we just do a check of dp[amount][0] value. If it's value is 0, we don't have a solution, else we compute it.
I am adaptated vivek_23's dynamic solution to required C++ language.
std::vector<int> get_change2(const std::vector<int>& coins, int amount) {
std::vector<int> denominations = coins;
std::sort(denominations.begin(), denominations.end());
std::vector<int> ans;
if (amount <= 0 || denominations[0] > amount)
return ans;
int** dp = new int* [amount + 1];
for (int i = 0; i < amount + 1; i++) {
dp[i] = new int[2];
dp[i][0] = 0;
dp[i][1] = 0;
}
for (int i = 0; i < denominations.size(); i++) {
if (denominations[i] > amount) break;
dp[denominations[i]][0] = 1;
dp[denominations[i]][1] = 0;
for (int j = denominations[i] + 1; j <= amount; ++j) {
if (dp[j - denominations[i]][0] > 0 && (dp[j][0] == 0 || dp[j - denominations[i]][0] + 1 < dp[j][0])) {
dp[j][0] = dp[j - denominations[i]][0] + 1;
dp[j][1] = j - denominations[i];
}
}
}
if (dp[amount][0] > 0) {
while (dp[amount][0] != 0) {
ans.push_back(amount - dp[amount][1]);
amount = dp[amount][1];
}
}
return ans;
}

Solution for SPOJ AGGRCOW

I have the folowing problem:
Farmer John has built a new long barn, with N (2 <= N <= 100,000)
stalls. The stalls are located along a straight line at positions
x1,...,xN (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don't like this barn layout and become
aggressive towards each other once put into a stall. To prevent the
cows from hurting each other, FJ wants to assign the cows to the
stalls, such that the minimum distance between any two of them is as
large as possible. What is the largest minimum distance?
Input
t – the number of test cases, then t test cases follows.
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
For each test case output one integer: the largest minimum distance.
Example
Input:
1
5 3
1
2
8
4
9
Output:
3 Output details:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8,
resulting in a minimum distance of 3. Submit solution!
My approach was to pick some pair which has a certain gap and check if there are enough elements in the array to satisfy the need of all the cows.
To find these elements,I used binary search.
When I find an element , I reset my left to mid so I can continue based on the number of cows left.
My code:
#include <iostream>
int bsearch(int arr[],int l,int r,int gap , int n,int c){
int stat = 0;
for (int i = 1;i <= c; ++i) {
while(l <= r) {
int mid = (l+r)/2;
int x = n+(i*gap);
if (arr[mid] > x && arr[mid-1] < x) {
l = mid;
++stat;
break;
}
if(arr[mid] < x) {
l = mid + 1;
}
if (arr[mid] > x) {
r = mid - 1;
}
}
}
if (stat == c) {
return 0;
}
else {
return -1;
}
}
int calc(int arr[],int n , int c) {
int max = 0;
for (int i = 0; i < n; ++i) {
for (int j = i+1;j < n; ++j) {
int gap = arr[j] - arr[i];
if (gap > max) {
if (bsearch(arr,j,n-1,gap,arr[j],c) == 0) {
max = gap;
}
}
}
}
return max;
}
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t , n ,c;
cin >> t;
for(int i = 0 ; i < t; ++i) {
cin >> n >> c;
int arr[n];
for (int z = 0 ; z < n; ++z) {
cin >> arr[z];
}
sort(arr,arr+n);
//Output;
int ans = calc(arr,n,c);
cout << ans;
}
return 0;
}
Problem Page:
https://www.spoj.com/problems/AGGRCOW/

Find Maximum Strength given number of elements to be skipped on left and right.Please Tell me why my code gives wrong output for certain test cases?

Given an array "s" of "n" items, you have for each item an left value "L[i]" and right value "R[i]" and its strength "S[i]",if you pick an element you can not pick L[i] elements on immediate left of it and R[i] on immediate right of it, find the maximum strength possible.
Example input:
5 //n
1 3 7 3 7 //strength
0 0 2 2 2 //Left Value
3 0 1 0 0 //Right Value
Output:
10
Code:
#include < bits / stdc++.h >
using namespace std;
unsigned long int getMax(int n, int * s, int * l, int * r) {
unsigned long int dyn[n + 1] = {};
dyn[1] = s[1];
for (int i = 2; i <= n; i++) {
dyn[i] = dyn[i - 1];
unsigned long int onInc = s[i];
int left = i - l[i] - 1;
if (left >= 1) {
unsigned int k = left;
while ((k > 0) && ((r[k] + k) >= i)) {
k--;
}
if (k != 0) {
if ((dyn[k] + s[i]) > dyn[i]) {
onInc = dyn[k] + s[i];
}
}
}
dyn[i] = (dyn[i] > onInc) ? dyn[i] : onInc;
}
return dyn[n];
}
int main() {
int n;
cin >> n;
int s[n + 1] = {}, l[n + 1] = {}, r[n + 1] = {};
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i <= n; i++) {
cin >> l[i];
}
for (int i = 1; i <= n; i++) {
cin >> r[i];
}
cout << getMax(n, s, l, r) << endl;
return 0;
}
Problem in your approach:
In your DP table, the information you are storing is about maximum possible so far. The information regarding whether the ith index has been considered is lost. You can consider taking strength at current index to extend previous indices only if any of the previously seen indices is either not in range or it is in range and has not been considered.
Solution:
Reconfigure your DP recurrence. Let DP[i] denote the maximum answer if ith index was considered. Now you will only need to extend those that satisfy range condition. The answer would be maximum value of all DP indices.
Code:
vector<long> DP(n,0);
DP[0]=strength[0]; // base condition
for(int i = 1; i < n ; i++){
DP[i] = strength[i];
for(int j = 0; j < i ; j++){
if(j >= (i-l[i]) || i <= (j+r[j])){ // can't extend
}
else{
DP[i]=max(DP[i],strength[i]+DP[j]); // extend to maximize result
}
}
}
long ans=*max_element(DP.begin(),DP.end());
Time Complexity: O(n^2)
Possible Optimizations:
There are better ways to calculate maximum values which you might want to look into. You can start by looking into Segment tree and Binary Indexed Trees.

C++ Birthday Probability

I am trying to teach myself C++ in preparation for graduate school this coming fall but I am having some trouble with this birthday paradox problem. My code seems to run ok but I am not getting the correct output. If anyone has any suggestions please let me know.
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
const int trials = 100000;
int birthdays[50];
int numMatches;
for(int i = 2; i <= 50; i++)
{
numMatches = 0;
for(int j = 1; j <= trials; j++)
{
for(int k = 1; k <= i; k++)
{
birthdays[k] = (rand() % 365) + 1;
}
int m = 1;
bool matched = false;
while(m < i && !matched){
int n = m + 1;
while(n <= i && !matched){
if(birthdays[m] == birthdays[n]){
numMatches++;
matched = true;
}
n++;
}
m++;
}
}
cout << "Probability of " << i << " people in a room sharing a birthday is \t"
<< ( float(numMatches) / float(trials) ) << endl;
}
}
Your code is not computing the probability of two people in a room of 50 sharing a birthday. There's several bugs, mostly with indexing, but here's the biggest issue:
for(int j = 1; j <= trials; j++) {
// assigns a random birthday to the first i people (should be 0 indexed)
for(k = 1; k <= i; k++)
birthdays[k] = (rand() % 365) + 1;
// Does *exactly* the same thing as the previous loop, overwriting what
// the initial loop did. Useless code
for(m = 1; m <= i; m++)
birthdays[m] = (rand() % 365) + 1;
// At this point, m = k = i + 1. Here you check if
// the i + 1st array value has the same b-day. It will, because they're
// the same thing. Note you never set the i + 1st value so the loops
// above did nothing
if(birthdays[k] == birthdays[m])
++numMatches;
}
So what you've got here is something like:
Perform 48 iterations of the following (from your first loop which goes from 2 to 50: no idea where those values came from)
For each of those 48 iterations, perform 10k iterations of:
assign a bunch of random stuff to an array overwriting stuff
Ignore the values you wrote in the array, do a comparison that's always true and increment numMatches by 1
Consider what's going on here:
for(int j = 1; j <= trials; j++) {
for(k = 1; k <= i; k++)
birthdays[k] = (rand() % 365) + 1;
for(m = 1; m <= i; m++)
birthdays[m] = (rand() % 365) + 1;
if(birthdays[k] == birthdays[m])
++numMatches;
}
You go through i birthdays and assign a random number, then you go through the same i birthdays and assign them a new random number. Then you try to find a match for just one value of k and m (which both happen to equal i+1, which isn't one of the values set!).
My suggestion is to break the problem down into smaller units that will make it easier to figure out how to code - here are the functions I would try to write.
/* randomizeBirthdays()
* Put n random birthdays into the pre-allocated array birthdays.
* birthdays must of course be of length <= n.
*/
void randomizeBirthdays(int * birthdays, int n);
/* hasMatchingBirthdays()
* Check if birthdays array has two people with the same birthday
* in the first n entries.
* Return value is boolean.
*/
bool hasMatchingBirthdays(int * const birthdays, int n);
/* probabilityOfMatch()
* Calculate the probability that at least 2 out of n people will
* have the same birthday, using nTrials number of trials.
* Return value is double.
*/
double probabilityOfMatch(int n, int nTrials);
If you break it down like this it becomes easier to write and easier to troubleshoot.
As I said in comments already:
I think your aim is to test if 2 people in room of 2-50 people share
birthday, not if 2-50 people share birthday as you say in output. And
that's 2 people out of 23 have 50.7%, not 24.
I completely reworked your code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define DAYS_IN_YEAR 365
#define TRIALS 10000
void clearArray (bool * array)
{
for (int i = 0; i < DAYS_IN_YEAR; i++)
array[i] = false;
}
int main()
{
srand(time(NULL));
bool birthdays[DAYS_IN_YEAR]; //we are trying to hit same day in year twice
int r, numMatches;
for(int i = 2; i < 50; i++)
{
numMatches = 0;
for(int j = 0; j < TRIALS; j++)
{
clearArray(birthdays);
for(int k = 0; k < i; k++)
{
r = rand() % DAYS_IN_YEAR; // == 0-364
if (birthdays[r])
{
numMatches++;
break; // 2 people already have same birthdays here
}
birthdays[r] = true;
}
}
cout << "Probability of 2 people having same birthday in room of " << i << " people is "
<< (float)numMatches / TRIALS << endl;
}
}
Output:
Probability of 2 people having same birthday in room of 23 people is 0.516
I think the code must be something like this.
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int birthdays[10000][50];
int numMatches;
int trials=10000,check;
for(int n=0;n<trials;n++)
{
for(int j=0;j<50;j++)
{
birthdays[n][j]=rand()%365+1;
}
}
for(int i=2;i<=50;i++)
{
numMatches=0;
for(int n=0;n<trials;n++)
{
check=1;
for(int j=0;j<i;j++)
{
for(int k=j+1;k<=i;k++)
{
if(birthdays[n][j]==birthdays[n][k]&&check)
{
numMatches++;
check=0;
}
}
}
}
cout << "Probability of " << i << " people in a room sharing a birthday is \t" <<
(static_cast<float>(numMatches) / (trials)) << endl;
}
}

Finding the number of subsets with sum equal to k

Can anyone explain me the dynamic algorithm, that finds number of subsets with sum equal to k.
I searched in Google, but cant find any simple explanation! Sorry for my English!
Here is the code:
int numbers[MAX];
int GetmNumberOfSubsets()
{
int dp[MAX];
dp[0] = 1;
int currentSum = 0;
for (int i = 0; i < numbers.length; i++)
{
currentSum += numbers[i];
for (int j = min(sum, currentSum); j >= numbers[i]; j--)
dp[j] += dp[j - numbers[i]];
}
return dp[sum];
}
Your DP solution should be 2-dimensional, 1 dimension for the sum, and 1 dimension for the number of elements.
The recursive formula defining this solution is:
DP(x,i) = 0 x < 0
DP(0,i) = 1
DP(x,0) = 0 x > 0
DP(x,i) = DP(x-numbers[i],i-1) + DP(x,i-1)
And it should be something like:
int dp[MAX+1][sum+1];
int i, x;
for (i = 0; i < MAX+1; i++) {
dp[i][0] = 1;
}
for (x = 1; x < sum+1; x++) {
dp[0][x] = 0
}
for (i = 1; i < MAX+1; i++) {
for (x = 1; x < sum+1; x++) {
dp[i][x] = dp[i-1][x];
if (x >= numbers[i])
dp[i][x] += dp[i][x-numbers[i]];
}
}
return dp[MAX][sum];
(Hope I didn't have minor issues, didn't test it - but it should give you idea how to implement it once recursive formulas are clear)
You can use the following example to find the number of subsets with sum equal to k:
#include <iostream>
using std::cout;
using std::cin;
int count = 0,K;
void noofsubsets(int arr[], int sum, int N){
if(N==0){
if(sum==K)
count++;
return;
}
noofsubsets(arr, sum, N-1);
noofsubsets(arr, sum+arr[N-1], N-1);
}
here is a solution using recursion ...
considering two cases
i) Including first element of an array.
ii) without first array element.
`
def subSetsSumK(arr, v, k) :
if (k == 0) :
for value in v :
print(value, end=" ")
print()
return
if (len(arr)== 0):
return
si=0
v1 = [] + v
v1.append(arr[si])
subSetsSumK(arr[1:], v1, k - arr[si])
subSetsSumK(arr[1:], v, k)
def subSetsSumK(arr, k):
v = []
subSetsSumK(arr,v, k)
# Driver code
arr = [ 2,1,3,2 ]
k_sum = 4
subSetsSumK(arr,k)