Imbalanced Array CodeForces - 817D (思维+单调栈)

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 valuesof all subsegments of this array.

For example, the imbalance value of array [1, 4, 1] is 9, because there are 6different 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

31 4 1

Output

9

题意:给定一个含有N个数的数组,求这个数组的所有连续区间的不平衡值的总和。一个区间的不平衡值为这个区间中的最大值减去最小值。思路:我们可以这样思考,这个题目的答案可以这样得来,对于每一个a[i],它对答案的贡献值为以a[i]为区间最大值的区间数量*a[i]-a[i]*以a[i]为区间最小值的区间数量。ans=sum{ contribution(a[i])}

那么我们的难题为如何计算以a[i]为区间最大/最小值的区间数量。这种模型我们很容易想到利用单调栈来O(N)求出。我们定义两个数组,l[n+5],r[n+5],l[i]和r[i] 分别维护的是从a[i]元素开始向左和向右第一个比a[i]小的元素的位置。(注意边界)然后我们可以通过l[i]和r[i]来求出区间数量,然后我们再利用单调栈求出从a[i]元素开始向左和向右第一个比a[i]大的元素的位置
然后我们愉快的计算每一个的贡献然后加起来就是我们要求的答案。细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int l[maxn];
int r[maxn];
ll ans=0ll;
int main()
{
    gg(n);
    repd(i,1,n)
    {
        gg(a[i]);
    }
    stack<int> s;
    repd(i,1,n)
    {
        while(s.size()&&a[s.top()]>a[i])
        {
            s.pop();
        }
        if(s.size())
        {
            l[i]=s.top();
        }else
        {
            l[i]=0;
        }
        s.push(i);
    }
    while(s.size())
    {
        s.pop();
    }
    for(int i=n;i>=1;i--)
    {
        while(s.size()&&a[s.top()]>=a[i])
        {
            s.pop();
        }
        if(s.size())
        {
            r[i]=s.top();
        }else
        {
            r[i]=n+1;
        }
        s.push(i);
    }
    repd(i,1,n)
    {
        ans=ans-1ll*a[i]*(i-l[i])*(r[i]-i);
    }
//    db(ans);
    while(s.size())
    {
        s.pop();
    }
    repd(i,1,n)
    {
        while(s.size()&&a[s.top()]<a[i])
        {
            s.pop();
        }
        if(s.size())
        {
            l[i]=s.top();
        }else
        {
            l[i]=0;
        }
        s.push(i);
    }
    while(s.size())
    {
        s.pop();
    }
    for(int i=n;i>=1;i--)
    {
        while(s.size()&&a[s.top()]<=a[i])
        {
            s.pop();
        }
        if(s.size())
        {
            r[i]=s.top();
        }else
        {
            r[i]=n+1;
        }
        s.push(i);
    }

    repd(i,1,n)
    {
        ans=ans+1ll*a[i]*(i-l[i])*(r[i]-i);
    }
    printf("%lld\n",ans );
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}



原文地址:https://www.cnblogs.com/qieqiemin/p/10332048.html

时间: 2024-08-30 06:58:30

Imbalanced Array CodeForces - 817D (思维+单调栈)的相关文章

Educational Codeforces Round 23 D. Imbalanced Array(单调栈)

题目链接:Educational Codeforces Round 23 D. Imbalanced Array 题意: 给你n个数,定义一个区间的不平衡因子为该区间最大值-最小值. 然后问你这n个数所有的区间的不平衡因子和 题解: 对每一个数算贡献,a[i]的贡献为 当a[i]为最大值时的 a[i]*(i-l+1)*(r-i+1) - 当a[i]为最小值时的a[i]*(i-l+1)*(r-i+1). 计算a[i]的l和r时,用单调栈维护.具体看代码,模拟一下就知道了. 然后把所有的贡献加起来.

[CF442C] Artem and Array (贪心+单调栈优化)

题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边相邻的数最小的分数.问你最多得到多少分. 将高度绘图,去除V的情况. 用单调栈优化,每个元素进栈一次,出栈一次.线性时间. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include

hdu 4923 Room and Moor (单调栈+思维)

题意: 给一个0和1组成的序列a,要构造一个同样长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列开始的连续0和末尾的连续1是可以不考虑的.因为只要b序列对应开头为0. 末尾为1,既不影响单调性又能使对应的(ai-bi)^2=0. 然后, 先找111100.11100.10这样以1开始以0结束的序列块.每一块对应的b值相等且均为 这一块的平均值,即1的个数/0和1的总个数. 但是要满足b的单调性,则我们用栈来维护,如果后面一

CodeForces 548D 单调栈

Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 548D Appoint description: Description Mike is the president of country What-The-Fatherland. There are n bears living in this

Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次数的平方和. $|s|\leq 10^5$ 题解 首先,这一题用$SAM$做就是模板题,比较简单. 但是,本着练一练$SA$的心态,我开始了$SA+单调栈$的苦海. 真毒瘤. 这里讲一讲$SA$的做法,也是经典的做法. $SA$闭着眼睛先写了再说. 首先,我们考虑出现次数大于$1$次的子串. 考虑按

[Codeforces Round #622 (Div. 2)] - C2. Skyscrapers (hard version) (单调栈)

[Codeforces Round #622 (Div. 2)] - C2. Skyscrapers (hard version) (单调栈) C2. Skyscrapers (hard version) time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output This is a harder version of the probl

Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 单调栈

Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 问题 传送门 我是参考了这篇题解传送门,然后按着思路做出了的(但大佬题解中的sumr[]数组操作我没看懂,然后自己改了改). 摘抄: 维护峰值最优 找左右边的第一个比自己小的元素,维护前缀和,找最大的峰值 l[i]:用单调栈维护左边第一个比它小的数 r[i]:用单调栈维护右边第一个比它小的数 suml[i]:左边的前缀和 sumr[i]:右边的前缀和 然后遍历一遍数组,找到

Codeforces Round #305 (Div. 2)D---Mike and Feet(单调栈)

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈

题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, r]内所有子区间最大值的和. 这个问题可以利用单调栈来做. 每次找到当前点左面第一个大于当前值的点, 然后更新答案. 姿势很多. 1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import