CCF2013123 the largest rectangle (C language version)

Source: Internet
Author: User

Tag: color int determines IDT width share shadow print void

The
problem description places n adjacent rectangles on the horizontal axis, each rectangle having a width of 1, and the height of the rectangle I (1≤i≤n) is hi. These n rectangles form a histogram. For example, the height of the six rectangles is 3, 1, 6, 5, 2, 3, respectively.



Find the rectangle with the largest area in the given histogram, and its edges are parallel to the axis. For the example given above, the maximum rectangle as shown in the shaded portion, the area is 10.
Input format the first line contains an integer n, which is the number of rectangles (1≤n≤1000).
The second line contains n integers h1, h2, ..., HN, and the adjacent numbers are separated by a space. (1≤hi≤10000). Hi is the height of the first rectangle. The output format outputs a row that contains an integer that is the area of the largest rectangle within a given histogram. Sample Input 6
3 1 6 5 2 3 Sample Output
The idea of solving problems: Enumerate all the rectangles that can form the requirements, select the maximum output. It is important to note that the height of the rectangle is determined by the smallest integer hi, so you need to find the minimum hi. Code See below:
1#include <stdio.h>2 #defineNmax 10103 4 intAeraintA[],intn);5 intMainvoid)6 {7     inti, N;8     intA[nmax];9scanf"%d",&n);Ten      for(i=0; I < n; i + +) One     { Ascanf"%d",&a[i]); -     } -printf"%d", Aera (a,n)); the     return 0; -  }  - intAeraintA[],intlen) -  { +     intI, low,temp, Max_aera =-1; -       for(i =0; I <len; i++) +      { Alow = A[i];//set minimum integer to low at           for(intj = i; J <len; J + +) -{//take position I as the starting point and position J as the end point to find the rectangle that meets the requirements. The beginning of each round is unchanged, the end point +1 -              if(A[j] <Low ) -Low =A[j]; -Temp = (J-i +1) * LOW;//calculation formula of rectangular area -              if(Temp >Max_aera) inMax_aera =temp; -         } to     } +      returnMax_aera; -}

CCF2013123 the largest rectangle (C language version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.