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

【解析】

使用枚举法,一共有六种情况,从中选出最小的一个,如果最小的有相同的,按字母顺序输出第一个。

【代码】

/*********************************
*   日期:2013-9-25
*   作者:SJF0115
*   题号: 102 - Ecological Bin Packing
*   来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=38
*   结果:AC
*   来源:UVA
*   总结:
**********************************/
#include<stdio.h>
char str[6][4] = {"BCG","BGC","CBG","CGB","GBC","GCB"};
int main(){
	int count[6];
	int B1,B2,B3,C1,C2,C3,G1,G2,G3,min,key;
	//freopen("C:\\Users\\XIAOSI\\Desktop\\acm.txt","r",stdin);
	while(scanf("%d %d %d %d %d %d %d %d %d",&B1,&G1,&C1,&B2,&G2,&C2,&B3,&G3,&C3) != EOF){
		count[0] = G1+C1+B2+G2+B3+C3;
		count[1] = G1+C1+B2+C2+B3+G3;
		count[2] = B1+G1+C2+G2+B3+C3;
		count[3] = B1+G1+B2+C2+C3+G3;
		count[4] = B1+C1+C2+G2+B3+G3;
		count[5] = B1+C1+B2+G2+C3+G3;
		min = count[0];
		key = 0;
		for(int i = 1;i < 6;i++){
			if(min > count[i]){
				min = count[i];
				key = i;
			}
		}
		printf("%s %d\n",str[key],min);
	}
	return 0;
}
时间: 2024-12-05 08:43:35

UVA之102 - Ecological Bin Packing的相关文章

UVa 102 Ecological Bin Packing (water ver.)

102 - Ecological Bin Packing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=38 Background Bin packing, or the placement of objects of certain weights int

Linux Shell脚本之通过json判断应用程序内部运行状态

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dgd2010.blog.51cto.com/1539422/1686475 之前写过一篇<Zabbix监控之Linux命令行/Shell脚本解析json>,文章提到一种"利于Zabbix监控报警的json数据格式",便于运维人员通过API获取应用程序中的运行状态.内部依赖关系等.但有的开发人员没有按照这种格式来,而是采用了下面的这种json数据格式: 成功

Apache Spark源码走读(五)部署模式下的容错性分析 &amp;standalone cluster模式下资源的申请与释放

<一>部署模式下的容错性分析 概要 本文就standalone部署方式下的容错性问题做比较细致的分析,主要回答standalone部署方式下的包含哪些主要节点,当某一类节点出现问题时,系统是如何处理的. Standalone部署的节点组成 介绍Spark的资料中对于RDD这个概念涉及的比较多,但对于RDD如何运行起来,如何对应到进程和线程的,着墨的不是很多. 在实际的生产环境中,Spark总是会以集群的方式进行运行的,其中standalone的部署方式是所有集群方式中最为精简的一种,另外是Me

调度器之单体调度器

本文讲的是调度器之单体调度器,[编者的话]本文描述了几个简单的单体调度器,基本的配置以及如何在Deis切换调度后端. 调度是一种向处理资源分配工作载荷的方式.在分布式环境中,调度器格外为大家需要,尤其是那些提供扩展性,资源意识以及高效能特性的调度器. 单体调度器是单个进程实体,进行调度决策并完成需要被调度的任务的部署.这些任务可以是长期运行的服务器程序,短期存在的批处理命令,MapReduce查询等等. 为了调度任务的决策,单体调度器需要:观察集群中资源的可用性(例如CPU.内存等),锁住资源,

使用 Apache Mesos 打造分布式资源调度系统

Netflix使用Apache Mesos运行了一系列批处理.流式处理,以及服务类型的工作负载.两年多来,我们创建了层出不穷的用例,例如实时异常检测.批处理作业的训练和模型构建.机器学习编排,以及基于Node.js的微服务.最近发布的Apache Mesos 1.0意味着这项技术已经成熟,相比我们首次使用该服务时已经有了巨大的改进. 我们最初使用Apache Mesos的动力主要在于该技术能够将来自同一个EC2实例的资源更为细化地分配给不同规模的任务.如果不使用Mesos或其他类似的资源管理器,

030_《Delphi COM深入编程》

<Delphi COM深入编程> Delphi 教程 系列书籍 (030) <Delphi COM深入编程> 网友(邦)整理 EMail: shuaihj@163.com 下载地址: Part1 Part2     书名: Delphi COM深入编程 作者: (美)Eric Harmon著 陈旭等译 出版社: 机械工业出版社 书号: 7111082710 出版日期:2000年10月 开本: 787*1092 1/16 页码: 473 版次: 2000年10月第一版第一次印刷 内容

燃!阿里11篇论文入选IJCAI2017 人工智能领域捷报频传

在今年的国际人工智能联合会议(IJCAI)上,阿里巴巴有11篇论文入选.这是继今年的CVPR会议入选4篇.KDD会议入选5篇后,阿里巴巴在人工智能顶级会议上斩获的最新成果. IJCAI被认为是人工智能领域最顶级的学术会议之一,涵盖机器学习.计算可持续性.图像识别.语音技术.视频技术等,对全球人工智能行业具有巨大影响力.今年IJCAI共收到2540篇论文投稿,再创历史新高,最终录用660篇,录用率26%. 阿里巴巴入选的11篇论文中,有6篇论文来自阿里巴巴-浙大前沿技术联合研究中心,3篇来自蚂蚁金

优化介绍及应用实践

云栖TechDay第33期,阿里巴巴iDST Staff Engineer杨森带来题为"优化介绍及应用实践"的演讲.本文主要从用户需求开始谈起,对婚姻配对算法进行了介绍,重点谈及了分配问题.路径规划和组合优化等问题,最后总结了优化的重要性.   以上是精彩内容整理: 用户需求 说起阿里巴巴,第一个标签肯定是电商平台.电商平台的核心问题就是如何关联用户.商品和卖家,是否成功的关键就是如何去高效率的匹配.如何高效率的匹配用户和商品,这直接影射出两个非常直观的问题: 首先我们要知道用户想要什

UVa 10300 Ecological Premium (water ver.)

10300 - Ecological PremiumTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=1241 German farmers are given a premium depending on the conditions at their far