Ecological Bin Packing
In this problem you will be solving a bin packing problem that deals with recycling glass.
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.
indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.
Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.
The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.
The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.
The integer indicating the minimum number of bottle movements should follow the string.
If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.
http://www.mathblog.dk/uva-102-ecological-bin-packing/
http://blog.adilakhter.com/2013/03/08/uva-102-ecological-bin-packing/
http://www.cnblogs.com/devymex/archive/2010/08/04/1792130.html
Read full article from Ecological Bin Packing
Background
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.
The Problem
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.
The Input
The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.
Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.
The 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.The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.
The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.
The integer indicating the minimum number of bottle movements should follow the string.
If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.
Sample Input
1 2 3 4 5 6 7 8 9 5 10 5 20 10 5 10 20 10
Sample Output
BCG 30 CBG 50https://github.com/ackoroa/UVa-Solutions/blob/master/UVa%20102%20-%20Ecological%20Bin%20Packing/src/UVa%20102%20-%20Ecological%20Bin%20Packing.cpp
int B[3], G[3], C[3]; | |
while (scanf("%d %d %d %d %d %d %d %d %d", &B[0], &G[0], &C[0], &B[1], | |
&G[1], &C[1], &B[2], &G[2], &C[2]) != EOF) { | |
int min = INT_MAX, temp; | |
char ans[4]; | |
//BCG | |
temp = C[0] + G[0] + B[1] + G[1] + B[2] + C[2]; | |
if (temp < min) { | |
min = temp; | |
strcpy(ans, "BCG"); | |
} | |
//BGC | |
temp = C[0] + G[0] + B[1] + C[1] + B[2] + G[2]; | |
if (temp < min) { | |
min = temp; | |
strcpy(ans, "BGC"); | |
} | |
//CBG | |
temp = B[0] + G[0] + C[1] + G[1] + B[2] + C[2]; | |
if (temp < min) { | |
min = temp; | |
strcpy(ans, "CBG"); | |
} | |
//CGB | |
temp = B[0] + G[0] + B[1] + C[1] + G[2] + C[2]; | |
if (temp < min) { | |
min = temp; | |
strcpy(ans, "CGB"); | |
} | |
//GBC | |
temp = C[0] + B[0] + C[1] + G[1] + B[2] + G[2]; | |
if (temp < min) { | |
min = temp; | |
strcpy(ans, "GBC"); | |
} | |
//GCB | |
temp = C[0] + B[0] + B[1] + G[1] + G[2] + C[2]; | |
if (temp < min) { | |
min = temp; | |
strcpy(ans, "GCB"); | |
} | |
printf("%s %d\n",ans,min); | |
} |
http://www.mathblog.dk/uva-102-ecological-bin-packing/
Scanner in =
new
Scanner(System.in);
PrintWriter out =
new
PrintWriter(System.out,
true
);
// types of bottles: brown, green, clear
char
[] bottleType = {
'B'
,
'G'
,
'C'
};
// all 6 possible permutations of bottle type-to-bin mappings
int
[][] permutations = {
{
0
,
1
,
2
},
{
0
,
2
,
1
},
{
1
,
0
,
2
},
{
1
,
2
,
0
},
{
2
,
0
,
1
},
{
2
,
1
,
0
}
};
// bottles[i][j] are the number of type j bottles in bin i
int
[][] bottles =
new
int
[
3
][
3
];
// while there is a test case left
while
(in.hasNextInt()) {
// the minimum bottle move-count so far,
// initially -1 to indicate that we haven't found anything yet
int
minCount = -
1
;
// the string representation of the permutation with the minimum bottle move-count so far
String minPerm =
""
;
// read in the bottle counts
for
(
int
i =
0
; i <
3
; i++) {
for
(
int
j =
0
; j <
3
; j++) {
bottles[i][j] = in.nextInt();
}
}
// loop through each permutation
for
(
int
i =
0
; i < permutations.length; i++) {
// count how many bottles need to be moved
int
curCount =
0
;
// j is the current bin, and permutations[i][j] is the bottle-type that we want in that bin
for
(
int
j =
0
; j <
3
; j++) {
// go through all each bottle-type k
for
(
int
k =
0
; k <
3
; k++) {
// if the current bottle type is not supposed to be in the current bin
if
(permutations[i][j] != k) {
// then we need to move them, and we're counting how many we need to move
curCount += bottles[j][k];
}
}
}
// the string representing the current permutation, example: "GCB"
String curPerm =
""
+ bottleType[permutations[i][
0
]] + bottleType[permutations[i][
1
]] + bottleType[permutations[i][
2
]];
// if this is the first permutation we try, or if the current bottle move-count is less than the minimum bottle move-count,
// or the current bottle move-count is equal to the minimum bottle move-count and the permutation string is less than the
// permutation string of the minimum bottle move-count
if
(minCount == -
1
|| curCount < minCount || (curCount == minCount && curPerm.compareTo(minPerm) <
0
))
{
// then this is the best permutation so far
minCount = curCount;
minPerm = curPerm;
}
}
// print the minimum bottle move-count and the permutation string
out.println(minPerm +
" "
+ minCount);
}
http://www.cnblogs.com/devymex/archive/2010/08/04/1792130.html
Read full article from Ecological Bin Packing