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. node 读取多个文件、合并多个文件、读写多个文件

    一.读取文件 1.找文件中匹配的内容 let fs = require('fs') let content = fs.readFileSync('/Users/**/desktop/Test.txt' ...

  2. ubuntu 常见命令整理

    SSH 查看ssh服务的进程是否已经开启ps -e | grep ssh 安装ssh服务组件sudo apt-get install openssh-server 服务启动和关闭 方法1:servic ...

  3. Linux环境nginx的配置

    1.创建nginx运行账户www并加入到www组,不允许www用户直接登录系统,查看创建. groupadd www useradd -g www www -s /bin/false tail /et ...

  4. eclipse 出现内存溢出问题解决办法

    1.eclipse.ini添加设置: -vm#eclipse启动使用的jdk设置,路径根据自己实际路径修改 C:/Program Files/Java/jdk1.6.0_45/bin/javaw.ex ...

  5. scrapy 日志处理

    Scrapy生成的调试信息非常有用,但是通常太啰嗦,你可以在Scrapy项目中的setting.py中设置日志显示等级: LOG_LEVEL = 'ERROR' 日志级别 Scrapy日志有五种等级, ...

  6. CentOS 4.8/OEL4.8 配置本地yum

    下载createrepo包并安装 下载地址: ftp://195.220.108.108/linux/dag/redhat/el4/en/i386/dag/RPMS/createrepo-0.4.6- ...

  7. Spring Boot 2 (二):Spring Boot 2 动态 Banner

    Spring Boot 2 (二):Spring Boot 2 动态 Banner Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner. 一.配置依赖 使用 Sp ...

  8. [c/c++] programming之路(9)、运算优先级

    一.运算优先级 二.条件运算符(表达式1?表达式2:表达式3) 当式1正确时,取式2的值:否则,取式3的值 三.格式字符 #include<stdio.h> #include<std ...

  9. makefile如果没有符合的显式规则将会使用隐式规则

    举例: 当前目录下有个Makefile和jello.c文件,其中有这样的规则jello.o:%.c %.h Makefile (静态模式规则),表明的含义为:要生成的jello.o目标依赖jello. ...

  10. (转)Spring Cloud(二)

    (二期)23.微服务框架spring cloud(二) [课程23]熔断器-Hystrix.xmind0.1MB [课程23]微服务...zuul.xmind0.2MB 熔断器-Hystrix 雪崩效 ...