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. poj1185 [NOI2001炮兵阵地]

    题目链接 状压DP 本来如果考虑所有情况应该开hh[n][2^10][2^10]表示i行在i-1的状态为j,i-2的状态为k的最大个数 但是由于每行中的人互相限制所以在m=10时只有60种情况 空间就 ...

  2. flask框架----flask入门

    一.Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是 ...

  3. 如何使用Wisdom RESTClient定制满足您个性化需求的API文档?

    Wisdom RESTClient 支持自动化测试RESTful API,输出精美的测试报告,生成精美的RESTful API文档. 这里介绍一下如何定制个性化的RESTful API文档. 定制个性 ...

  4. mxnet 查看 Sym shape

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

  5. strpos 的正确使用方式

    首先简单介绍下strpos函数: strpos函数是查找某个字符在字符串中的位置:这里需要明确这个函数的作用: 这个函数得到的是位置:如果存在:返回数字:否则返回的是false: 而很多时候我们拿这个 ...

  6. SaaS的中年危机(转)

    如果说SaaS软件和人有什么地方很像的话,中年危机一定是其中一个.另一个是交税. 经常有人问我,春阳,你觉得xx SaaS公司怎么样? 如果这是一家成立2年以上的公司,我的回答多半是“活的不是那么滋润 ...

  7. jar中META-INF

    一直记得META-INF中只有在直接启动jar可执行文件时需要在manifest中配置启动类,最近看dubbo的配置,发现dubbo的配置都丢在META-INF下,特地搜索了下,官网对于META-IN ...

  8. Java线程池详解,看这篇就够了!

    构造一个线程池为什么需要几个参数?如果避免线程池出现OOM?Runnable和Callable的区别是什么?本文将对这些问题一一解答,同时还将给出使用线程池的常见场景和代码片段. 基础知识 Execu ...

  9. Educational Codeforces Round 40 (Rated for Div. 2) Solution

    从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...

  10. caffe中的若干问题

    找不到cublas....: 在/etc/ld.so.conf文件夹中新建cuda.conf,里面添加/usr/local/cuda/lib64,然后sudo /sbin/ldconfig -v. c ...