Problem description
n adjacent rectangles are placed on the horizontal axis, the width of each rectangle is 1, and the height of the first (1≤i≤n) rectangle 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 10 code:
1#include <fstream>2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cmath>7#include <cstdlib>8 9 using namespacestd;Ten One #definePI ACOs (-1.0) A #defineEPS 1e-10 - #defineLLL __int64 - #definell Long Long the #defineINF 0x7fffffff - - intn,ic[10005]; - + intMain () - { + //freopen ("d:\\input.in", "R", stdin); A //freopen ("D:\\output.out", "w", stdout); at intans=-1, t=-1, H; -scanf"%d",&n); - for(intI=0; i<n;i++) scanf ("%d",&ic[i]); - for(intI=0; i<n;i++){ - if(ic[i]<=t) { -T=ic[i];//note here to update T in Continue; - } toh=t=Ic[i]; + for(intj=i+1; j<n;j++){ - if(ic[j]<h) { theAns=max (ans,h* (J-i)); *H=Ic[j]; $ }Panax Notoginseng } -Ans=max (ans,h* (ni)); the } +printf"%d\n", ans); A return 0; the}
View Code
Three---Largest rectangles of CCF certification simulation problem