日期:2014-05-20 浏览次数:21213 次
Description 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: [img=http://hi.csdn.net/attachment/201106/1/1376742_1306892143sZcG.gif][/img] 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
static void Main(string[] args) { string s = "7 2 1 5 5 1 3 3"; int[] nums= s.Split(' ').ToList().ConvertAll<int>(x => int.Parse(x)).ToArray(); int[] rects = (int[])nums.Clone(); int max = 0; for (int i = 0; i < nums.Length; i++) { for (int j = i + 1; j < nums.Length; j++) { if (rects[i] > max) max = rects[i]; if (nums[i] > nums[j]) nums[i] = nums[j]; rects[i] = nums[i] * (j-i + 1); } } if (rects.Max() > max) max = rects.Max(); Console.Write(max); Console.Read(); }
------解决方案--------------------
各个矩形的宽度都是1,只是高度不同,是吗?
稍微想了一下,还没有思路。
------解决方案--------------------
写错了一行
static void Main(string[] args) { string s = "7 2 1 5 5 1 3 3"; int[] nums= s.Split(' ').ToList().ConvertAll<int>(x => int.Parse(x)).ToArray(); int[] rects = (int[])nums.Clone(); int max = 0; for (int i = 0; i < nums.Length; i++) { for (int j = i + 1; j < nums.Length; j++) { if (nums[i] > nums[j]) nums[i] = nums[j]; rects[i] = nums[i] * (j-i + 1); if (rects[i] > max) max = rects[i]; } } Console.Write(max); Console.Read(); }
------解决方案--------------------