D. 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

题意:给你n个数,一个区间的贡献是区间最大值-区间最小值,求所有区间贡献和。

思路:首先想到n*n的rmq,明显超时。

   换个角度,枚举每个数作为最小值的区间个数,枚举每个数作为最大值的区间个数;

   怎么算呢,例如求最小值的区间,找出左边有x个比其大的,右边x个比其大的,区间的个数为x*y;

   这个用单调栈可以O(n)处理,注意需要半开半闭;

  

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e6+,inf=,mod=1e9+;
const LL INF=1e18+,MOD=1e9+; int a[N],d[N];
int zn[N],yn[N];
int zx[N],yx[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
a[]=;
int k=;d[++k]=;
for(int i=;i<=n;i++)
{
while(a[d[k]]>=a[i])k--;
zn[i]=d[k];
d[++k]=i;
}
a[n+]=;
k=;d[++k]=n+;
for(int i=n;i>=;i--)
{
while(a[d[k]]>a[i])k--;
yn[i]=d[k];
d[++k]=i;
}
a[]=1e6+;
k=;d[++k]=;
for(int i=;i<=n;i++)
{
while(a[d[k]]<=a[i])k--;
zx[i]=d[k];
d[++k]=i;
}
a[n+]=1e6+;
k=;d[++k]=n+;
for(int i=n;i>=;i--)
{
while(a[d[k]]<a[i])k--;
yx[i]=d[k];
d[++k]=i;
}
LL ans=;
for(int i=;i<=n;i++)
{
ans+=1LL*(yx[i]-i)*(i-zx[i])*a[i];
ans-=1LL*(yn[i]-i)*(i-zn[i])*a[i];
}
cout<<ans<<endl;
return ;
}

Educational Codeforces Round 23 D. Imbalanced Array 单调栈的更多相关文章

  1. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  2. Educational Codeforces Round 23 A-F 补题

    A Treasure Hunt 注意负数和0的特殊处理.. 水题.. 然而又被Hack了 吗的智障 #include<bits/stdc++.h> using namespace std; ...

  3. Educational Codeforces Round 23 补题小结

    昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...

  4. Educational Codeforces Round 11 A. Co-prime Array 水题

    A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...

  5. Educational Codeforces Round 23 B. Makes And The Product

    B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Educational Codeforces Round 23 E. Choosing The Commander trie数

    E. Choosing The Commander time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Educational Codeforces Round 23.C

    C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. vue去掉严格开发,即去掉vue-cli安装时的eslint

    vue去掉严格开发,即去掉vue-cli安装时的eslint : 1.vue-cli书写规范(主要是js规范) a.逗号.冒号后面要加空格 b.不能使用双引号,一律使用单引号 webpack的语法检查 ...

  2. Codeforce 507B - Amr and Pins

    Amr loves Geometry. One day he came up with a very interesting problem. Amr has a circle of radius r ...

  3. python里面的xlrd模块

    ♦python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 今天就先来说一下xlrd模块: 一.安装xlrd模块 ♦ 到python官网下载 ...

  4. Hadoop学习笔记之二:NameNode

    NameNode对三大协议接口(NamenodeProtocol.ClientProtoco.DatanodeProtocol)进行实现,利用ipc::Server通过三个协议分别向SNN.Clien ...

  5. 前端框架VUE----cli脚手架(框架)

    一.创建vue项目 npm install vue-cli -g #-g全局 (sudo)npm install vue-cli -g #mac笔记本 vue-init webpack myvue # ...

  6. Linux 安装 mysql 数据库

    1. 克隆虚拟机 2. 上传安装文件 1.上传文件 2.解压文件 tar -xvf 文件 3. 安装数据库 安装顺序: .debuginfo .shared .client .server 1. rp ...

  7. Python进阶【第十一篇】模块(下)之常用模块

    内置模块是Python自带的功能,在使用内置模块相应的功能时,需要[先导入]再[使用] 一.time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳 ...

  8. solr 使用指定数据源

    1,将solr 解压,我们观察发现它其实即可以是web服务也可以做数据分析 数据库 2,我们在example目录下新建一个hai的文件夹,用于存放数据 参考solr 目录,将solr.xml 复制一份 ...

  9. mxnet 查看 Sym shape

    import mxnet as mximport numpy as npimport randomimport mxnet as mximport sysdata_shape = {'data':(6 ...

  10. Python小项目四:实现简单的web服务器

    https://blog.csdn.net/u010103202/article/details/74002538 本博客是整理在学习实验楼的课程过程中记录下的笔记形成的,参考:https://www ...