Description
UVa 102 - Ecological Bin Packing
Problem:
Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.
In this problem you will be solving a bin packing problem that deals with recycling glass. Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.
The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes. For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.
Sample Input
Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.
1
2
1 2 3 4 5 6 7 8 9
5 10 5 20 10 5 10 20 10
Sample Output
For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.
1
2
BCG 30
CBG 50
Ideas
Try all possible combination as the time limit is 3.0 sec. Total operations 3! * 3 * 3 = 54
Solution
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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
int main()
{
int A[9];
unordered_map<char, vector<int>> pos;
pos['B'] = vector<int>({0, 3, 6});
pos['G'] = vector<int>({1, 4, 7});
pos['C'] = vector<int>({2, 5, 8});
while (scanf("%d %d %d %d %d %d %d %d %d", &A[0], &A[1], &A[2], &A[3], &A[4], &A[5], &A[6], &A[7], &A[8]) != EOF)
{
string s = "BCG";
int ans = INT_MAX;
string ans_str;
do
{
int sum = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s[i];
for (int j = 0; j < 3; j++)
{
if (j != i)
sum += A[pos[c][j]];
}
}
if (sum < ans)
{
ans = sum;
ans_str = s;
}
} while (next_permutation(s.begin(), s.end()));
cout << ans_str << " " << ans << endl;
}
return 0;
}