給定一個數組,定義某個區間的參考值爲:區間所有元素的和*區間最小元素。求該數組中的最大參考值以及對應的區間。
設某個區間所有元素的和爲height,區間最小元素爲width,
則對於單個元素的區間,height = width = 元素的值。
建立一個單調遞增的棧。從第一個元素開始入棧,每個元素入棧之前必須先從棧頂開始刪除大於或等於它的元素,把刪除的所有元素的height累加到當前元素的height,然後把當前元素的值保存在width值中,這表示把當前元素前面比它大或相等的連續元素的值加起來,乘以它自己,也就是這段區間的參考值。每一次刪除元素都需要計算一個參考值,取參考值的最大值就是答案了。不過題目還要求給出對應區間的起點和終點,因此在棧的操作過程中還得記錄當前元素保存的區間的起點和大小,在更新參考值的過程中順便更新區間的起點和終點就可以了。
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100005;
struct Elem
{
long long height;
long long width;
int begin;
int count;
};
Elem stack[N];
int top;
int main()
{
int num, n;
long long ans, tmp, tot;
int ansbeg, ansend, count;
scanf("%d", &n);
top = 0;
ans = 0;
ansbeg = ansend = 1;
for (int i = 0; i < n; ++i)
{
scanf("%d", &num);
tmp = 0;
count = 0;
while (top > 0 && stack[top - 1].width >= num)
{
stack[top - 1].count += count;
tot = (stack[top - 1].height + tmp) * stack[top - 1].width;
if (tot > ans)
{
ans = tot;
ansbeg = stack[top - 1].begin;
ansend = ansbeg + stack[top - 1].count - 1;
}
tmp += stack[top - 1].height;
count = stack[top - 1].count;
--top;
}
stack[top].height = num + tmp;
stack[top].width = num;
stack[top].begin = i + 1 - count;
stack[top].count = 1 + count;
++top;
}
tmp = 0;
count = 0;
while (top > 0)
{
stack[top - 1].count += count;
tot = (stack[top - 1].height + tmp) * stack[top - 1].width;
if (tot > ans)
{
ans = tot;
ansbeg = stack[top - 1].begin;
ansend = ansbeg + stack[top - 1].count - 1;
}
tmp += stack[top - 1].height;
count = stack[top - 1].count;
--top;
}
printf("%lld\n%d %d\n", ans, ansbeg, ansend);
return 0;
}