Va OJ 121-pipe fitters (pipe mounting)

Source: Internet
Author: User

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

 

Background
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 .)
In UNIX operating systems, filters are an important class.Program. The format of the data that is "processed" is changed. A pipeline is a concept in an operating system that allows data to flow between processes (or easily connect to a filter and work collaboratively ).

This problem involves maximizing the number of Pipes that can be fit into a storage iner (but it's a pipe fitting problem, not a bin packing problem ).
This problem is about loadingThe maximum number of tubes in the storage box (this is a problem about Tube Storage, not the packing problem ).

 

The Problem
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.
A company produces a pipe of the same diameter. All tubes are stored in rectangles.Storage box,The storage bins are of different sizes. Pipes are arranged in one rowIn the storage box, there is no gap between each row of pipes (there may be gaps at both ends of a row). That is to say, all the tubes placed in the same row are tangent, or they are external. AThe rectangular cross section of the storage box shows that only"NetworkGrid and staggered Load: Two groups on the leftCross SectionIs a "Grid" mode, two groups on the rightCross SectionIt is an "staggered" method.

 

 

Note that although it may not be apparent from the divisor, 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.
Note that there is no interval between adjacent pipes in any row, which may be invisible in the figure. Each line of pipe is tangent to the next line of pipe (external), the bottom line andStorage Tank Bottom tangent. When the pipe is loadedThe storage may leave some space that is insufficient for a pipe. Therefore, you need to fill up the space, otherwise the shipping will not start.

 

The input
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 ×.
Input is a group The size of the storage box cross section. Each cross-section data exclusive to one row. Two real values are given, separated by spaces in the middle. The unit of dimensions is  The diameter of a pipe. All dimensions are smaller than 27. Note: The size is a × B.Can also be seenB × A's.

 

The output
Output

For each cross-section in the input, your program shocould 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" shocould be printed.
For each row of cross-section input, your program needs to print the maximum number of tubes that can be loaded in this cross section. This number is represented by an integer-if there are no half tubes. The following output is a word "Grid" or "skew", respectively, indicating that the maximum number of installation methods is grid or staggered. If both methods can reach the same maximum number, "Grid" is output ".

 

Sample Input
Input example

3 3
2.9 10
2.9 10.5
11

 

Sample output
Output example

9 Grid
29 skew
30 skew
126 skew

Analysis
Analysis

This is an interesting mathematical problem. There is no gap between each pipe line according to the question requirement. Therefore, there can be only two types of positional relationships between any two pipes: positive or 60 degrees oblique, other angles create gaps between the tubes of the previous or next line. The question also shows that the diameter of the pipe is a unit, which makes the problem much simpler. If it is in the grid (that is, upper and lower positive), directlyThe storage box is rounded up to accommodate the number of tubes.

If it is staggeredArrange (the upper and lower rows are skewed to 60 degrees)The situation is a little complicated. For ease of description, we refer to the bottom line of the box as 0th rows, the last row as 1st rows, and so on. The Row Height Of The 0th rows is obviously the diameter. From the beginning of the 1st rows, the height of each row is the distance from the top of the following row to the top of this row. This line can be calculated simply by using the hook theorem. For details, refer:


 

In the figure, r is the radius, D is the diameter, and redEqual edgeThe triangle vertices are the center of the cross section of the three tubes. It can be seen that the Row Height of the above line is equal to the height of the triangle, and thus can be calculatedHow many rows can be stored in the storage box. The column conditions are slightly different, as shown in:

 


 

If the bottom row is filled with tubes and the remaining space is greater than the radius, all rows above can be placed with the same number of tubes. However, if the remaining space is less than the radius, the number of rows that can be dropped by an odd number is one less than that of an even number. For easy calculation, multiply the number of tubes in each row by the number of rows to get the number of tubes, and then remove the pipes in the odd rows that cannot be put down. For detailed implementation methods, see the followingCode.

 

Solution
Answer

# Include <iostream> using namespace STD; // calculate the number of int skew (float X, float y) in the staggered permutation method {// fsqrt3_2 is the start 3 divided by 2, it indicates the distance between the top of two adjacent rows from static const float fsqrt3_2 = 0.8660254f; // you can calculate the total number of rows that can be ranked. Except that the base Row Height is 1, the rest rows are fsqrt3_2int nrows = (Y> = 1) + (INT) (Y-1)/fsqrt3_2 ); // calculate the full number of columns in the bottom row first. If the remaining space in the bottom row is less than 0.5 // the odd number of rows (the bottom row is 0) the number of columns is one less than the number of even rows. You must subtract 1 return (nrows * (INT) x-(nrows/2) * (X-(INT) x <0.5f);} // main function int main (void) {// read and process all data cyclically for (float X, Y; CIN >>> x> Y; cout <Endl) {// grid arrangement, that is, a simple row and column is rounded up. In the staggered mode, int ngrid = (INT) x * (INT) y, nskew = max (skew (x, y), and skew (Y, x) are calculated )); // The maximum number of output pipes and their arrangement mode cout <max (ngrid, nskew) <(ngrid> = nskew? "Grid": "skew");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.