单调栈的性质:
(1):栈中元素是单调递增或单调递减的
(2):后进先出
应用:求某一元素左(右)边第一个比它大(小)的值。
Description
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people’s memories about some period of life.
A new idea Bill has recently developed assigns a non-negative integer value to each day of human
life. Bill calls this value the emotional value of the day. The greater the emotional value is, the better
the day was. Bill suggests that the value of some period of human life is proportional to the sum of the
emotional values of the days in the given period, multiplied by the smallest emotional value of the day
in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.
Now Bill is planning to investigate his own life and find the period of his life that had the greatest
value. Help him to do so.
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are
separated by a single blank line.
The first line of the input file contains n — the number of days of Bill’s life he is planning to
investigate (1 ≤ n ≤ 100000). The rest of the file contains n integer numbers a1, a2, . . . , an ranging
from 0 to 106 — the emotional values of the days. Numbers are separated by spaces and/or line breaks.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases
will be separated by a blank line.
On the first line of the output file print the greatest value of some period of Bill’s life.
On the second line print two numbers l and r such that the period from l-th to r-th day of Bill’s
life (inclusive) has the greatest possible value. If there are multiple periods with the greatest possible
value, then print any one of them.
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5
题意:求一个区间,使得区间和乘上区间最小值最大。输出该值和区间的l,r(要输出区间长度最短的,区间长度一样输出左端点最左的)。
做法:设计一个结构体,x代表值,id代表下标,l、r代表以当前值为区间最小值时的最长区间的最左端和最右端,维护一个单调递减的栈。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 100005;
typedef struct
{
int l,r,id;
ll x;
}node;
node a[maxn];
ll sum[maxn];
int main()
{
int n;
int num=1;
while(scanf("%d",&n) != EOF)
{
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i].x);
a[i].id = i;
sum[i] = sum[i-1] + a[i].x;//预处理前缀和
}
stack<node>s;
for(int i=1; i<=n; i++)
{
a[i].l = i;//一开始l都等于自己的id
while(s.empty() == 0 && s.top().x >= a[i].x)//维护单调递减的栈
{
a[s.top().id].r = i - 1;//当前值小于等于栈顶元素,说明i-1是以栈顶元素为最小值的最长区间的最右端
a[i].l = a[s.top().id].l;//以当前值为最小值的最长区间的最左端是栈顶元素的id
s.pop();
}
s.push(a[i]);
}
while(s.empty() == 0)//遍历完所有元素,如果栈还不为空,说明栈中元素的最长区间的最右端都为n
{
a[s.top().id].r = n;
s.pop();
}
ll ans = -1;
int l = 0,r = 0;
for(int i=1; i<=n; i++)
{
if((sum[a[i].r] - sum[a[i].l - 1]) * a[i].x > ans)
{
ans = (sum[a[i].r] - sum[a[i].l - 1]) * a[i].x;
if(ans == 0)
{
l = a[i].id;
r = a[i].id;
}
else
{
l = a[i].l;
r = a[i].r;
}
}
else if((sum[a[i].r] - sum[a[i].l - 1]) * a[i].x == ans)
{
if(a[i].r - a[i].l < r - l)//输出最短的区间长度
{
l = a[i].l;
r = a[i].r;
}
else if(a[i].r - a[i].l == r - l)//区间长度相同,输出左端点在最左的
{
if(a[i].l < l)
{
l = a[i].l;
r = a[i].r;
}
}
}
}
if(num != 1)
printf("\n");
num++;
printf("%lld\n",ans);
printf("%d %d\n",l,r);
}
}
Description:
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
There is a row of trees along the East-9 Road which consists of N trees. Now that we know the height of each tree, the gardeners of HUST want to know the super-neatness of this row of trees. The neatness of a sequence of trees is defined as the difference between the maximum height and minimum height in this sequence. The super-neatness of this sequence of trees is defined as the sum of neatness of all continous subsequences of the trees.
Multiple cases please process until the end of input. There are at most 100 test cases.
题意:求所有子序列的最大值-最小值的和
做法:把所有子序列都列出来显然不现实,那么我们可以把每个子序列的最大值的和统计出来,再减去最小值的和,我们又想,每个子序列的最大值、最小值是有可能重复的,那么我们就可以转换成每个数作为最大值、最小值分别出现了多少次,即找每个数最左和最右第一个比它小的数、第一个比它大的数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
int a[maxn],mxl[maxn],mxr[maxn],mil[maxn],mir[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
mxl[i] = i,mil[i] = i;
mxr[i] = n,mir[i] = n;
}
stack<int> s1,s2;
for(int i=1; i<=n; i++)
{
while(!s1.empty() && a[i] > a[s1.top()])
{
mxl[i] = mxl[s1.top()];
mxr[s1.top()] = i-1;
s1.pop();
}
s1.push(i);
while(!s2.empty() && a[i] < a[s2.top()])
{
mil[i] = mil[s2.top()];
mir[s2.top()] = i-1;
s2.pop();
}
s2.push(i);
}
ll ans = 0;
for(int i=1; i<=n; i++)
{
ll k1 = (ll)(mxr[i]-i+1)*(i-mxl[i]+1);//作为最大值出现的次数
ll k2 = (ll)(mir[i]-i+1)*(i-mil[i]+1);//作为最小值出现的次数
ans += a[i]*(k1-k2);
}
printf("%lld\n",ans);
}
}