单调栈。
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX = 3e4 + 5;
ll arr[MAX];
struct NODE
{
ll l, val;
NODE(ll tmp_l = 0, ll tmp_val = 0)
: l(tmp_l), val(tmp_val) {}
};
int main()
{
int t, cas = 0;
scanf("%d", &t);
stack<NODE> sta;
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%lld", &arr[i]);
sta.push(NODE(0, 0));
NODE tmp;
ll res = 0;
for (int i = 1; i <= n; ++i)
{
tmp.l = i;
while (sta.top().val > arr[i])
{
tmp = sta.top();
sta.pop();
ll tmp_res = tmp.val * (i - 1 - tmp.l + 1);
res = max(tmp_res, res);
}
sta.push(NODE(tmp.l, arr[i]));
}
while (!sta.empty())
{
NODE tmp = sta.top();
sta.pop();
res = max(res, tmp.val * (n - tmp.l + 1));
}
printf("Case %d: %lld\n", ++cas, res);
}
return 0;
}