原题:
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
中文翻译:
(来自lucky 猫)
有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 <bits/stdc++.h>
using namespace std;
int b1,g1,c1,b2,g2,c2,b3,g3,c3;
vector<pair<int,string> > ans;
bool cmp(const pair<int,string> &p1,const pair<int,string> &p2)
{
if(p1.first!=p2.first)
return p1.first<p2.first;
else
return p1.second<p2.second;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>b1>>g1>>c1>>b2>>g2>>c2>>b3>>g3>>c3)
{
ans.clear();
int tmp=0;
tmp=b2+b3+g1+g3+c1+c2;
ans.push_back(make_pair(tmp,"BGC"));
tmp=b2+b3+g1+g2+c1+c3;
ans.push_back(make_pair(tmp,"BCG"));
tmp=g2+g3+b1+b3+c1+c2;
ans.push_back(make_pair(tmp,"GBC"));
tmp=g2+g3+b1+b2+c1+c3;
ans.push_back(make_pair(tmp,"GCB"));
tmp=c2+c3+b1+b3+g1+g2;
ans.push_back(make_pair(tmp,"CBG"));
tmp=c2+c3+g1+g3+b1+b2;
ans.push_back(make_pair(tmp,"CGB"));
sort(ans.begin(),ans.end(),cmp);
cout<<ans[0].second<<" "<<ans[0].first<<endl;
}
return 0;
}
解答:
超级简单题,一共3!种方法,枚举后找个最小的即可。