121 - Pipe Fitters

UVa網站題目連結
My Solved Problems Performance

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.

picture26

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 tex2html_wrap_inline133 . Note that a cross section with dimensions tex2html_wrap_inline135 can also be viewed as a cross section with dimensions tex2html_wrap_inline137 .

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.

Sample Input

3 3
2.9 10
2.9 10.5
11 11

Sample Output

9 grid
29 skew
30 skew
126 skew

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;

  3. public class Main {

  4. public static void main (String[] args) {

  5. double double1, double2;
  6. int grid, skew;
  7. Scanner in = new Scanner(new BufferedInputStream(System.in));

  8. while (in.hasNext()) {
  9. double1 = in.nextDouble();
  10. double2 = in.nextDouble();
  11. grid = (int)(double1) * (int)(double2);
  12. skew = skew(double1, double2);
  13. if (grid>=skew) {
  14. System.out.println(grid + " grid");
  15. } else {
  16. System.out.println(skew + " skew");
  17. }
  18. }
  19. }

  20. private static int skew(double double1, double double2) {

  21. if ((double1<1)|(double2<1)) {
  22. return 0;
  23. } else {
  24. int skew1 =(((int)((double1-1)/(1.732/2))+1+1)/2 * (int)(double2) +
  25. ((int)((double1-1)/(1.732/2))+1)/2 * (int)(double2-0.5));
  26. int skew2 =(((int)((double2-1)/(1.732/2))+1+1)/2 * (int)(double1) +
  27. ((int)((double2-1)/(1.732/2))+1)/2 * (int)(double1-0.5));
  28. return (skew1>skew2) ? skew1 : skew2;
  29. }
  30. }
  31. }
關鍵字:UVa Online Judge, ACM. Java

這個網誌中的熱門文章

繳納2018/107年度牌照稅信用卡回饋/分期整理!

證件遺失補辦身分證/健保卡/駕照/行照/信用卡總整理

2018最新博客來100,50,25折價券序號e-coupon分享

國泰Koko combo icash網購/超商/icash/繳費5%!

所得稅要繳多少?最新最完整的所得稅計算教學!再教你看懂稅率級距表/免稅額/扣除額!

Line Pay最新信用卡活動+店家30%+回饋整理!

街口支付最新信用卡+店家街口幣回饋整理!

週六全家台新滿百最高送20可再搭三大Pay活動!

TOEIC多益成績分數查詢、計算標準與換算對照表

信用卡額度太高想調低?千萬別這麼做!