日期:2014-05-20  浏览次数:21142 次

【C# 每日一题9】求最大的矩形
C# code

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


解释一下:
输入为:7 2 1 4 5 1 3 3
的时候表示,后面有7个数字,每个数字m代表宽度为1,高度为m的矩形,之后他们都挨着,这样呢,我们需要从中间找出最大的一个矩形结构(可以跨越多个矩形,这个看图大家就理解了)!

输出找到的矩形的面积8
8来自于第三个和第四个矩形,他们两个组成了宽度为2,高度为4的矩形,面积为8

大家想想有什么好办法么?
硬算肯定是可以解决问题,但是好像效率上有点问题,也欢迎大家贴一下代码,练练手啊!!


------解决方案--------------------
说说思路:循环后面七个数,比对旁边的数,大于或者对于自己则继续比对下去,一圈之后再比较各个结果。
------解决方案--------------------
C# code

        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,只是高度不同,是吗?
稍微想了一下,还没有思路。
------解决方案--------------------
写错了一行
C# code
        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();
        }

------解决方案--------------------