-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bin Packing Problem - First Fit Decreasing.cpp
78 lines (61 loc) · 1.2 KB
/
Bin Packing Problem - First Fit Decreasing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
using namespace std;
void dispArr(int arr[], int n)
{
int i;
cout << "The Array: ";
for(i=0; i<n; i++)
{
cout << arr[i];
if(i != n-1)
cout << " ";
}
cout << endl;
}
int firstFit(int arr[], int n, int c)
{
int bins=0, i, bin_rem[n];
for(i=0; i<n; i++)
{
int j;
for(j=0; j<bins; j++)
{
if(bin_rem[j] >= arr[i])
{
bin_rem[j] -= arr[i];
break;
}
}
if(j == bins)
{
bin_rem[bins] = c - arr[i];
bins++;
}
}
return bins;
}
int firstFitDec(int arr[], int n, int c)
{
sort(arr, arr+n, greater<int>());
return firstFit(arr, n, c);
}
int main()
{
int i, n, c;
cout << "Enter Bin capacity: ";
cin >> c;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter array data: ";
for(i=0; i<n; i++)
{
cin >> arr[i];
}
cout << endl;
dispArr(arr, n);
int low_bnd = firstFitDec(arr, n, c);
cout << "Minimum Bin required: " << low_bnd << endl;
return 0;
}
// Special Thanks to GeeksforGeeks