UVa - 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.

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

没看出动态规划,直接枚举了六种情况,找出最小的即可,注意当结果相同的时候,需要输出字典序最小的,所以输入的时候需要做个小处理。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>
#include <functional>

using namespace std;

const int maxn = 3;
int bottles[maxn][maxn];
int ans, ri, rj, rk;

void init()
{
	// 输入的时候注意,为了按字母顺序输出,所以这里的存储顺序有所改变
	cin >> bottles[0][2] >> bottles[0][1];
	cin >> bottles[1][0] >> bottles[1][2] >> bottles[1][1];
	cin >> bottles[2][0] >> bottles[2][2] >> bottles[2][1];
}

void solve()
{
	ans = 1 << 31 - 1;
	for (int i = 0; i < maxn; i++) {
		for (int j = 0; j < maxn; j++) {
			if (i != j) {
				int k = 3 - i - j;
				int total = 0;
				for (int m = 0; m < maxn; m++) {
					if (i != m) {
						total += bottles[0][m];
					}
					if (j != m) {
						total += bottles[1][m];
					}
					if (k != m) {
						total += bottles[2][m];
					}
				}
				if (total < ans) {
					ans = total;
					ri = i;
					rj = j;
					rk = k;
				}
			}
		}
	}
	cout << (ri == 0 ? 'B' : ((ri == 1) ? 'C' : 'G'));
	cout << (rj == 0 ? 'B' : ((rj == 1) ? 'C' : 'G'));
	cout << (rk == 0 ? 'B' : ((rk == 1) ? 'C' : 'G'));
	cout << ' ' << ans << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	while (cin >> bottles[0][0]) {
		init();
		solve();
	}

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值