# poj 1319 Pipe Fitters

356人阅读 评论(0)

Pipe Fitters

Time limit: 3.000 seconds

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

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.

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

3 3
2.9 10
2.9 10.5
11 11

9 grid
29 skew
30 skew
126 skew

## Analysis分析

#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网站的观点或立场
个人资料
• 访问：21325次
• 积分：380
• 等级：
• 排名：千里之外
• 原创：13篇
• 转载：22篇
• 译文：0篇
• 评论：0条
文章存档