102 - Ecological Bin Packing
Time limit: 3.000 secondsBackground
Bin packing, or the placement of objects of certain weights intodifferent bins subject to certain constraints, is an historicallyinteresting problem. Some bin packing problems are NP-complete but areamenable to dynamic programming solutions or to approximately optimalheuristic solutions.
In this problem you will be solving a bin packing problem that dealswith recycling glass.
The Problem
Recycling glass requires that the glass be separated by color into oneof three categories: brown glass, green glass, and clear glass. In thisproblem you will be given three recycling bins, each containing aspecified number of brown, green and clear bottles. In order to berecycled, the bottles will need to be moved so that each bin containsbottles of only one color.
The problem is to minimize the number of bottles that are moved. Youmay assume that the only problem is to minimize the number of movementsbetween boxes.
For the purposes of this problem, each bin has infinitecapacity and the only constraint is moving the bottles so that eachbin 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 9integers. The first three integers on a line represent the number ofbrown, green, and clear bottles (respectively) in bin number 1, thesecond three represent the number of brown, green and clear bottles(respectively) in bin number 2, and the last three integersrepresent the number of brown, green, and clear bottles(respectively)in bin number 3. For example, the line10 15 20 30 12 8 15 8 31
indicates that there are 20 clear bottles in bin 1, 12 green bottles inbin 2, and 15 brown bottles in bin 3.
Integers on a line will be separated by one or more spaces. Yourprogram should process all lines in the input file.
The Output
For each line of input there will be one line of output indicating what colorbottles 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 thethree upper case characters 'G', 'B','C' (representing the colors green, brown, and clear) representing thecolor associated with each bin.
The first character of the stringrepresentsthe color associated with the first bin, the second character of thestring represents the color associated with the second bin, and thethird character represents the color associated with the third bin.
The integer indicating the minimum number of bottle movementsshould follow the string.
If more than one order of brown, green, and clear binsyields the minimum number of movementsthen the alphabetically first string representing a minimalconfiguration 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 50思路是比较好找的,就是模拟搬运积木过程,同时维护步骤最小值。很多人会把这个问题看成是一个动态规划问题,但是我们仔细分析一下其实问题很简单。
下面给大家发一个我画的图,相信就理解了~
然后我们发现其实一共就只有六种情况,所以就可以变成一个简单的模拟题。
上代码
#include<stdio.h>
#include<string.h>
char str[6][10]={"BCG","BGC","CBG","CGB","GBC","GCB"};
int n[3][3],sum[6];
int main()
{
int i,minn,p;
while(scanf("%d%d%d%d%d%d%d%d%d",&n[0][0],&n[0][1],&n[0][2],&n[1][0],&n[1][1],&n[1][2],&n[2][0],&n[2][1],&n[2][2])!=EOF)
{
sum[0] = n[1][0] + n[2][0] + n[0][2] + n[2][2] + n[0][1] + n[1][1];
sum[1] = n[1][0] + n[2][0] + n[0][1] + n[2][1] + n[0][2] + n[1][2];
sum[2] = n[1][2] + n[2][2] + n[0][0] + n[2][0] + n[0][1] + n[1][1];
sum[3] = n[1][2] + n[2][2] + n[0][1] + n[2][1] + n[0][0] + n[1][0];
sum[4] = n[1][1] + n[2][1] + n[0][0] + n[2][0] + n[0][2] + n[1][2];
sum[5] = n[1][1] + n[2][1] + n[0][2] + n[2][2] + n[0][0] + n[1][0];
minn=sum[0];
p=0;
for(i=1;i<6;i++)
if(sum[i]<minn)
{
minn=sum[i];
p=i;
}
printf("%s %d\n",str[p],sum[p]);
}
return 0;
}