Ccf201312-3 the largest rectangle (100 points) __CCF

Source: Internet
Author: User

Question number: 201312-3
Question Name: The largest rectangle
Time limit: 1.0s
Memory Limit: 256.0MB
Problem Description: The problem description puts n adjacent rectangles 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 in the following figure is 3, 1, 6, 5, 2, 3.

Find the largest rectangle that can be placed in a given histogram, with its edges parallel to the axis. For the example given above, the largest rectangle is the shaded part shown in the following figure, with an area of 10.
Input format the first row contains an integer n, the number of rectangles (1≤n≤1000).
The second row contains n integers h1, h2, ..., HN, the adjacent number 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, which is the area of the largest rectangle within the given histogram. Sample Input 6
3 1 6 5 2 3 Example output 10

#include <iostream>
#include <math.h>
 
using namespace std;
int main ()
{
	int a[1001];//input array
	int n;
	cin>>n;//input Data Number
	if (n==0) return
	0;
	
	int b[100];//An array of storage area for 
	(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	int k=0; 
	int height;
		int max=0;
		int anx=0;
	for (int j=0;j<n;j++)
	{	
		height=a[j];
		for (int m=j;m<n;m++)
		{
			if (a[m]
This problem encountered a lot of problems, such as the use of array storage, you will report a run error.

The judgment should overflow for an array.

The following code is an error code

Test Sample 5 1 6 5 2 3 The correct result should be 10

and output 20

Comparisons can only be compared by selecting the smallest rectangle.

Because in order to ensure that there will not be a gap between the parts.

Store values as much as possible with variable storage

/* sample input 6 3 1 6 5 2 3 Sample output: #include <iostream> #include <math.h> using namespace std;
	int main () {int a[1001];//input array int n;
	cin>>n;//input Data number if (n==0) return 0;
	int b[100];//An array of storage area for (int i=0;i<n;i++) {cin>>a[i]; 
	int k=0;
	int max=0; 
	int ans; 
				for (int j=0;j<n;j++) {for (int m=j;m<n;m++) {if (A[j]<a[m]) {ans= (A[J)) * (m-j+1);   if (Ans>max) {Max=ans;
				The minimum must be saved for comparison}} else {ans= (a[m]) * (m-j+1);
				if (Ans>max) {Max=ans;

		}}} cout<<max<<endl; 5 1 6 5 2 3 Test Error} 
	/* ccf201312-3 maximum rectangular
	train of thought: two loops  one from array 0 to n-1
	one to compute
	the size of the rectangular area. It's the smallest height in the past,
	so Max is global  . High is also the global 2018/3/8 7min;
* *
#include <iostream>
using namespace std;
const int n=1000;
int array[n];
int main ()
{
	int n;//the number of input rectangles 
	cin>>n;
	int max=0;
	for (int i=1;i<=n;i++)
	{
		cin>>array[i];
		if (Array[i]>max)
		max=array[i];
	}
for	(int i=1;i<=n;i++)
//	{
//		cout<<array[i]<< "";	the
	int mj;//area 
	for (int i=1;i<=n;i++)
	{
		int minheight=array[i];//traversal in the high minimum for 
		( int j=i+1;j<=n;j++)
		{
			if (array[j]<minheight)
			{
				minheight=array[j];
				mj=minheight* (j-i+1);
				if (Mj>max) max=mj;			
			}
			else 
			{
				mj=minheight* (j-i+1);
				if (Mj>max) max=mj
	}}} cout<<max;	
}




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.