动态规划D - Largest Rectangle in a Histogram HDU - 1506
2017-10-23 22:26
357 查看
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Input
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, …, hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
Output
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; /** * Created by 95112 on 10/23/2017. */ public class LargestRectangle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Long> answers = new ArrayList<>(); int n ; do { n = scanner.nextInt(); if (n == 0) break; HashMap<Integer,Integer> map = new HashMap<>(); int[] left = new int ; int[] right = new int ; int[] input = new int ; left[0] = 0; right[n-1] = n-1; for (int i = 0 ; i < n; i ++ ){ input[i] = scanner.nextInt(); } long sum = 0; for (int i = 1 ; i < n ; i++) { int j =i ; while (j > 0 && input[j-1] >= input[i] ) { j = left[j-1]; } left[i] = j; } for (int i = n-2; i >=0 ; i--) { int j = i ; while ( j < n - 1 && input[j+1] >= input[i]) { j = right[j+1]; } right[i] = j; } for (int i= 0 ; i< n; i++) { Long length = Long.valueOf((right[i] - left[i] +1 )); Long tmp = length * input[i]; if (tmp > sum){ sum = length * input[i]; } } answers.add(sum); }while (true); for (Long answer : answers) System.out.println(answer); } }
相关文章推荐
- HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)
- hdu 1506 Largest Rectangle in a Histogram[动态规划]
- HDU 1506 Largest Rectangle in a Histogram 【动态规划】
- Largest Rectangle in a Histogram(HDU 1506 动态规划)
- HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)
- HDU-1506 Largest Rectangle in a Histogram 动态规划
- hdu 1506 Largest Rectangle in a Histogram(动态规划,很棒的优化)
- hdu 1506 Largest Rectangle in a Histogram(DP)
- hdu 1506(nyoj 258)Largest Rectangle in a Histogram(最大长方形(二))(单调栈)
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1507 Largest Rectangle in a Histogram 动态规划计算最大面积
- HDU 1506 Largest Rectangle in a Histogram(单调栈)
- HDU 1506 Largest Rectangle in a Histogram
- HDU1506 Largest Rectangle in a Histogram
- hdu 1506(nyoj 258)Largest Rectangle in a Histogram(最大长方形(二))(单调栈)
- HDU_1506_Largest Rectangle in a Histogram_dp
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram(dp求最大子矩阵和)
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
- HDU 1506 Largest Rectangle in a Histogram