关闭

poj 1319 Pipe Fitters

标签: containersinputoutputintegerunixeach
285人阅读 评论(0) 收藏 举报

Pipe Fitters

以下思路来源于:http://www.cnblogs.com/devymex/archive/2010/08/15/1799966.html

Time limit: 3.000 seconds
限时:3.000秒

Background
背景

Filters, or programs that pass "processed" data through in some changed form, are an important class of programs in the UNIX operating system. A pipe is an operating system concept that permits data to "flow" between processes (and allows filters to be chained together easily.)
在UNIX操作系统中,过滤器是重要的一类程序。通过它“处理”的数据,其格式会发生改变。管道是操作系统中的一个概念,它使得数据可以在进程间流动(也很容易和过滤器相连,协同工作)。

This problem involves maximizing the number of pipes that can be fit into a storage container (but it's a pipe fitting problem, not a bin packing problem).
这个问题是关于使装入贮藏箱的管子数量最大化(这是关于管子贮藏的问题,不是装箱问题)。

The Problem
问题

A company manufactures pipes of uniform diameter. All pipes are stored in rectangular storage containers, but the containers come in several different sizes. Pipes are stored in rows within a container so that there is no space between pipes in any row (there may be some space at the end of a row), i.e., all pipes in a row are tangent, or touch. Within a rectangular cross-section, pipes are stored in either a grid pattern or a skew pattern as shown below: the two left-most cross-sections are in a grid pattern, the two right-most cross-sections are in a skew pattern.
一家公司生产一种直径相同的管子。所有管子都存储在矩形贮藏箱中,但贮藏箱的尺寸大小不一。管子按行排列在一个贮藏箱内,以使每行管子之间没有任何的空隙(在一行的两端可能存在空隙),也就是说所有放在同一行的管子都是相切的,或说是外接的。下图显示了一个贮藏箱的矩形横截面,可以看到管子只按“网格”和“交错”两种方式装入:左边的两组横截面是“网格”方式,右边的两组横截面是“交错”方式。

管道

Note that although it may not be apparent from the diagram, there is no space between adjacent pipes in any row. The pipes in any row are tangent to (touch) the pipes in the row below (or rest on the bottom of the container). When pipes are packed into a container, there may be "left-over" space in which a pipe cannot be packed. Such left-over space is packed with padding so that the pipes cannot settle during shipping.
注意,任何一行相邻的管子间都没有间隔,这可能在图中看不清楚。每行管子都与下一行管子相切(外接),最底下一行与贮藏箱底相切。当管子装入贮藏箱后,可能会留下一些不够放进一个管子的空间。因此需要将这些空间填充满,否则船运将无法启程。

The Input
输入

The input is a sequence of cross-section dimensions of storage containers. Each cross-section is given as two real values on one line separated by white space. The dimensions are expressed in units of pipe diameters. All dimensions will be less than 27. Note that a cross section with dimensions a×b can also be viewed as a cross section with dimensions b×a.
输入是一组 贮藏箱横截面的尺寸。每个横截面数据独占一行,给出两个实数值,中间以空格隔开。尺寸的单位就是 一个管子的直径。所有尺寸都小于27。注意,尺寸为a×b的横截面,也可看做是b×a的

The Output
输出

For each cross-section in the input, your program should print the maximum number of pipes that can be packed into that cross section. The number of pipes is an integer -- no fractional pipes can be packed. The maximum number is followed by the word "grid" if a grid pattern results in the maximal number of pipes or the word "skew" if a skew pattern results in the maximal number of pipes. If the pattern doesn't matter, that is the same number of pipes can be packed with either a grid or skew pattern, then the word "grid" should be printed.
对应于每一行横截面的输入,你的程序要打印出在这个横截面中能装入管子的最大数量。该数量由一个整数表示——不存在半个管子的情况。后面输出一个单词“grid”或着“skew”,分别表示达到最大数量的装法是网格或交错。如果两种装法都能够达到相同的最大数量,则输出“grid”。

Sample Input
输入示例

3 3
2.9 10
2.9 10.5
11 11

Sample Output
输出示例

9 grid
29 skew
30 skew
126 skew


Analysis
分析

这是很有意思的一道数学题。按题目要求每行管子之间不能有空隙,因此任意上下两个管子的位置关系只能有两种:正对或60度斜错,其它的角度都会使上一行或下一行的管子间产生空隙。题目还给出管子的直径是一个单位,这样问题就简单多了。那么如果是网格方式排列(即上下正对),那么直接将贮藏箱的长宽取整即为可容纳管子的数量。

如果是交错排列(上下行斜错60度),情况稍有点复杂。为方便描述,我们把箱子最底一的一行称为第0行,之上一行为第1行,以此类推。第0行的行高显然为直径,从第1行开始向上,每行的行高都为下面一行的顶到这一行顶的距离。该行高可简单的用勾股定理计算,方法见下图:


skew

图中r为半径,d为直径,红色等边三角形的顶点分别为三个管子横截面的圆心。可以看出,上面一行的行高即等于该三角形的高,由此可计算出贮藏箱一共可以放多少行。但列的情况又稍有不同,如下图所示:

skew1

如果最底一行在放满管子后,剩下的空间大于半径,那么上面的所有行都可放置相同数量的管子。但如果剩下的空间不足半径,那么奇数行就只能放下的数量就比偶数行少1个。为方便计算,可先按每行相同数量的管子乘以行数,得到所有的管子数量,然后减掉不能放下的奇数行的管子。具体实现方法详见下面代码中的注释。特别强调:

要注意输入的数据可能小于1的情况。

#include<iostream>
using namespace std;
#include<math.h>
const double P=sqrt(3.0)/2.0;
const double eps=1e-8;
int main()
{
	int max1,max2,max3,max,t;
	double a,b;
	while(cin>>a>>b)
	{
		max1=floor(a)*floor(b);
		max2=floor(a);
		t=1;
		while(1)
		{
			t++;
			if(b-(t-1)*P-1<eps)
				break;
			if(t%2==0)
		    	max2+=floor(a-0.5);
			else max2+=floor(a);
		}
		max3=floor(b);
		t=1;
		while(1)
		{
			t++;
			if(a-(t-1)*P-1<eps)
				break;
			if(t%2==0)
	    		max3+=floor(b-0.5);
			else
				max3+=floor(b);
		}
		max=max1;
		if(max<max2)
			max=max2;
		if(max<max3)
			max=max3;
		if(a-1.0<eps||b-1.0<eps)
			max=0;
		if(max==max1)
			cout<<max<<" "<<"grid"<<endl;
		else
			cout<<max<<" "<<"skew"<<endl;
	}
	return 0;
}


 


#include<iostream>
using namespace std;
#include<math.h>
const double P=sqrt(3.0)/2.0;
const double eps=1e-8;
int main()
{
	int max1,max2,max3,max,t;
	double a,b;
	while(cin>>a>>b)
	{
		max1=floor(a)*floor(b);
		max2=floor(a);
		t=1;
		while(1)
		{
			t++;
			if(b-(t-1)*P-1<eps)
				break;
			if(t%2==0)
		    	max2+=floor(a-0.5);
			else max2+=floor(a);
		}
		max3=floor(b);
		t=1;
		while(1)
		{
			t++;
			if(a-(t-1)*P-1<eps)
				break;
			if(t%2==0)
	    		max3+=floor(b-0.5);
			else
				max3+=floor(b);
		}
		max=max1;
		if(max<max2)
			max=max2;
		if(max<max3)
			max=max3;
		if(a-1.0<eps||b-1.0<eps)
			max=0;
		if(max==max1)
			cout<<max<<" "<<"grid"<<endl;
		else
			cout<<max<<" "<<"skew"<<endl;
	}
	return 0;
}


 

 

0
0

猜你在找
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20501次
    • 积分:376
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:22篇
    • 译文:0篇
    • 评论:0条
    文章存档