UVa 102 - Ecological Bin Packing(规律,统计)
| 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完全的,但可以通过动态规划法或近似最优的启发式解法来解决。在这个问题中,你要解决一个回收玻璃瓶的装箱问题
问题
玻璃瓶的回收需要将其按颜色分为三类:棕、绿和无色。在这个问题中你会得到三个回收废品箱,每个都包括给定数量的棕、绿或无色玻璃瓶。为了便于回收,瓶子需要进行分捡和移动,使得每个废品箱都只有一个颜色的瓶子。
问题就是要使移动的瓶子数最小化。你可以假定唯一的问题就是使箱子间的移动次数最小化。
对于这一问题的目的,每个废品箱的容量是无限的,并且唯一的约束条件就是要通过移动瓶子使每个废品箱中都只有一种颜色的瓶子。瓶子的总量永不会超过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.题目要求按照字典序最小的输出。将这六种的移动次数全部求出,找出最小的次数,同时输出对应的颜色方案》
代码:
#include <bits/stdc++.h>
using namespace std;
string s[]={"BCG","BGC","CBG","CGB","GBC","GCB"};
int n[][];
int num[];
int main()
{
while(~scanf("%d%d%d%d%d%d%d%d%d",&n[][],&n[][],&n[][],&n[][],&n[][],&n[][],&n[][],&n[][],&n[][]))
{
int ans=,i,j;
num[]=n[][] + n[][] + n[][] + n[][] + n[][] + n[][];
num[]=n[][] + n[][] + n[][] + n[][] + n[][] + n[][];
num[]=n[][] + n[][] + n[][] + n[][] + n[][] + n[][];
num[]=n[][] + n[][] + n[][] + n[][] + n[][] + n[][];
num[]=n[][] + n[][] + n[][] + n[][] + n[][] + n[][];
num[]=n[][] + n[][] + n[][] + n[][] + n[][] + n[][];
for (j=i=; ++i<; j=num[i]<num[j] ? i : j);
cout<<s[j]<<" "<<num[j]<<endl;
}
}
UVa 102 - Ecological Bin Packing(规律,统计)的更多相关文章
- UVa - 102 - Ecological Bin Packing
Background Bin packing, or the placement of objects of certain weights into different bins subject t ...
- UVa 1149 (贪心) Bin Packing
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
- 【习题 8-1 UVA - 1149】Bin Packing
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个背包只能装两个东西. 而且每个东西都要被装进去. 那么我们随意考虑某个物品.(不必要求顺序 这个物品肯定要放进某个背包里面的. ...
- Bin Packing
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...
- Vector Bin Packing 华为讲座笔记
Vector bin packing:first fit / best fit / grasp 成本:性价比 (先验) 设计评价函数: evaluation function:cosine simil ...
- UVA 1149 Bin Packing
传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- uva 1149:Bin Packing(贪心)
题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...
随机推荐
- [转载]浅谈组策略设置IE受信任站点
在企业中,通常会有一些业务系统,要求必须加入到客户端IE受信任站点,才能完全正常运行访问,在没有域的情况下,可能要通过管理员手动设置,或者通过其它网络推送方法来设置. 有了域之后,这项工作就可以很好的 ...
- WebApi 集成 Swagger
1. Swagger(俗称:丝袜哥)是什么东西? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同 ...
- Java向前引用容易出错的地方
所谓向前引用,就是在定义类.接口.方法.变量之前使用它们,例如, class MyClass { void method() { System.out.println(myvar); } String ...
- [爬虫学习笔记]基于Bloom Filter的url去重模块UrlSeen
Url Seen用来做url去重.对于一个大的爬虫系统,它可能已经有百亿或者千亿的url,新来一个url如何能快速的判断url是否已经出现过非常关键.因为大的爬虫系统可能一秒钟就会下载 ...
- 【Android】将Xamarin For VS升级为4.1.0.530版
分类:C#.Android.VS2015(自带Update2).Win10 创建日期:2016-06-10 2016-08-03说明:该版本已过时,新版本详见本博客置顶的更新. 一.Xamarin f ...
- MongoDB之数据分布式存储
在MongoDB的世界,做数据分布式存储显得非常简单.只要按照前面介绍的 集群搭建 完成就完全具备了数据分布式存储的要求. 在这里分清几个概念:去前面的文章可以找到介绍 1. 复制集 功能是实现数 ...
- python输出excel能够识别的utf-8格式csv文件
http://blog.csdn.net/azhao_dn/article/details/16989777 可能大家都遇到过,python在输出的csv文件中如果有utf-8格式的中文,那么在使用e ...
- 爱你.一万年>>数据库基础
嗨!最近学习了一项新的技能,学习了数据库的知识,在这里抛砖引玉,给大家说说,望赐教!!>> 01.首先给大家谈谈为什么要使用数据库? 数据库可以看成是一个永久保存数据的仓库. 可以存储大量 ...
- linux怎么模糊查找一个文件
linux如何模糊查找一个文件 在当前目录下搜索指定文件: find . -name test.txt 在当前目录下模糊搜索文件: find . -name '*.txt' 在当前目录下搜索特定属性的 ...
- 功放AUX接口解析
功放上的AUX输入端口就是音频输入端口,用来连接音频输出设备:PC机上声卡.MP3播放器.CD机等