CodeForses_871D Imbalanced Array

题目描述

CodeForses871D. Imbalanced Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a consisting of 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] is 9, because there are 6 different subsegments of this array:

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

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

The first line contains one integer n (1 ≤ n ≤ 106) — size of the array a.

The second line contains n integers a1, a2… an (1 ≤ ai ≤ 106) — elements of the array.
Output

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

3
1 4 1

Output

9

网址

CodeForses: 点这里.

想法

一开始在训练的时候居然想不到这个题该怎么写,哎好难受,看着过了的人那么多,看了一个小时都没看出来居然是和上次训练的第一题类似的,就是原本是求 “ 任意l-r所有最小值的和”现在编程Max - Min就不知道了,好菜好菜。
可以先按照加法求了Max的和,再按照减法求Min的和。


单调栈,主要就是求被两个高峰,或者低谷夹住的部分的一些属性。这里呢,被两个小的值夹住,就是作为最小值的贡献,被两个大的值中间的数作为最大值的贡献。(比如 5 4 5,4只有在两个五中间才作为最大值存在)。

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>

using namespace std;
const int N = 1e6 + 1e3;
const int Maxn = 0x3f3f3f3f;
typedef long long int LL;
LL num[N] , n , path[N] , bp[N];
stack<LL> sta;

void init(){
	for(int i = 1 ; i <= n ; i ++ ) path[i] = i;
}
LL getDist(LL indexl , LL indexmid , LL indexr){
	return ((indexmid-indexl+1)*(indexr-indexmid));
}
int main(){
	std::ios::sync_with_stdio(false);
	LL i , j ;
	LL res = 0;
	cin>>n;
	init();
	for(i = 1 ; i <= n ; i ++ ){
		cin>>num[i];bp[i] = num[i];
	}
	num[i] = bp[i] = Maxn;n++;
	//先算 每个数作为Max的贡献 
	for(i = 1 ; i <= n ; i ++ ){
		if(sta.empty() || num[sta.top()] > num[i]){
			sta.push(i);
		}else{
			int tem;
			while(sta.size() && num[sta.top()] <= num[i]){
				tem = sta.top();
				res += num[sta.top()] * getDist(sta.top() , path[sta.top()] , i);
				sta.pop();
			}
			num[tem] = num[i];//为了让num[tem]在计算的 
			path[tem] = i;//等于num[i],因为它影响到tem
			sta.push(tem);//在这个范围内,它最大。path记录了 
		}//i的原本位置,现在移动到了tem位置,为了之后方便计算 
	}
	/*中间的重置
	num[n] = bp[n] = -1;
	init();
	/*计算Min的副作用
	while(sta.size()) sta.pop();
	for(i = 1 ; i <= n ; i ++ ){
		num[i] = bp[i];
		if(sta.empty() || num[sta.top()] < num[i]){
			sta.push(i);
		}else{
			int tem;
			while(sta.size() && num[sta.top()] >= num[i]){
				tem = sta.top();
				res -= num[sta.top()]*getDist(sta.top() , path[sta.top()] , i);
				sta.pop();
			}
			num[tem] = num[i];
			path[tem] = i;
			sta.push(tem);
		}
	}
	cout<<res<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值