F - Pipe Fitters解题报告(熊禾强)

F - Pipe Fitters
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

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

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 128. Note that a cross section with dimensions a*b can also be viewed as a cross section with dimensions b * a.

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.

Sample Input

3 3
2.9 10
2.9 10.5
11 11

Sample Output

9 grid
29 skew
30 skew
126 skew

题目大意:给定一个矩形截面的长和宽,给出一种最优方案,使得这个矩形能放进最多的直径为1的管子

这纯粹就是数学题:

解题思路:

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

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

 

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

 

 

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

 

代码:

#include<stdio.h>
#include<math.h>
const double h=sqrt(3.0);
int main()
{
	double a,b;
	int x1,x2,x3,sum;
	while(scanf("%lf%lf",&a,&b)!=EOF)
	{
		x1=(int)a*(int)b;
		sum=(a-1)/(h/2)+1;//以a为底
       if(b-(int)b-0.5>=0)
		   x2=(int)b*sum;
	   else
		   x2=(int)b*sum-sum/2;
	   sum=(b-1)/(h/2)+1;//以b为底
	   if(a-(int)a-0.5>=0)
		   x3=(int)a*sum;
		   else
		   x3=(int)a*sum-sum/2;
	   if(x1>=x2 && x1>=x3) printf("%d grid\n",x1);
	   else if(x2>x3) printf("%d skew\n",x2);
	   else printf("%d skew\n",x3);
	  }
return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值