[解题报告]102 - 生态装箱 时间限制:3.000秒

题目原文:


 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.

 

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
题目大意:

 生态装箱 

 

背景

装箱,或一定重量的物体放置到不同的垃圾箱受到一定的制约,是一个历史上的有趣的问题。有些装箱问题是NP-完全的,但适合于动态规划的解决方案或近似最优的启发式解决方案。

在这个问题中,你将被解决了装箱问题,处理回收玻璃。

 

存在的问题

回收玻璃,玻璃颜色分为三类:茶色玻璃,绿色玻璃,透明玻璃隔开。在这个问题上,你会得到3个回收箱,每个都包含指定数目的棕色,绿色和透明的瓶子。为了是回收,瓶子将需要被移动,从而使各bin中包含仅一种颜色的瓶。

的问题是移动的瓶的数量降到最低。你可以假设,唯一的问题是,以尽量减少在箱子之间的变动。

对于这个问题的目的,每一个的bin具有无限的能力和唯一的约束是移动的瓶子,使每个bin包含一个单一颜色的瓶。总瓶数将不会超过2 ^ 31。

 

“输入

输入包括一系列的线,每行包含9整数。第一3的行上的整数代表棕,绿,和透明的瓶子(分别)在段数1的数量,第二个三代表的棕色,绿色和透明的瓶(分别地),在第2段数的数目,和最后三个整数表示,棕色,绿色和透明的瓶子(分别)在本数3。例如,在生产线10 15 20 30 12 8 15 8月31日

表明,有20个透明的瓶子斌1,12个绿色的瓶子,纸盒2,棕色瓶和15槽3。

将被分开的行上的整数,由一个或多个空格。你的程序应该处理输入文件中的所有行。

 

输出

对于输入的每一行都会有一个输出线,说明什么颜色的瓶子去什么斌,以尽量减少瓶运动。你也应该打印瓶运动的最小数量。

输出应包括三个大写字符'G','B','C'代表绿色,棕色和明确表示的颜色与每个bin的字符串。

的字符串的第一个字符表示与第一bin相关联的颜色,第二个字符的字符串表示与第二bin相关联的颜色,和第三个字符表示与第三箱相关联的颜色。

瓶运动的最小数量的整数,指示应遵循的字符串。

如果以上的订单,棕色,绿色和明确的箱变动产生的最小数目,然后按字母顺序排列的第一个字符串,表示最小的配置应该被打印出来。

 

样例输入

 

1 2 3 4 5 6 7 8 9
5 10 5 20 10 10 10 20 10

样本输出

 

BCG 30
CBG 50
参考代码:
 1 #include<stdio.h>
 2 int main()
 3  {
 4   int a1,a2,a3,b1,b2,b3,c1,c2,c3; 
 5   int s[6],i,flag;
 6   char k[6][4]={"BCG","BGC","CBG","CGB","GBC","GCB"};         
 7   while(scanf("%d %d %d %d %d %d %d %d %d",&a1,&a2,&a3,&b1,&b2,&b3,&c1,&c2,&c3)!=EOF)
 8    {
 9     flag=0;
10     s[0]=a2+a3+b1+b2+c1+c3;
11     s[1]=a2+a3+b1+b3+c1+c2;
12     s[2]=a1+a2+b2+b3+c1+c3;
13     s[3]=a1+a2+b1+b3+c2+c3;
14     s[4]=a1+a3+b2+b3+c1+c2;
15     s[5]=a1+a3+b1+b2+c2+c3;
16     for(i=0;i<6;i++)
17        if(s[i]<s[flag]) flag=i;
18     printf("%s %d\n",k[flag],s[flag]);
19    }
20   return 0; 
21  }

 

 
posted @ 2013-02-24 12:17  菜的真要吐  阅读(159)  评论(0编辑  收藏  举报