CCF201312-3 largest rectangle (100)

Question number 201312-3
Questions Name Largest rectangle
time limit 1.0s
Memory Limit 256.0MB
Problem Description Description of the problem:
  put the n adjacent rectangles on the horizontal axis, the width of each rectangle is 1, and the first i (1 ≤ i ≤ n) of the rectangular height hi. This constitutes a rectangle the n histograms. For example, in FIG. (FIG see below) six rectangular height on respectively 3, 1, 6, 5, 2, 3. Find the histogram can be placed in a given area of the largest rectangle in which the side to be parallel to the axis. For the example given above, the maximum rectangular as shown shaded in FIG., The area of 10.
Input format
  The first line contains an integer n, i.e. the number of rectangles (1 ≤ n ≤ 1000).
  The second line contains n integers h1, h2, ..., hn, between adjacent numbers separated by a space. (1 ≤ hi ≤ 10000). hi is the i-th height of the rectangle.
Output format
  output line, comprising an integer, i.e. to the largest histogram within a given rectangle.
Sample input
. 6
. 3. 1. 3 2. 6. 5
Sample Output
10

Input:
Here Insert Picture Description
Results:
Here Insert Picture Description
The first method method violence search elements (O (n ^ 2)) (100 points):
Since each rectangle starts with a small rectangle is i, and finally another small rectangular j (j> = i ), so long as to find the largest area of these rectangles.

public class Main {
  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    int[] h = new int[1000];
    int num = scan.nextInt();

    // 读取数据
    for (int i = 0; i < num; i++) {
      h[i] = scan.nextInt();
    }
    int MaxArea = 0;
    for (int i = 0; i < num; i++) {
      int height = h[i];
      for (int j = i; j < num; j++) {
        if (height > h[j]) // 以高度最低的矩形为合成矩形的高度
          height = h[j];
        
        int area = height * (j - i + 1); //计算以j为终止,以i为起始的最大矩形面积
        if(area>MaxArea)
          MaxArea = area;
      }
    }
    System.out.println(MaxArea);
  }
}

The second method is relatively clever, because each of rectangle certainly is high for a small rectangle own high. Therefore, we only need to find each small rectangle is the largest rectangular area at high, then the largest of which can be selected. Below, respectively, each of the small rectangle is high, marked out below its maximum width corresponding, respectively, to calculate the area of a rectangle 3,661,083, the final result 10 is
Here Insert Picture Descriptionused here to hold a stack of rectangular increasing height a sequence of rectangular stack stored index (0,1,2,3,4 ...). Just scan it again you can find the largest rectangular. When determining the width of rectangle, calculating the area of rectangle, and updates the maximum area of each rectangle is calculated with a high stack rectangular area is high.

  • When faced with h [i]> h [stack], to be described h [stack] the width of rectangle is high also be extended to the right, it will push i.
  • When faced with h [i] <h [stack], to be described h [stack] The width of rectangle has been determined is high, it is calculated in h [stack] The rectangular area of ​​synthetic high. Its width should be i - stack --1 (incremented since the stack height of the rectangle, the rectangle synthesis "left point" should be an element of the stack), the calculation is completed, updates the maximum area of ​​the rectangle.
import java.util.Scanner;
import java.util.Stack;

public class Main {
  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int[] h = new int[1001];
    int num = scan.nextInt();
    
    //读取数据
    for (int i = 0; i < num; i++) {
      h[i] = scan.nextInt();
    }
    h[num] = 0;//将最后一个数置为0,因为0<任何矩形的高,所以当i = num时,会计算出栈中所有矩形的合成矩形的面积
    int MaxArea = 0;
    Stack<Integer> stack = new Stack<Integer>();
    for (int i = 0; i <= num; i++) {
      if (stack.empty() || h[stack.peek()] < h[i]) {
        stack.push(i);
      } else {
        int temp = stack.pop(); // 弹出当前栈顶,并计算以该小矩形为高时的最大矩形面积
        int area = h[temp]*(stack.empty() ? i : i - stack.peek() - 1); //计算面积,高度*宽度
        if(area > MaxArea) {
          MaxArea = area;
        }
        i--; //重新判断该小矩形是否可以入栈
      }
    }
    System.out.println(MaxArea);
  }
}
Published 29 original articles · won praise 10 · views 7174

Guess you like

Origin blog.csdn.net/weixin_42017042/article/details/94473004