这题用动态规划,S[i][j]表示坐标i到坐标j之间最大的矩形面积,注意坐标从1开始,一直到n-1
注意元素下标即可
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<limits>
using namespace std;
//最大的矩形
const int maxn=1010;
int S[maxn][maxn];//S[i][j]表示下标i到下标j之间的最大矩形面积
int h[maxn];//h[1]-h[n]
int main()
{
int i, j;
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
scanf("%d",&h[i]);
}
int minh;
int maxlen;
for (int i = 1; i <= n; i++)
{
minh = h[i];
maxlen = 1;
S[i][i + 1] = h[i];
for (int j = i+2; j <= n+1; j++)
{
if (h[j-1] >= minh)
{
maxlen++;
S[i][j] = minh * maxlen;
}
else
{
minh = h[j-1];
maxlen++;
S[i][j] = max(S[i][j-1], minh * maxlen);
}
}
}
int maxS = -1;
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n+1; j++)
{
if (S[i][j] > maxS)
maxS = S[i][j];
}
}
printf("%d",maxS);
return 0;
}