題目來源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=38
Ecological Bin Packing
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.
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.
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.
1 2 3 4 5 6 7 8 9 5 10 5 20 10 5 10 20 10
BCG 30 CBG 50
題目翻譯:
裝箱問題,即在一定的約束條件下如何將不同重量的物體放入不同的箱子,是一個歷史悠久的有趣問題。某些裝箱問題是NP完全的,但可以通過動態規劃法或近似最優的啟發式解法來解決。在這個問題中,你要解決一個回收玻璃瓶的裝箱問題
玻璃瓶的回收需要將其按顏色分為三類:棕、綠和無色。在這個問題中你會得到三個回收廢品箱,每個都包括給定數量的棕、綠或無色玻璃瓶。為了便於回收,瓶子需要進行分撿和移動,使得每個廢品箱都只有一個顏色的瓶子。
問題就是要使移動的瓶子數最小化。你可以假定唯一的問題就是使箱子間的移動次數最小化。
對於這一問題的目的,每個廢品箱的容量是無限的,並且唯一的約束條件就是要通過移動瓶子使每個廢品箱中都只有一種顏色的瓶子。瓶子的總量永不會超過231。
輸入由多行數據構成,每行數據中有9個整數。一行的前3個整數表示在1號廢品箱中棕、綠和無色瓶子的數量各是多少,中間3個整數表示在2號廢品箱中棕、綠和無色瓶子的數量各是多少,後3個整數表示在3號廢品箱中棕、綠和無色瓶子的數量各是多少。比如下面的一行:
10 15 20 30 12 8 15 8 31
表示共有1號廢品箱中有20個無色瓶子,2號廢品箱有12個綠色瓶子,3號廢品箱中有15個棕色瓶子。
每行的各整數間有一個或多個空格。你的程序要處理輸入數據中的所有行。
對於每行輸入要有對應的一行輸出,給出哪個顏色的瓶子裝入哪個廢品箱才能使瓶子的移動最小化。你還應打印出瓶子移動的最少次數。
輸出應用3個大寫字母‘G’、‘B’、‘C’(分別表示綠色、棕色和無色)構成的字符串來表示各廢品箱中瓶子的顏色。
字符串的第1個字母為1號廢品箱的顏色,第2個字母為2號廢品箱的顏色,第3個字母為3號廢品箱的顏色。在字符串之後應用整數輸出移動瓶子次數的最小值。如果有多於一種次序的棕、綠和無色廢品箱都滿足同一個最少的移動次數,則按照字母表的順序輸入第一個字符串最小的一種。
1 2 3 4 5 6 7 8 9
5 10 5 20 10 5 10 20 10
BCG 30
CBG 50
解題思路:
首先明白題目中的玻璃瓶只有三種顏色,因此三種顏色的全排列3*2*1= 6.題目要求按照字典序最小的輸出。將這六種的移動次數全部求出,找出最小的次數,同時輸出對應的顏色方案》
代碼:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 string s[6]={"BCG","BGC","CBG","CGB","GBC","GCB"}; 6 int n[3][3]; 7 int num[6]; 8 int main() 9 { 10 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])) 11 { 12 int ans=0,i,j; 13 num[0]=n[1][0] + n[2][0] + n[0][2] + n[2][2] + n[0][1] + n[1][1]; 14 num[1]=n[1][0] + n[2][0] + n[0][1] + n[2][1] + n[0][2] + n[1][2]; 15 num[2]=n[1][2] + n[2][2] + n[0][0] + n[2][0] + n[0][1] + n[1][1]; 16 num[3]=n[1][2] + n[2][2] + n[0][1] + n[2][1] + n[0][0] + n[1][0]; 17 num[4]=n[1][1] + n[2][1] + n[0][0] + n[2][0] + n[0][2] + n[1][2]; 18 num[5]=n[1][1] + n[2][1] + n[0][2] + n[2][2] + n[0][0] + n[1][0]; 19 for (j=i=0; ++i<6; j=num[i]<num[j] ? i : j); 20 cout<<s[j]<<" "<<num[j]<<endl; 21 } 22 }