102 - 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 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

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.

有3个桶子用来装回收的玻璃瓶,玻璃瓶的颜色有三种:棕色(Brown)、绿色(Green)、透明色(Clear)。在这个问题里我们会告诉你每个桶子里的玻璃瓶的颜色及数量,现在要搬移桶子里的玻璃瓶使得最后每个桶子里都只有单一颜色的玻璃瓶,以方便回收。你的任务就是要算出最小搬移的瓶子数。你可以假设每个桶子的容量无限大,并且总共搬移的瓶子数不会超过231。

Input

每笔测试资料一行,每行有9个整数.前3个代表第1个桶子里Brown, Green, Clear颜色的瓶子数。接下来的3个数代表第2个桶子里Brown, Green, Clear颜色的瓶子数。最后的3个数代表第3个桶子里Brown, Green, Clear颜色的瓶子数。

例如:10 15 20 30 12 8 15 8 31
表示有20个Clear色的玻璃瓶在第1个桶子里,12个Green色的玻璃瓶在第2个桶子里,15个Brown色的玻璃瓶在第3个桶子里。

Output

对每一笔测试资料,输出3个桶子内最后存放之玻璃瓶颜色,以及最小搬移的瓶子数。请以大写的'G'、 'B'、 'C​​' 分别代表绿色(Green)、棕色(Brown)、透明色(Clear)。

例如:BCG 30
代表最后搬移的结果第1个桶子内的玻璃瓶颜色为Brown,第2个桶子内的玻璃瓶颜色为Clear,第3个桶子内的玻璃瓶颜色为Green.并且总共搬移了30个玻璃瓶。

如果最小搬移瓶子数有一组以上的组合,请输出字典顺序最小的那一组答案。

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>
int main()
{
long a[3],b[3],c[3],sum,n[6]={0},i,j,m;
while(scanf("%ld%ld%ld%ld%ld%ld%ld%ld%ld",&a[0],&a[1],&a[2],&b[0],&b[1],&b[2],&c[0],&c[1],&c[2])!=EOF)
            {sum=a[0]+a[1]+a[2]+b[0]+b[1]+b[2]+c[0]+c[1]+c[2];
            n[0]=a[0]+b[2]+c[1];
            n[1]=a[0]+b[1]+c[2];
            n[2]=a[2]+b[0]+c[1];
            n[3]=a[2]+b[1]+c[0];
            n[4]=a[1]+b[0]+c[2];
            n[5]=a[1]+b[2]+c[0];
            for(j=m=i=0;i<6;i++)
            if(n[i]>m){
                       m=n[i];
                       j=i+1;
                       }
            switch(j)
                       {
                       case 1: printf("BCG %ld\n",sum-n[0]);break;
                       case 2: printf("BGC %ld\n",sum-n[1]);break;
                       case 3: printf("CBG %ld\n",sum-n[2]);break;
                       case 4: printf("CGB %ld\n",sum-n[3]);break;
                       case 5: printf("GBC %ld\n",sum-n[4]);break;
                       case 6: printf("GCB %ld\n",sum-n[5]);break;
                       } 
            }
return 0;
    }

 

posted on 2013-02-03 09:41  喂喂还债啦  阅读(345)  评论(0编辑  收藏  举报