【luoguCF817D】【单调栈】Imbalanced Array

传送门

题目描述

You are given an array a a a consisting of n n n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance values of all subsegments of this array.

For example, the imbalance value of array [1,4,1] [1,4,1] [1,4,1] is 9 9 9 , because there are 6 6 6 different subsegments of this array:

[1] [1] [1] (from index 1 1 1 to index 1 1 1 ), imbalance value is 0 0 0 ;
[1,4] [1,4] [1,4] (from index 1 1 1 to index 2 2 2 ), imbalance value is 3 3 3 ;
[1,4,1] [1,4,1] [1,4,1] (from index 1 1 1 to index 3 3 3 ), imbalance value is 3 3 3 ;
[4] [4] [4] (from index 2 2 2 to index 2 2 2 ), imbalance value is 0 0 0 ;
[4,1] [4,1] [4,1] (from index 2 2 2 to index 3 3 3 ), imbalance value is 3 3 3 ;
[1] [1] [1] (from index 3 3 3 to index 3 3 3 ), imbalance value is 0 0 0 ;

You have to determine the imbalance value of the array a a a .

输入格式

The first line contains one integer n n n ( 1<=n<=106 1<=n<=10^{6} 1<=n<=106 ) — size of the array a a a .

The second line contains n n n integers a1,a2… an a_{1},a_{2}…\ a_{n} a1​,a2​… an​ ( 1<=ai<=106 1<=a_{i}<=10^{6} 1<=ai​<=106 ) — elements of the array.

输出格式

Print one integer — the imbalance value of a a a .

题意翻译

对于给定由 n 个元素构成的数组。一个子数组的不平衡值是这个区间的最大值与最小值的差值。数组的不平衡值是它所有子数组的不平衡值的总和。

以下是数组[1,4,1]不平衡值为9的例子,共有6个子序列:

[1] (从第一号到第一号)不平衡值为 0;

[1, 4] (从第一号到第二号), 不平衡值为 3;

[1, 4, 1] (从第一号到第三号),不平衡值为 3;

[4] (从第二号到第二号),不平衡值为 0;

[4, 1] (从第二号到第三号),不平衡值为 3;

[1] (从第三号到第三号)不平衡值为 0;

输入输出样例
输入 #1
3
1 4 1
输出 #1
9


在这里插入图片描述


解题思路

题目中的答案是 ( m a x − m i n ) + ( m a x − m i n ) + ( m a x − m i n ) … … (max-min)+(max-min)+(max-min)…… maxmin+maxmin+maxmin
变个式就是 ( m a x + m a x + m a x … … ) − ( m i n + m i n + m i n … … ) (max+max+max……)-(min+min+min……) max+max+maxmin+min+min
m a x max max=第 i i i个数在几个区间是最大数 x 第 i i i个数的数值

做两个单调栈,一个递减求第 i i i个数在哪些区间里是最大数,一个递增求第 i i i个数在哪些区间里是最小数
在压入时记录左端点,被弹出时记录右端点

求有多少个区间,用 ( i − l [ i ] + 1 ) ∗ ( r [ i ] − i + 1 ) (i-l[i]+1)*(r[i]-i+1) (il[i]+1)(r[i]i+1)(l是第i个数所在区间的左端点,r是第i个数所在区间的右端点)

注意
要记得建墙(也可以在单调栈结束后再做一次,但是有点麻烦)
在这里插入图片描述


#include<iostream>
#include<cstdio>
using namespace std;
struct DT{
	long long v;
	int i;
}f[1000100];
struct demo{
	int l,r;
}ans_max[1000100],ans_min[1000100];
long long n,top,ansmax,ansmin,l,a[1000100];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	    scanf("%lld",&a[i]);
	top=1,a[n+1]=-0x7f7f7f7f;//初始化,建墙
	for(int i=1;i<=n+1;i++){//递增,确定在第i个数是最小值的最大区间
		l=i;
		while(top&&f[top-1].v>=a[i]){
			top--;
			ans_min[f[top].i].r=i-1;//被弹出时记录下右端点
		    l=ans_min[f[top].i].l;//左端点尽量往左靠
	    }
	    if(i<=n)
		   f[top].v=a[i],f[top].i=i,ans_min[f[top].i].l=l,top++;//压入栈,记录左端点
	}
	top=1,a[n+1]=0x7f7f7f7f,f[0].v=0x7f7f7f7f;//初始化,建墙
	for(int i=1;i<=n+1;i++){//递减,确定在第i个数是最大值的最大区间
		l=i;
		while(top&&f[top-1].v<=a[i]){
			top--;
			ans_max[f[top].i].r=i-1;//被弹出时记录右端点
		    l=ans_max[f[top].i].l;//左端点尽量往左靠
	    }
	    if(i<=n)
	       f[top].v=a[i],f[top].i=i,ans_max[f[top].i].l=l,top++;//压入栈,记录左端点
	}
	for(int i=1;i<=n;i++)
	    ansmax+=a[i]*(i-ans_max[i].l+1)*(ans_max[i].r-i+1);//计算max的和 *注意:要乘a[i]
	for(int i=1;i<=n;i++)
	    ansmin+=a[i]*(i-ans_min[i].l+1)*(ans_min[i].r-i+1);//计算min的和 *注意:要乘a[i]
	printf("%lld",ansmax-ansmin);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值